我使用了python3 ssh,现在我可以登录远程设备,并执行我的远程C程序,但程序打印信息不能在本地pc上实时显示。如果我的C程序使用setbuf(stdout, NULL);来设置非缓冲,我的电脑可以获得实时信息;我想知道如果C程序没有设置非缓冲,我如何在本地电脑上获得远程实时信息。
下面是我的代码:
def get_ssh_log(hostip, login_name, pw, privaete_key, cmd):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(hostname='%s' % hostip, port=22, username='%s' % login_name, password='%s' % pw,key_filename='%s' % privaete_key)
stdin, stdout, stderr = ssh.exec_command("%s" % cmd, bufsize=1)
for line in iter(stdout.readline, ""):
print(line)
ssh.close()发布于 2017-09-05 20:29:25
您可以在expect包的包装器unbuffer下运行任何程序。在您的案例中:
ssh.exec_command("unbuffer %s" % cmd)当然,这需要在目标计算机上安装unbuffer。
参考:https://unix.stackexchange.com/questions/25372/turn-off-buffering-in-pipe
https://stackoverflow.com/questions/46054463
复制相似问题