如何使用tcl-expect将文件从remotehost复制到localhost

时间:2011-08-23 09:22:58

标签: tcl expect

这里我使用telnet连接两台机器,在远程主机中创建一个名为“file.txt”的文件,并在file.txt中存储“ls”命令执行的结果。现在我需要将文件发送到本地主机。我不知道该怎么做。任何知道它的人都会帮助我。

#!/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"

1 个答案:

答案 0 :(得分:3)

强烈建议您使用sshscp执行此任务。

#!/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
    }
}