Pythonでリモートサーバ上でコマンドを実行し、stdoutをローカルコマンドにパイプする方法はありますか? Pythonでssh host 'echo test' | cat
を行うために、私はParamikoのstdoutをサブプロセスのstdinとして使用する
import paramiko
import subprocess
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('host', username='user')
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command('echo test')
proc = subprocess.Popen(['cat'], stdin=ssh_stdout)
outs, errs = proc.communicate()
print(outs)
を試してみましたが、私は例外'ChannelFile' object has no attribute 'fileno'
を取得します。 Paramikoのssh_stdout
は、subprocess.Popenでstdinとして使用できないようです。
'proc.poll():'は本当に必要ではありませんが、_wrong_です。正常終了すると、0を返します。プログラム終了を積極的に待つためには、proc.poll()はNone:ではありません。 –