在这里,我使用 telnet 在两台机器之间建立连接,并在远程主机中创建一个名为“file.txt”的文件,并将“ls”命令执行的结果存储在 file.txt 中。现在我需要将文件发送到本地主机。我不知道该怎么做。任何知道的人都请帮助我。
#!/usr/local/bin/expect
spawn telnet <machine ip>
expect "login:"
send "<username>\n"
expect "Password:"
send "<password>\n"
expect "bash"
send " ls > file.txt\r "
expect "bash"
send " cat file.txt \r "
expect "bash"
send " command for copying file to the localhost computer\r"
expect "bash"
send "exit\r"
最佳答案
强烈建议您使用 ssh
和 scp
来执行此任务。
#!/usr/local/bin/expect
### Factor these out
set user <username>
set pass <password>
set host <machineIP>
### Get the file list into a file remotely
spawn ssh $user@$host sh -c {ls -1 >file.txt}
expect {
"Password:" {
send "$pass\r"
exp_continue
}
eof {
close
}
}
### Copy the file to the local machine
spawn scp $user@${host}:file.txt .
expect {
"Password:" {
send "$pass\r"
exp_continue
}
eof {
close
}
}
关于tcl - 如何使用 tcl-expect 将文件从远程主机复制到本地主机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158786/