以下bash脚本不起作用,因为无论远程脚本/tmp/my.sh返回哪个退出代码,命令“expect”始终返回0。
有什么想法可以使它起作用吗?谢谢。
#!/usr/bash
user=root
passwd=123456abcd
host=10.58.33.21
expect -c "
spawn ssh -o StrictHostKeyChecking=no -l $user $host bash -x /tmp/my.sh
expect {
\"assword:\" {send \"$passwd\r\"}
eof {exit $?}
}
"
case "$?" in
0) echo "Password successfully changed on $host by $user" ;;
1) echo "Failure, password unchanged" ;;
2) echo "Failure, new and old passwords are too similar" ;;
3) echo "Failure, password must be longer" ;;
*) echo "Password failed to change on $host" ;;
esac
编辑于2013年11月27日上午10:23
感谢您的评论。让我再次强调这个问题,
该主脚本应该在Linux服务器A 上以静默方式运行,在此过程中,它将在服务器B 上无人参与的上调用另一个脚本my.sh。问题是如何获取my.sh 的退出代码?
这就是为什么我不能使用ssl_key方法的原因,这种方法至少需要一次配置。
最佳答案
#!/usr/bin/expect
set user root
set passwd 123456abcd
set host 10.58.33.21
set result_code 255
# exp_internal 1 to see internal processing
exp_internal 0
spawn ssh -o StrictHostKeyChecking=no -l $user $host bash -x /tmp/my.sh && echo aaa0bbb || echo aaa$?bbb
expect {
"assword:" {send "$passwd\r"; exp_continue}
-re "aaa(.*)bbb" {set result_code $expect_out(1,string)}
eof {}
timeout {set result_code -1}
}
switch $result_code {
0 { puts "Password successfully changed on $host by $user" }
1 { puts "Failure, password unchanged" }
2 { puts "Failure, new and old passwords are too similar" }
3 { puts "Failure, password must be longer" }
-1 { puts "Failure, timeout" }
default { puts "Password failed to change on $host" }
}
exit $result_code
关于linux - 是否可以设置 'expect'的退出代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20219931/