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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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 Paramiko模拟ssh-t选项_Python_Ssh_Paramiko - Fatal编程技术网

Python Paramiko模拟ssh-t选项

Python Paramiko模拟ssh-t选项,python,ssh,paramiko,Python,Ssh,Paramiko,我实例化一个paramiko通道,然后执行一个命令并获得其输出: channel = transport.open_session() channel.exec_command('service myservice restart') stdout = channel.makefile('rb') for line in stdout: print line, 但是,在执行命令(完成)后,输出迭代将被阻止 我使用ssh进行了测试: ssh myhost service myserv

我实例化一个paramiko通道,然后执行一个命令并获得其输出:

channel = transport.open_session()
channel.exec_command('service myservice restart')

stdout = channel.makefile('rb')

for line in stdout:
    print line,
但是,在执行命令(完成)后,输出迭代将被阻止

我使用ssh进行了测试:

ssh myhost service myservice restart     # terminal gets blocked
ssh -t myhost service myservice restart  # OK
所以我想模拟paramiko中的“-t”选项。到目前为止,我试过:

channel = transport.open_session()
channel.get_pty()
channel.invoke_shell()

stdin, stdout = channel.makefile('wb'), channel.makefile('rb')
stdin.write('service myservice restart\n')

for line in stdout:
    print line,
但是现在,stdout没有关闭,for永远不会结束


有什么想法吗?

它看起来像是
invoke_shell()
返回一个
频道
,而
频道
要求您显式关闭它们。我将尝试关闭您正在打开的一些频道,特别是由
invoke_shell()
返回的频道。请查看您试图运行的脚本,看看是否有类似的行

/dev/null 2>&1
我和你有同样的问题——在我的例子中,我试图远程运行bitnami控制脚本。您帖子中的某些内容让我想起了控制脚本中的输出重定向(这些内容以前让我很头疼)


通常情况下,它们要么忽略错误,要么记录在某个特定的地方-我还没有机会尝试,但要么在脚本结束时将它们重新导出,或者如果您不关心响应,甚至可能手动重定向一些创建的数据>&2都会起作用。

显然,invoke\u shell不会返回任何结果。另外,在发送命令后关闭通道(通过写入stdin)不会让命令完成,我也不会通过“don't let the command finish”(不让命令完成)将输出结果发送到stdout(有时我只会收到“Last login:Tue Dec 11 09:16:32 2012 from localhost”)这样的文本,我的意思是“不要等待命令完成”