简体   繁体   English

Paramiko跳过频道recv上的一些数据

[英]Paramiko skipping some data on channel recv

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. 使用putty SSH控制台时,可以在控制台上看到不断更新的数据。

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. 当我尝试使用使用recv函数读取的Paramiko通道读取输出数据时。 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. 在检查recv_ready状态之后,我将执行recv操作。

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

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)

Would you, by any chance, by only getting the first 2,048 bytes? 您是否有机会仅获得前2,048个字节?

Channel.recv() takes the number of bytes you want to read as a parameter. Channel.recv()将要读取的字节数作为参数。 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. 例如, channel.recv(4000)将输出前4,000个字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM