首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用Pexpect监测SSH隧道的健康状况?

如何用Pexpect监测SSH隧道的健康状况?
EN

Stack Overflow用户
提问于 2017-01-04 20:50:35
回答 1查看 712关注 0票数 1

我用Pexpect模块完成了一个SSH隧道,这是我只能阅读的。如何检查连接是否仍在启动和运行,例如,在此期间是否存在网络连接问题?隧道的另一边随机发送消息,所以可能有一天流中没有任何数据。我检查了pexpect.isalive()函数,但似乎没有检测到网络连接中断。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-05 03:38:35

我认为你可以使用sshServerAliveIntervalServerAliveCountMax选项:

代码语言:javascript
运行
复制
ssh -o ServerAliveInterval=15 -o ServerAliveCountMax=3 user@host ...

如果您的ssh服务器不支持这些选项,仍然可以尝试TCPKeepAlive

代码语言:javascript
运行
复制
ssh -o TCPKeepAlive=yes user@host ...

然后,在您的pexpect脚本中,只需要检查pexpect.EOF

以下内容来自ssh_config手册页:

代码语言:javascript
运行
复制
 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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41473000

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档