2016-04-28 9 views
0

Paramikoは、それぞれの実行で異なる出力を与えています。時にはそれは、これを返します。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) 

あなたが1つのストリームにstdoutとstderrを組み合わせることParamikoを語っています。しかし、それがどのように行われるかは、タイミングに左右されることがあります。

ストリームを結合しないで、何が起こっているのかを確認してください。

+0

フォローアップの質問:タイミングには何が影響しますか? –

+0

@AlexOsheter:誰が知っている。それはあなたが発見したように非決定論的です。たぶん、ある特定の瞬間にマシンが多かれ少なかれ忙しいかもしれません。 –

関連する問題