MRo*_*lin 9 python ssh paramiko
有没有办法检查paramikoSSH连接是否仍然存在?
In [1]: import paramiko
In [2]: ssh = paramiko.SSHClient()
In [3]: ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
In [4]: ssh.connect(hostname)
Run Code Online (Sandbox Code Playgroud)
我想模仿这样的东西(目前还不存在)
In [5]: ssh.isalive()
True
Run Code Online (Sandbox Code Playgroud)
use*_*286 14
我观察到is_active()返回误报.
我建议使用这件作品:
# use the code below if is_active() returns True
try:
transport = client.get_transport()
transport.send_ignore()
except EOFError, e:
# connection is closed
Run Code Online (Sandbox Code Playgroud)
Jor*_*ley 10
if ssh.get_transport() is not None:
ssh.get_transport().is_active()
Run Code Online (Sandbox Code Playgroud)
应该这样做......假设我正确阅读了文档
小智 6
对我来说,上面的答案不起作用,所以我通过发送一个超时的exec_command()来完成它:
self.ssh.exec_command('ls', timeout=5)
Run Code Online (Sandbox Code Playgroud)
例如,完整的方法如下:
def check_connection(self):
"""
This will check if the connection is still availlable.
Return (bool) : True if it's still alive, False otherwise.
"""
try:
self.ssh.exec_command('ls', timeout=5)
return True
except Exception as e:
print "Connection lost : %s" %e
return False
Run Code Online (Sandbox Code Playgroud)
我每隔 5 秒左右调用一次。
| 归档时间: |
|
| 查看次数: |
14296 次 |
| 最近记录: |