Paramiko and exec_command - killing remote process?

前端 未结 8 1637
情书的邮戳
情书的邮戳 2020-12-09 05:09

I\'m using Paramiko to tail -f a file on a remote server.

Previously, we were running this via ssh -t, but that proved flaky, and the

8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 05:24

    You should use ssh keepalives... the problem you have is that the remote shell has no way of knowing (by default) that your ssh session was killed. Keepalives will enable the remote shell to detect that you killed the session

    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect('someserver', username='victorhooi', password='blahblah')
    transport = client.get_transport()
    transport.set_keepalive(1)   # <------------------------------
    # ... carry on as usual...
    

    Set the keepalive value as low as you like (even 1 second)... after several seconds, the remote shell will see that the ssh login died, and it will terminate any processes that were spawned by it.

提交回复
热议问题