linux – 是否可以设置’expect’的退出代码

以下bash脚本不起作用,因为命令’expect’始终返回0,无论远程脚本/tmp/my.sh返回哪个退出代码.

任何让它成功的想法?谢谢.

#!/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’的退出代码 - 代码日志