Expect 脚本在调用 "expect eof" 时因 "spawn id not open" 而失败
Expect script failed with "spawn id not open" when calling "expect eof"
我正在尝试从 bash 调用一个 expect 脚本并让它 return 退出代码以防万一连接超时。
基本上 bash 脚本是这样调用 expect 的:
if [[ -f ]]; then
do something
else
script.exp $device
# here I want to evaluate the exit code but it is always 0
fi
我之前发现了一些关于它的问题,捕获退出代码的正确方法是:
expect eof
catch wait result
exit [lindex $result 3].
预期脚本:
#!/usr/bin/expect
exp_internal 1
log_user 1
set timeout -1
set hostname [lindex $argv 0]
set prompt "sftp>";
if { $hostname == "blah" } {
set username "user1"
set password "pass1"
} else {
set username "user2"
set password "pass2"
}
spawn sftp $username@$hostname "-o ConnectTimeout=3"
expect {
"timeout" {
puts "A timeout occured"
exp_continue
}
"password:" {
send "$password\r"
exp_continue
}
"sftp>" {
send "cd cfg\n"
expect $prompt
send "get * /some/dir/$hostname\n"
expect $prompt
send "get running-config /some/dir/$hostname-config\n"
expect $prompt
send "exit\r";
exp_continue
}
}
expect eof
catch wait result
exit [lindex $result 3]
当我测试 expect 脚本以查看会发生什么时,出现此错误:
expect: spawn id exp4 not open
while executing
"expect eof"
我试过移动 expect eof 但它并没有改变行为。它指出生成的进程 ID 未打开,我猜这是正确的,因为它已退出并关闭了 Timed Out/Connection?
(合并之前的评论)
将它添加到之前的 expect 命令中,但没有正文:
expect {
timeout {...}
password: {...}
sftp> {...}
eof
}
wait result
if {[lindex $result 2] == 0} {
exit [lindex $result 3]
} else {
error "system error: errno [lindex $result 3]"
}
- 在
send "exit\r"
之后删除 exp_continue
,否则 EOF 将被 expect { ... }
块消耗,另一个 expect eof
将失败。
- 将
lindex $result 3
更改为评论中的lindex $result 3
。
我正在尝试从 bash 调用一个 expect 脚本并让它 return 退出代码以防万一连接超时。
基本上 bash 脚本是这样调用 expect 的:
if [[ -f ]]; then
do something
else
script.exp $device
# here I want to evaluate the exit code but it is always 0
fi
我之前发现了一些关于它的问题,捕获退出代码的正确方法是:
expect eof
catch wait result
exit [lindex $result 3].
预期脚本:
#!/usr/bin/expect
exp_internal 1
log_user 1
set timeout -1
set hostname [lindex $argv 0]
set prompt "sftp>";
if { $hostname == "blah" } {
set username "user1"
set password "pass1"
} else {
set username "user2"
set password "pass2"
}
spawn sftp $username@$hostname "-o ConnectTimeout=3"
expect {
"timeout" {
puts "A timeout occured"
exp_continue
}
"password:" {
send "$password\r"
exp_continue
}
"sftp>" {
send "cd cfg\n"
expect $prompt
send "get * /some/dir/$hostname\n"
expect $prompt
send "get running-config /some/dir/$hostname-config\n"
expect $prompt
send "exit\r";
exp_continue
}
}
expect eof
catch wait result
exit [lindex $result 3]
当我测试 expect 脚本以查看会发生什么时,出现此错误:
expect: spawn id exp4 not open
while executing
"expect eof"
我试过移动 expect eof 但它并没有改变行为。它指出生成的进程 ID 未打开,我猜这是正确的,因为它已退出并关闭了 Timed Out/Connection?
(合并之前的评论)
将它添加到之前的 expect 命令中,但没有正文:
expect {
timeout {...}
password: {...}
sftp> {...}
eof
}
wait result
if {[lindex $result 2] == 0} {
exit [lindex $result 3]
} else {
error "system error: errno [lindex $result 3]"
}
- 在
send "exit\r"
之后删除exp_continue
,否则 EOF 将被expect { ... }
块消耗,另一个expect eof
将失败。 - 将
lindex $result 3
更改为评论中的lindex $result 3
。