我在python脚本中使用plink.exe向路由器发送命令。我在"output“中得到结果。一条命令会触发路由器自己运行一系列命令。有没有办法连续获得输出,直到所有命令都完成。我只将前几行返回到输出中。
comm = "plink.exe -pw admin admin@172.16.0.1 COMMAND"
b = sub.Popen(comm,stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = b.communicate()
print output我希望这对某些人有任何意义。=)
发布于 2016-05-27 20:38:53
我找到了我的问题的答案。Paramiko模块为我工作。=)
import sys
import time
import paramiko
host = '192.168.1.1'
user = 'admin'
pwd = 'admin'
i = 1
#def interactive_shell():
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
transport = paramiko.Transport((host,22))
transport.connect(username=user, password=pwd)
time.sleep(0.2)
#chan = paramiko.transport.open_session()
chan = transport.open_session()
chan.setblocking(0)
chan.invoke_shell()
chan.send("command") # Send command to device
chan.send("\n") # Send Enter
time.sleep(1) #optional
while not chan.exit_status_ready():
time.sleep(0.1)
if chan.recv_ready() :
output = chan.recv(8192)
if len(output) > 0 :
outputLines = output.splitlines(True)
sys.stdout.write(output)
if "unit" in output:
sys.stdout.flush()
break
if chan.recv_stderr_ready() :
mystderr = chan.recv_stderr(8192)
if len(mystderr) > 0 :
print mystderr, ","
transport.close()https://stackoverflow.com/questions/35380026
复制相似问题