Paramiko skipping some data on channel recv

Deadly 提交于 2019-12-13 18:23:59

问题


I try to execute a command which sniffs the serial port and prints on the stdout. The command runs continuously, it doesn't exit or stop. When I use the putty SSH console, I can see the data constantly updated on the console.

I'm able to send the command and start the trace. When I try to read output data using the Paramiko channel read using the recv function. I'm observing that it doesn't capture all the data put out by the sniffer.

I perform the recv operation after checking the recv_ready status.

The below is the code. How can I avoid missing the data?

ssh= paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.connect('host.example.com')
channel = ssh.get_transport().open_session()
channel.get_pty()

channel.exec_command("sniff /dev/stty2")

while(True):
    if(channel.recv_ready): # Doesnt get triggered often
        print channel.recv(2048) # Reads only a part of the data

回答1:


i have the same problem here, and i found a solution,
maybe not a great one, but it works for me
here's piece of code, hope this help :)

while True:
    if channel.recv_ready():
        break
    time.sleep(2)
channel.send('exit\n')

stdout_data = []
try:
    part = channel.recv(4096)
    while part:
        stdout_data.append(part)
        part = channel.recv(nbytes)
except:
    raise

print 'exit status: ', channel.recv_exit_status()
print ''.join(stdout_data)



回答2:


Would you, by any chance, by only getting the first 2,048 bytes?

Channel.recv() takes the number of bytes you want to read as a parameter. If you want to read more bytes then you need to increase this number. For instance, channel.recv(4000) would print the first 4,000 bytes.



来源:https://stackoverflow.com/questions/16871668/paramiko-skipping-some-data-on-channel-recv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!