我用Pexpect模块完成了一个SSH隧道,这是我只能阅读的。如何检查连接是否仍在启动和运行,例如,在此期间是否存在网络连接问题?隧道的另一边随机发送消息,所以可能有一天流中没有任何数据。我检查了pexpect.isalive()函数,但似乎没有检测到网络连接中断。
发布于 2017-01-05 03:38:35
我认为你可以使用ssh的ServerAliveInterval和ServerAliveCountMax选项:
ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=3 user@host ...如果您的ssh服务器不支持这些选项,仍然可以尝试TCPKeepAlive。
ssh -o TCPKeepAlive=yes user@host ...然后,在您的pexpect脚本中,只需要检查pexpect.EOF。
以下内容来自ssh_config手册页:
ServerAliveCountMax
Sets the number of server alive messages (see below) which may be
sent without ssh(1) receiving any messages back from the server.
If this threshold is reached while server alive messages are
being sent, ssh will disconnect from the server, terminating the
session. It is important to note that the use of server alive
messages is very different from TCPKeepAlive (below). The server
alive messages are sent through the encrypted channel and there-
fore will not be spoofable. The TCP keepalive option enabled by
TCPKeepAlive is spoofable. The server alive mechanism is valu-
able when the client or server depend on knowing when a connec-
tion has become inactive.
The default value is 3. This option applies to protocol
version 2 only.
ServerAliveInterval
Sets a timeout interval in seconds after which if no data has
been received from the server, ssh(1) will send a message through
the encrypted channel to request a response from the server. The
default is 0, indicating that these messages will not be sent to
the server. This option applies to protocol version 2 only.
TCPKeepAlive
Specifies whether the system should send TCP keepalive messages
to the other side. If they are sent, death of the connection or
crash of one of the machines will be properly noticed. However,
this means that connections will die if the route is down tempo-
rarily, and some people find it annoying.
The default is ``yes'' (to send TCP keepalive messages), and the
client will notice if the network goes down or the remote host
dies. This is important in scripts, and many users want it too.https://stackoverflow.com/questions/41473000
复制相似问题