2016-04-28 65 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:谁知道。你发现它是非确定性的。也许机器在某个特定时刻或多或少处于忙碌状态。 –