bash 如何使SSH命令执行超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15785832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make SSH command execution to timeout
提问by gmhk
I have a program like this:
我有一个这样的程序:
ssh -q [email protected] exit
echo "output value -->$?"
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.
在上面的代码中,我试图通过 SSH 连接到远程服务器,并尝试检查我是否可以连接。我的服务器很少,密码较少的服务器被激活,而密码尚未停用的服务器也很少。所以我担心,如果有密码,它会卡在要求输入密码的同一个屏幕上,并且它会在那里无限长时间,没有退出逻辑。
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) When I execute the above code, i get the following error codes:
2)当我执行上面的代码时,我得到以下错误代码:
127 -- > I guess its for success
225 -- > for any error.
are there any other error codes other than above?
除了上述错误代码还有其他错误代码吗?
回答by Austin Phillips
You could wrap the call to sshusing the timeoutcommand. The timeoutcommand exits with code 124 if a timeout occurs.
您可以将调用包装为sshusingtimeout命令。timeout如果发生超时,该命令将退出,代码为 124。
timeout 10s ssh -q [email protected] exit
if [ $? -eq 124 ]; then
echo "Timeout out"
fi
Or, as Vorsprung has commented on your question (as I was looking up the man page!):
或者,正如 Vorsprung 对您的问题所做的评论(因为我正在查找手册页!):
ssh -oPasswordAuthentication=no -q [email protected] exit
which will disallow interactive password authentication. You'd then have to check the exit code.
这将禁止交互式密码身份验证。然后您必须检查退出代码。
回答by michael
It sounds like the question is asking about 'batch mode' processing against a number of hosts at once (e.g., in a loop). The following sshcommand will simply fail if PKA is notconfigured for a given host (public key authentication -- the "password-less" login mentioned the original question), so it is good for scripts that you just want to continue running regardless of problems connecting to one or two hosts. It also won't bother with all the queries about "are you sure you want to continue connecting" to previously unknown hosts (or you can just add StrictHostKeyChecking noto your ~/.ssh/configfor that). For example,
听起来这个问题是在询问一次针对多个主机的“批处理模式”处理(例如,在循环中)。ssh如果未为给定主机配置PKA,则以下命令将简单地失败(公钥身份验证——“无密码”登录提到了原始问题),因此对于您只想继续运行而不管问题如何的脚本很有用连接到一台或两台主机。它也不会打扰所有关于“您确定要继续连接”到以前未知的主机的查询(或者您可以为此添加StrictHostKeyChecking no到您的主机~/.ssh/config)。例如,
for host in host1 host2 host3 # etc
do
ssh -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=5 \
user@$host 'uptime' || echo "problem: host=$host"
done
You'll have to check your (client) sshman page to see if these options (BatchMode, ConnectTimeout) are supported, though.
不过,您必须检查您的(客户端)ssh手册页以查看是否支持这些选项 ( BatchMode, ConnectTimeout)。

