Python paramiko script, problems reading output during exec_command()

前端 未结 4 1911
南方客
南方客 2021-01-02 04:03

Background: I am using python and paramiko to automate the process I go through everytime I have to hand in a program for a class. We use a command called

相关标签:
4条回答
  • 2021-01-02 04:41

    To workaround this and get the output of Ifconfig, you may wanna try to use the option get_pty=True

    e.g:

    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    
    def MonitorProcess():
        ssh.connect('10.2.0.230', username='ibmsys1', password='passw0rd', timeout=5)
        stdin, stdout, stderr = ssh.exec_command('/sbin/ifconfig', timeout=3, get_pty=True)
        #stdin, stdout, stderr = ssh.exec_command('/sbin/ifconfig') -> simple exec ex.
        print stdout.read()
    
    MonitorProcess()
    
    0 讨论(0)
  • 2021-01-02 04:43

    This is an old thread but I have seen this issue as well. This is due to a bug in Paramiko which it cannot process large STDOUT from the remote command so it hangs.

    Ref: https://github.com/paramiko/paramiko/issues/515

    Possible Solution: https://gist.github.com/matjohn2/2c1c4988e17112a34f310667e0ff6e7e

    0 讨论(0)
  • 2021-01-02 04:59

    I don't see anything wrong with the code snippit above. The program below is a complete script which logs into a host and runs the ls command to view files. I just tried it and it works for me. Perhaps try this and see if it works for you. If it does not work, I suspect some issue specific to either your ssh server, command you are running, or paramiko installation. If it does work for you, its just a matter of making changes to this to move towards your existing functionality and see where it breaks.

    import paramiko
    ssh=paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('<ip address here>',username='<username here>',password='<password here>')
    stdin,stdout,stderr = ssh.exec_command("ls /")
    print stdout.readlines()
    

    If that works for you my next suggestion would be to try replacing the 'ls /' with the actual handin command you are trying to run. Its possible that command is hanging waiting for user input, etc.

    0 讨论(0)
  • 2021-01-02 05:05

    The problem might be that the remote command is waiting for input (it expects you to write something to stdin, not knowing that you aren't going to unless you tell so). Try stdin.channel.shutdown_write() (I believe stdin.close() by itself won't do the trick: that will only cause a flush)

    0 讨论(0)
提交回复
热议问题