Paramiko 在读取所有输出之前完成过程
Paramiko finish process before reading all output
我正在尝试制作一个实时 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
谢谢。
我无法用你的命令重现问题,但我可以用像 cat some_big_file.txt
这样的命令重现它。
看来你的假设是对的。在您阅读 channel
中的所有内容之前,退出状态可以准备就绪。不清楚您是否真的需要使用 select
。如果不是我会重写循环:
while True:
buf = channel.recv(1024)
if not buf:
break
print buf
这样的循环将在其中有一些数据时继续读取通道。如果你真的想使用 select
你可以把上面的循环放在你的循环之后。它将读取并打印剩余数据。
我正在尝试制作一个实时 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
谢谢。
我无法用你的命令重现问题,但我可以用像 cat some_big_file.txt
这样的命令重现它。
看来你的假设是对的。在您阅读 channel
中的所有内容之前,退出状态可以准备就绪。不清楚您是否真的需要使用 select
。如果不是我会重写循环:
while True:
buf = channel.recv(1024)
if not buf:
break
print buf
这样的循环将在其中有一些数据时继续读取通道。如果你真的想使用 select
你可以把上面的循环放在你的循环之后。它将读取并打印剩余数据。