Long-running ssh commands in python paramiko module (and how to end them)

后端 未结 6 1763
野性不改
野性不改 2020-11-30 02:39

I want to run a tail -f logfile command on a remote machine using python\'s paramiko module. I\'ve been attempting it so far in the following fashion:

6条回答
  •  一生所求
    2020-11-30 03:21

    Just a small update to the solution by Andrew Aylett. The following code actually breaks the loop and quits when the external process finishes:

    import paramiko
    import select
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect('host.example.com')
    channel = client.get_transport().open_session()
    channel.exec_command("tail -f /var/log/everything/current")
    while True:
        if channel.exit_status_ready():
            break
        rl, wl, xl = select.select([channel], [], [], 0.0)
        if len(rl) > 0:
            print channel.recv(1024)
    

提交回复
热议问题