我正在使用Python 3.5。我需要通过ssh执行命令,如果执行相同的命令,返回的结果通常不是在终端中看到的结果。通常,它似乎会在第一个'\ n'之后缩短它的时间,但有时却可以正常工作。为什么要这样做?
def simple_ssh(zone, cmd):
# get zone ip address
machine = socket.getaddrinfo(zone, 22, socket.AF_INET)
ip_address = machine[0][4][0]
# print(ip_address)
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip_address, port=22, username='xxxxxxxxx', password='xxxxxxxxx', look_for_keys=False)
data = [None, None]
_, stdout, stderr = client.exec_command(cmd, timeout=10)
stdout.channel.recv_exit_status() # wait till finished
data[0] = stdout.read().decode("utf-8")
data[1] = stderr.read().decode("utf-8")
client.close()
return data
print(simple_ssh('fetish', "cd /usr/fl/log/ws-fsdb_vol_fetish-images ; grep 'first pass' console.txt"))
输出:
['2019-07-18 11:22:58 fsdb (0 sec, 10.00 ms, 139696900073312): main: first pass: started\n', '']
在终端中运行相同的代码
['2019-07-18 11:22:58 fsdb (0 sec, 10.00 ms, 139696900073312): main: first pass: started\n2019-07-18 11:24:11 fsdb (73 sec, 14050.00 ms, 139696900073312): main: first pass status:complete time:73\n', '']