Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 帕拉米科联合收割机_Python_Ssh_Paramiko - Fatal编程技术网

Python 帕拉米科联合收割机

Python 帕拉米科联合收割机,python,ssh,paramiko,Python,Ssh,Paramiko,我试图将stdout和stderr的输出结合起来。我相信这可以通过通道对象的set_combine_stderr()实现 这就是我正在做的: SSH = paramiko.SSHClient() #I connect and everything OK, then: chan = ssh.invoke_shell() chan.set_combine_stderr(True) chan.exec_command('python2.6 subir.py') resultado = chan.mak

我试图将stdout和stderr的输出结合起来。我相信这可以通过通道对象的set_combine_stderr()实现

这就是我正在做的:

SSH = paramiko.SSHClient()
#I connect and everything OK, then:
chan = ssh.invoke_shell()
chan.set_combine_stderr(True)
chan.exec_command('python2.6 subir.py')
resultado = chan.makefile('rb', -1.)
但是,当我尝试存储结果时(上面的最后一行,chan.makefile())出现以下错误:

错误:通道已关闭


任何帮助都将不胜感激

@AaronMcSmooth:我指的是我正在连接的计算机的stdout和stderr(通过SSH)

我最终做了这样的事:

stdin, stdout, stderr = ssh.exec_command(...)

output = stdin.read().strip() + stdout.read().strip()
就我的应用程序而言,区分stdout和stderr并不重要,但我认为这不是将两者结合起来的最佳方式

SSHClient.exec_command()
的代码是(查看paramiko的源代码):


我正在通道上执行相同的操作,但接收到的通道是关闭错误。

虽然
set\u combine\u stderr
确实将
stderr
转移到
stdout
流,但它是以混乱的顺序进行的,因此您不会得到您可能想要的结果,即按写入的顺序组合的行,就像在本地终端窗口中运行命令一样。相反,请使用
get_pty
。这将导致服务器通过一个伪终端运行这些线路,并将它们保持在时间顺序上

这里有一个测试程序,
outerr.py
,它在
stdout
stdin
上交替写入行。假设它位于llmps@meerkat2.

#/usr/bin/env python
导入系统
对于x范围内的x(1101):
(sys.stdout,sys.stderr)[x%2]。写入('这是第%s行,在标准%s上。\n'%
(x,('out','err')[x%2]))
现在,请尝试以下代码以远程运行它:

#/usr/bin/env python
进口帕拉米科
def connect():
ssh=paramiko.SSHClient()
ssh.set_缺少_主机_密钥_策略(paramiko.AutoAddPolicy())
ssh.connect('meerkat2',username='llmps',password=''..')
返回ssh
def运行测试(ssh):
tran=ssh.get_transport()
chan=tran.open_session()
#chan.set\u combine\u stderr(真)
chan.get_pty()
f=chan.makefile()
chan.exec_命令('./outerr.py')
打印f.read(),
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
ssh=connect()
运行测试(ssh)
ssh.close()

如果您运行上面的命令,您应该会看到100行,按照所写的顺序排列。相反,如果您注释掉
chan.get_pty()
调用并取消注释
chan.set_combine\u stderr(True)
调用,您将得到大量的
stdout
stderr
行,从一次运行到另一次运行都是随机分布的。

好的,我知道这是一个很老的主题,但我遇到了同样的问题,我得到了一个(也许不是这样-)很好的解决方案。只需调用远程服务器上的命令,将stderr重定向到stdout,然后始终从stdout读取。例如:

client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='user', password='pass')

stdin,stdout,stderr = client.exec_command('python your_script.py 2> \&1')
print stdout.read()

看起来您正在尝试从
stdin
读取数据?我想你可能是想读《标准规范》。
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('hostname', username='user', password='pass')

stdin,stdout,stderr = client.exec_command('python your_script.py 2> \&1')
print stdout.read()