【发布时间】:2016-05-05 00:07:43
【问题描述】:
我正在尝试创建一个实时 SSH 库,但由于经常遇到问题,我从Long-running ssh commands in python paramiko module (and how to end them) 获取了此代码。 但是这段代码不会打印整个输出。
我猜当 while 循环在 channel.exit_status_ready() 上退出时,通道仍然有数据要读取。我一直在尝试解决此问题,但并未针对所有输入进行修复。
我怎样才能使这项工作打印所有类型的命令?
import paramiko
import select
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect('host.example.com')
channel = client.get_transport().open_session()
channel.exec_command("cd / && ./test.sh")
while True:
if channel.exit_status_ready():
break
rl, wl, xl = select.select([channel], [], [], 0.0)
if len(rl) > 0:
print channel.recv(1024)
test.sh:
echo 1
wait 1
echo 2
wait 1
echo 3
输出:
1
2
Process finished with exit code 0
谢谢。
【问题讨论】: