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