2016-04-28 68 views
0

帕拉米科在每次運行中給了我不同的輸出,只是好奇爲什麼會這樣。有時,它返回:paramiko輸出不一致?

The programs included with the Debian GNU/Linux system are free software; 
... 

$ 
$ su - 
Password: 

而其他時候它返回:

The programs included with the Debian GNU/Linux system are free software; 
... 

$ $ su - 
Password: 

這是我的代碼:

def execute_shell_command(self, command, timeout=10, wait_for_answer=True): 
    if self.session is None: 
     message = "'{}' could not execute '{}', session is not open".format(self.interface_id, command) 
     self.sys_conf.logger.warning(message) 
     raise SessionIsNotAvailable(message) 

    channel = self.shell 
    channel.set_combine_stderr(True) 
    output_lines = [] 
    try: 
     # Clear the buffer before executing any commands 
     while channel.recv_ready(): 
      output_lines.append(channel.recv(99999).decode("utf-8", "ignore")) 

     # Execute command, wait {timeout} seconds and try to clear the buffer again. 
     channel.send('{}\n'.format(command)) 
     time.sleep(timeout) 
     if wait_for_answer: 
      while channel.recv_ready(): 
       output_lines.append(channel.recv(99999).decode("utf-8", "ignore")) 

    except socket.timeout as e: 
     message = "Timeout reached - failed to execute command '{}'".format(command) 
     self.sys_conf.logger.warning(message) 
     raise ExecutionTimeout(message, e) 

    answer = "".join(output_lines) 
    if wait_for_answer: 
     return answer 

只是覺得奇怪,我,不應該有任何因爲它與相同服務器的命令相同。

回答

0

我認爲這是因爲這個:

channel.set_combine_stderr(True) 

你告訴的paramiko到輸出和錯誤合併成一個流。但如何完成可能取決於時間。

您可以嘗試不合並流,然後看看會發生什麼。

+0

後續問題:什麼影響時間? –

+0

@AlexOsheter:誰知道。你發現它是非確定性的。也許機器在某個特定時刻或多或少處於忙碌狀態。 –