在 Expect 中获取远程脚本的结果
Get the result of remote script in Expect
我正在执行远程脚本并检查 return 脚本的状态,但是如果我按照以下方式进行操作,它 return 会检查密码的状态,而不是被调用的状态 script.How 我可以获取被调用脚本的 return 状态吗?请帮忙提前谢谢。
#!/usr/bin/expect
proc auto { } {
global argv
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no}
set script /path-to-script/test.sh
spawn ssh {*}$ssh_opts $user@$ip bash $script {*}$argv
expect "Password:"
send "$password\r"
send "echo $?\r"
expect {
"0\r" { puts "Test passed."; }
timeout { puts "Test failed."; }
}
expect eof
}
auto {*}$argv
您正在自动化 ssh bash remote_script
,因此您不会得到 shell 提示,您可以 echo $?
-- ssh 将启动您的脚本然后退出。
您需要做的是获取派生进程的退出状态(ssh 应该以远程命令的退出状态退出)。 expect 的 wait
命令获取退出状态(以及其他信息)
spawn ssh {*}$ssh_opts $user@$ip bash $script {*}$argv
expect {
"Password:" { send "$password\r"; exp_continue }
timeout { puts "Test failed." }
eof
}
# ssh command is now finished
exp_close
set info [wait]
# [lindex $info 0] is the PID of the ssh process
# [lindex $info 1] is the spawn id
# [lindex $info 2] is the success/failure indicator
if {[lindex $info 2] == 0} {
puts "exit status = [lindex $info 3]"
} else {
puts "error code = [lindex $info 3]"
}
我正在执行远程脚本并检查 return 脚本的状态,但是如果我按照以下方式进行操作,它 return 会检查密码的状态,而不是被调用的状态 script.How 我可以获取被调用脚本的 return 状态吗?请帮忙提前谢谢。
#!/usr/bin/expect
proc auto { } {
global argv
set timeout 120
set ip XXXX.XXX.XX.XX
set user name
set password pass
set ssh_opts {-o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no}
set script /path-to-script/test.sh
spawn ssh {*}$ssh_opts $user@$ip bash $script {*}$argv
expect "Password:"
send "$password\r"
send "echo $?\r"
expect {
"0\r" { puts "Test passed."; }
timeout { puts "Test failed."; }
}
expect eof
}
auto {*}$argv
您正在自动化 ssh bash remote_script
,因此您不会得到 shell 提示,您可以 echo $?
-- ssh 将启动您的脚本然后退出。
您需要做的是获取派生进程的退出状态(ssh 应该以远程命令的退出状态退出)。 expect 的 wait
命令获取退出状态(以及其他信息)
spawn ssh {*}$ssh_opts $user@$ip bash $script {*}$argv
expect {
"Password:" { send "$password\r"; exp_continue }
timeout { puts "Test failed." }
eof
}
# ssh command is now finished
exp_close
set info [wait]
# [lindex $info 0] is the PID of the ssh process
# [lindex $info 1] is the spawn id
# [lindex $info 2] is the success/failure indicator
if {[lindex $info 2] == 0} {
puts "exit status = [lindex $info 3]"
} else {
puts "error code = [lindex $info 3]"
}