使用期望脚本进行scp'ing时出现“ spawn id exp5 not open”错误

时间:2018-08-27 08:34:57

标签: expect scp spawn

我有一个期望脚本,其开始如下:

#!/usr/bin/expect -f

set timeout 1200
set prompt {[#$] }

set server [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set package [lindex $argv 3]

spawn scp -o StrictHostKeyChecking=no "$package" ${username}@${server}:~

expect {
    timeout { send_user "\nscp connect time out\n"; exit 1 }
    "*assword"
}

after 3000

send "$password\r"

# wait for upload complete

expect {
    timeout { send_user "\nscp timeout\n"; exit 1 }
    eof
    }

....
close

在大约15秒后,出现以下错误消息:

send: spawn id exp5 not open
        while executing
    "send "$password\r""

这可能是什么原因? exp5是什么意思? 我也尝试过删除“ 3000之后”的语句,但出现相同的错误。

1 个答案:

答案 0 :(得分:0)

在您到达发送密码的位置时,连接已关闭。我怀疑您不需要为该服务器输入密码(是否设置了ssh密钥?),并且在3秒钟(after 3000)结束时,文件传输已完成。

您可以稍微压缩一下脚本:

#!/usr/bin/expect -f

set timeout 1200
set prompt {[#$] }

lassign $argv server username password package
# if your expect version does not have `lassign`, stick to below
#set server [lindex $argv 0]
#set username [lindex $argv 1]
#set password [lindex $argv 2]
#set package [lindex $argv 3]

spawn scp -o StrictHostKeyChecking=no "$package" ${username}@${server}:~

expect {
    timeout    {send_user "\nscp time out\n"; exit 1}
    "*assword" {send "$password\r"; exp_continue }
    eof
}

这会失去连接超时和传输超时之间的区别。