2016-02-08 49 views
3

我使用下面的代碼:的paramiko Python模塊掛在stdout.read()

import paramiko 

def runSshCmd(hostname, username, password, cmd, timeout=None):   
    client = paramiko.SSHClient() 
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
    client.connect(hostname, username=username, password=password, 
      allow_agent=False, look_for_keys=False, timeout=timeout) 
    stdin, stdout, stderr = client.exec_command(cmd) 
    stdin.flush() 
    data = stdout.read() 
    print (data) 
    client.close() 

runSshCmd("10.128.12.32", "root", "C0mput3Gr!d", "ts_menu") 

當談到stdout.read(),它掛起......有時它打印很長一段時間後的輸出。

你可以請建議,如果有什麼可以做這件事?

我看到這個問題已經被報告:

https://bugs.python.org/issue24026

是否有蟒蛇的ssh連接並運行命令沒有更好的模塊?

回答

6

可能與https://github.com/paramiko/paramiko/issues/109

下面是我面臨什麼樣的解釋,以及如何我工作圍繞它。

我也遇到過這個問題,這是由於 stdout.channel.eof_received == 0

import paramiko 
client = paramiko.SSHClient() 
client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
client.connect("1.1.1.1", username="root", password="pass") 
stdin, stdout, stderr = client.exec_command("service XXX start") 

標準輸入,輸出和錯誤都保持開放......

>>> print stdin 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 
>>> print stdout 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 
>>> print stderr 
<paramiko.ChannelFile from <paramiko.Channel 3 (open) window=2097152 in-buffer=50 -> <paramiko.Transport at 0x17eff90L (cipher aes128-ctr, 128 bits) (active; 1 open channel(s))>>> 

所以在EOF沒有收到...

>>> print stdin.channel.eof_received 
0 

通常我收到True,並且可以只是stdout.read(),但爲了安全起見我使用此解決方案(工作!): 等待超時,力stdout.channel.close(),然後stdout.read():

>>> timeout = 30 
>>> import time 
>>> endtime = time.time() + timeout 
>>> while not stdout.channel.eof_received: 
...  sleep(1) 
...  if time.time() > endtime: 
...   stdout.channel.close() 
...   break 
>>> stdout.read() 
'Starting XXX: \n[ OK ]\rProgram started . . .\n' 
>>> 

BTW我使用:

Python 2.6.6 
paramiko (1.15.2) 

希望這有助於...

0

它使用的時候沒有數據標準輸出或有沒有EOL線發生(即在sh腳本中的讀取語句中)。嘗試設置'get_pty = True',然後只讀取stdout中的字節。爲了避免無限循環,儘管繼續聲明中設置了超時和睡眠是一個好主意:

stdin, stdout, stderr = ssh.exec_command("your-command",get_pty=True) 
stdout.flush() 
nbytes = 0 
while (len(stdout.channel.in_buffer)==0): 
    continue 

nbytes=len(stdout.channel.in_buffer) 
print(nbytes) 
stdout.read(nbytes)