通过Paramiko执行多个Linux命令,但无法获取输出

时间:2019-06-17 11:26:45

标签: python python-3.x paramiko

我正在尝试连接到远程服务器并执行一系列Linux命令。我可以使用以下代码实现此目标,但是无法保存这些Linux命令的输出

print(stdout.read())没有显示任何内容。

import sys
import paramiko as pm
sys.stderr = sys.__stderr__
import os
class AllowAllKeys(pm.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

HOST = ''
USER = ''
PASSWORD = ''
client = pm.SSHClient()
client.set_missing_host_key_policy(pm.AutoAddPolicy())
client.connect(HOST, username=USER, password=PASSWORD)

channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')

try:
    stdin.write('''
    cd /tmp
    mkdir aveksatest
    cd aveksatest
    mkdir packagestest
    mkdir stagingtest
    mv /packagesfolder /tmp/aveksatest
    ''')
    print("Successfully created packages & staging directories")
except:
    print("Error!!")
try:
    stdin.write('''
    cd /tmp/aveksa/packages
    tar -jtvf asmlib-008_x64.tar.bz2
    tar -jtvf aveksa-7.1.0.tar.bz2
    tar -jtvf AveksaAgent-7.1.0.tar.bz2
    tar -jtvf openjdk18_v002.tar.bz2
    ''')
    print("Successfully made test2")
    print (stdout.read())
except:
    print("Error while test2!!")        

stdout.close()
stdin.close()
client.close()

预期的输出为:

-rw-r--r-- oracle/oinstall 136337 2014-04-15 00:13 oracleasm-2.6.18-128.1.10.el5                                 -2.0.5-1.el5.x86_64.rpm
-rw-r--r-- oracle/oinstall 136083 2014-04-15 00:13 oracleasm-2.6.18-128.1.14.el5                                 -2.0.5-1.el5.x86_64.rpm

但是实际输出是:

Successfully created packages & staging directories
Successfully made test2

0 个答案:

没有答案