如何使用Pexpect监控SSH隧道的健康状况?

时间:2017-01-04 20:50:36

标签: python python-2.7 pexpect

我使用Pexpect模块完成了SSH隧道,我只允许读取该模块。如何检查连接是否仍在运行,例如如果在此期间有任何网络连接问题?我的隧道的另一端随机发送消息,因此可能有一天在流中没有任何数据。我已经检查了pexpect.isalive()功能,但似乎没有检测到网络连接已关闭。

1 个答案:

答案 0 :(得分:1)

我认为您可以使用ssh的{​​{1}}和ServerAliveInterval选项:

ServerAliveCountMax

如果您的ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=3 user@host ... 服务器不支持这些选项,您仍然可以尝试ssh

TCPKeepAlive

然后在ssh -o TCPKeepAlive=yes user@host ... 脚本中,您只需要检查pexpect

以下内容来自pexpect.EOF手册页:

ssh_config