“ssh root @ server”永远挂起

有时我的ssh客户端将永远登录.

当服务器没有响应(重载,终止处理,……)时会发生这种情况.我的自动脚本将失败,因为ssh进程永远不会退出.

如果ssh在预定义的秒数后无法登录,是否有ssh配置值来设置超时以便失败?

我知道服务器端有旋钮,但我必须在客户端设置这个,因为sshd进程没有响应或响应不正确.

谢谢!

解决方法

我见过甚至设置ConnectTimeout都不起作用的情况.当使用自动ssh连接到大量服务器时,这可能会特别烦人.我的解决方案是在客户端使用一个包装程序,如果它没有连接并足够快地返回,则会终止ssh进程.像这样(在perl中):
$SshCmd = "ssh server.example.com uname -a";
$TimeOut = 120;
eval {
  local $SIG{ALRM} =
    sub {
      # ignore SIGHUP here so the kill only affects children.
      local $SIG{HUP} = 'IGnorE';
      kill 1,(-$$);
      print STDERR "ssh terminated,max run time of $TimeOut seconds exceeded.\n";
    };
  alarm $TimeOut;
  system ($SshCmd) || die "Failed to run $SshCmd: $!";
  alarm 0;
};
$SIG{HUP} = 'DEFAULT';

设置$TimeOut秒的警报,并在超过警报时杀死子节点(ssh命令).

相关文章

1、安装Apache。 1)执行如下命令,安装Apache服务及其扩展包...
一、先说一下用ansible批量采集机器信息的实现办法: 1、先把...
安装配置 1. 安装vsftpd 检查是否安装了vsftpd # rpm -qa | ...
如何抑制stable_secret读取关键的“net.ipv6.conf.all.stabl...
1 删除0字节文件 find -type f -size 0 -exec rm -rf {} ...
## 步骤 1:安装必要的软件包 首先,需要确保系统已安装 `dh...