我刚刚编写了一个非常简单的Expect脚本来包装rsync,但它似乎给了我麻烦。基本上,我正在自动化从rsync调用的SSH登录提示。我还必须通过rsync将参数传递给SSH,这样它就不会进行主机密钥检查。我很清楚SSH身份验证密钥和ssh-keygen,但我有充分的理由这样做,所以没有关于在命令行上传递密码的讲座。
#!/usr/local/bin/expect -f
if {$argc != 5} {
puts "usage: remoteCopy {remotehost, username, password, localfile, remoteloc}"
exit 1
}
set remotehost [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set localfile [lindex $argv 3]
set remoteloc [lindex $argv 4]
set timeout -1
spawn rsync -e \"ssh -q -o StrictHostKeyChecking=no\" $localfile $username@$remotehost:$remoteloc
expect "Password"; send "$password\r"
以下是脚本的完整输出:
avoelker@localhost $ ./remoteFileCopy.tcl remotehost remoteuser remotepass ~/localfile /tmp/
spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/
rsync: Failed to exec "ssh: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(83)
rsync: connection unexpectedly closed (0 bytes received so far) [sender]
rsync error: error in rsync protocol data stream (code 12) at io.c(434)
send: spawn id exp6 not open
while executing
"send "$password\r""
(file "./remoteFileCopy.tcl" line 17)
avoelker@localhost $
在我看来rsync正在尝试执行"ssh
而不仅仅是ssh
,但是,将Expect的stdout(rsync -e "ssh -q -o StrictHostKeyChecking=no" /localhost/home/avoelker/localfile remoteuser@remotehost:/tmp/
)的第一行输出复制并粘贴到shell工作得非常好,所以我知道它不是rsync的问题。
当我从spawn rsync
行完全删除-e参数时,Expect脚本执行时没有错误,但我必须在脚本中添加主机密钥检查,我不想这样做。< / p>
对Expect / Tcl更有经验的人是否知道我做错了什么?
答案 0 :(得分:6)
放下你的反斜杠。 spawn
行应该说:
spawn rsync -e "ssh -q -o StrictHostKeyChecking=no" $localfile $username@$remotehost:$remoteloc
毕竟,你不希望rsync
看到带引号的命令,对吗?