SSH 远程命令退出代码 [英] SSH Remote command exit code

查看:55
本文介绍了SSH 远程命令退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于它的讨论,但我需要你帮助解决 ssh 远程命令退出代码.我有那个代码:(scan 是一个脚本,用于扫描给定文件中的病毒)

I know there are lots of discussions about it but i need you help with ssh remote command exit codes. I have that code: (scan is a script which scans for viruses in the given file)

for i in $FILES 
do
    RET_CODE=$(ssh $SSH_OPT $HOST "scan $i; echo $?")
    if [ $? -eq 0 ]; then
        SOME_CODE

扫描有效,如果发现病毒,它将返回 0 或(1 表示错误)或 2.但不知何故,我的返回码始终为 0.即使我扫描了病毒.

The scan works and it returns either 0 or (1 for errors) or 2 if a virus is found. But somehow my return code is always 0. Even, if i scan a virus.

这里是 set -x 输出:

++ ssh -i /home/USER/.ssh/id host 'scan Downloads/eicar.com; echo 0'
+ RET_CODE='File Downloads/eicar.com: VIRUS: Virus found.
    code of the Eicar-Test-Signature virus
0'

如果我在没有 ssh 的远程"机器上运行这些命令,这是输出:

Here is the Output if i run those commands on the "remote" machine without ssh:

[user@ws ~]$ scan eicar.com; echo $?
File eicar.com: VIRUS: Virus found.
    code of the Eicar-Test-Signature virus
2

我只想要返回代码,我不需要 scan 的所有其他输出.

I just want to have the return Code, i dont need all the other output of scan.

!更新!

看起来,echo 是问题所在.

It seems like, echo is the problem.

推荐答案

你的 ssh 总是返回 0 的原因是因为最后的 echo> 命令总是成功!如果您想从 scan 获取返回码,请删除 echo 或将其分配给变量并使用 exit.在我的系统上:

The reason your ssh is always returning 0 is because the final echo command is always succeeding! If you want to get the return code from scan, either remove the echo or assign it to a variable and use exit. On my system:

$ ssh host 'false'
$ echo $?
1
$ ssh host 'false; echo $?'
1
$ echo $?
0
$ ssh host 'false; ret=$?; echo $ret; exit $ret'
1
$ echo $?
1

这篇关于SSH 远程命令退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆