我的bash文件中包含以下行:
LIST=$(ssh 192.168.0.22 'ls -1 /web');
我遇到的问题是它是自动化脚本的一部分,我经常在stdout
而不是我需要的数据上获取此信息:
ssh_exchange_identification: Connection closed by remote host
我意识到,这LIST
只能获得stdout
的ls
。所以我正在寻找一个可以从命令中获取更多信息的命令。特别是:
stdout
对于ls
-我现在有那个stderr
因为ls
-并不是很感兴趣,我认为那里不会有问题stdout
对于ssh
-不感兴趣,我什至不知道它将输出什么stderr
对ssh
-这就是我要找来检查它是否ssh
正确。这是空的,应该意味着我有$LIST
预期的数据从Ubuntu 16.04(LTS)的ssh手册页中:
EXIT STATUS
ssh exits with the exit status of the remote command or with 255 if an error occurred.
知道了,我们可以检查ssh
命令的退出状态。如果退出状态为225
,我们就知道这是一个ssh
错误,如果它是其他任何非零值,那就是ls
错误。
#!/bin/bash
TEST=$(ssh $USER@localhost 'ls /proc' 2>&1)
if [ $? -eq 0 ];
then
printf "%s\n" "SSH command successful"
elif [ $? -eq 225 ]
printf "%s\n%s" "SSH failed with following error:" "$TEST"
else
printf "%s\n%s" "ls command failed" "$TEST"
fi
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句