bash-从远程ssh命令捕获退出代码 [英] bash - Capture exit code from remote ssh command
问题描述
我正在寻找在shell脚本中捕获远程ssh命令的退出状态我有一个如下的shell脚本:
I'm looking at capturing the exit status of a remote ssh command within a shell script I have a shell script as follows :
function rSSH {
local x
echo "Running ssh command"
x=$(ssh -o StrictHostKeyChecking=no -i $keyPair -o ProxyCommand="ssh -W %h:%p -i $keyPair user@$bastionIP 22" user@$IP "$cmd")
x=$?
echo "SSH status is : $x"
if [ $x -eq 0 ];then
echo "Success"
else
echo "Failure"
exit 1
fi
}
rSSH
exit 0
当我使用无效的$ bastionIP(测试失败方案)将上述脚本作为后台作业执行时,其结果是退出代码为0(而不是1),并且我在日志中看到的唯一信息是第一个回显正在运行ssh命令",它只是退出脚本.
When I execute the above script as a background job with an invalid $bastionIP( testing failure scenario), it results with an exit code of 0 ( instead of 1) and the only information I see in the logs is the first echo "Running ssh command" and it just exits the script.
有人可以指出我做错了什么还是捕获远程ssh命令退出状态的更好方法.感谢任何帮助.
Can someone point out what I'm doing wrong or a better way to capture the exit status of the remote ssh command. Appreciate any help.
推荐答案
该脚本似乎正在 set -e
下运行,这意味着当 ssh
连接失败时,脚本立即退出.
The script appears to be running under set -e
, which means when the ssh
connection fails, the script exits immediately.
这篇关于bash-从远程ssh命令捕获退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!