Paramiko channel stucks when reading large ouput

前端 未结 6 858
星月不相逢
星月不相逢 2020-12-09 05:41

I have a code where i am executing a command on remote Linux machine and reading the output using Paramiko. The code def looks like this:

ssh = paramiko.SSHC         


        
6条回答
  •  有刺的猬
    2020-12-09 06:34

    I am posting the final code which worked with inputs from Bruce Wayne( :) )

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect(IPAddress, username=user['username'], password=user['password'])
    
    chan = self.ssh.get_transport().open_session()
    chan.settimeout(10800)
    
    try:
        # Execute the given command
        chan.exec_command(cmd)
    
        # To capture Data. Need to read the entire buffer to capture output
        contents = StringIO.StringIO()
        error = StringIO.StringIO()
    
        while not chan.exit_status_ready():
            if chan.recv_ready():
                data = chan.recv(1024)
                #print "Indside stdout"
                while data:
                    contents.write(data)
                    data = chan.recv(1024)
    
            if chan.recv_stderr_ready():            
                error_buff = chan.recv_stderr(1024)
                while error_buff:
                    error.write(error_buff)
                    error_buff = chan.recv_stderr(1024)
    
        exit_status = chan.recv_exit_status()
    
    except socket.timeout:
        raise socket.timeout
    
    output = contents.getvalue()
    error_value = error.getvalue()
    
    return output, error_value, exit_status
    

提交回复
热议问题