如何使SSH命令执行超时 [英] how to make SSH command execution to timeout

查看:667
本文介绍了如何使SSH命令执行超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个程序:

ssh -q harigm@8.19.71.238 exit
echo "output value  -->$?"

在上面的code,我想SSH到远程服务器,并试图检查,如果我可以连接与否。
我有几台服务器,这是密码较少被激活,几台服务器的哪个密码仍然尚未取消激活。
所以我的关注,如果有一个密码,它会卡住在同一屏幕上要求输入密码,这将是有无限的时间,没有退出的逻辑。

In the above code, I am trying to SSH to the remote server, and tries to check if I can connect or not. I have few servers, which is password less is activated and few servers for which passwords are still not yet deactivated. So my concern, if there is a password, it will get stuck in the same screen asking for the password and it will be there for infinite time, without Exit logic.

问题:如何落实上述code中的计时器,如果它停留在相同的屏幕要求输入密码。并与一些错误code出口

Question : How to implement the timers for the above code, and if it stays in the same screen asking for the password. and exit with some error code

2)当我执行上述code,我得到以下错误codeS:

2) When I execute the above code, i get the following error codes:

127 -- > I guess its for success
225 -- > for any error.

在那里除上述以外的其他任何错误codeS?

are there any other error codes other than above?

推荐答案

您可以使用超时包装调用 SSH 命令。在暂停与code 124命令退出时如果发生暂停。

You could wrap the call to ssh using the timeout command. The timeout command exits with code 124 if a timeout occurs.

timeout 10s ssh -q harigm@8.19.71.238 exit
if [ $? -eq 124 ]; then
    echo "Timeout out"
fi

或者,如Vorsprung对您的问题发表意见的(因为我是仰视男人页面!)

Or, as Vorsprung has commented on your question (as I was looking up the man page!):

ssh -oPasswordAuthentication=no -q harigm@8.19.71.238 exit

这将不允许交互式密码验证。然后,你必须检查出code。

which will disallow interactive password authentication. You'd then have to check the exit code.

这篇关于如何使SSH命令执行超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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