简体   繁体   English

我可以从ssh命令获得某种返回值吗?

[英]Can I get a return value of some kind from a ssh command?

I'm running the following command in a while loop, going through a list of machines: 我在while循环中运行以下命令,遍历机器列表:

ssh -n -o ConnectTimeout=10 hostname 'smbios-sys-info' >> MACHINEINFO

Works great. 效果很好。 Except when I run into a connection that fails or times out, it outputs the timeout error message into MACHINEINFO. 除非遇到连接失败或超时的连接,否则它将超时错误消息输出到MACHINEINFO。 I instead want it to silently fail without muddling MACHINEINFO, but instead write the hostname of the failed connection into another file like FAILEDMACHINES. 相反,我希望它在不使MACHINEINFO混乱的情况下静默失败,而是将失败的连接的主机名写入另一个文件,如FAILEDMACHINES。

I assume to do something like this I'd need to get a return value from ssh to determine whether or not the connection and smbios-sys-info command ran successfully, and if not, output to FAILEDMACHINES. 我假设要执行类似的操作,我需要从ssh获取返回值以确定连接和smbios-sys-info命令是否成功运行,如果没有成功,则输出到FAILEDMACHINES。

After running the command, $? 运行命令后, $? will give you the result code. 将为您提供结果代码。

If the ssh succeeds, $? 如果ssh成功,则$? will be the result code of the remote command, otherwise, it will return 255. 将是远程命令的结果代码,否则将返回255。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM