Python Paramiko shell 输出 return 超级慢

Python Paramiko shell is super slow to return output

我正在使用以下代码通过 Paramiko

通过 ssh 运行 命令

出于某种原因,一些命令会永远 return 输出(例如,一条命令需要整整 10 分钟才能 return 输出,而只需要 2 分钟即可 运行 在服务器上)

import paramiko
import re

class ShellHandler:

def __init__(self, host, user, psw):
    self.ssh = paramiko.SSHClient()
    self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.ssh.connect(host, username=user, password=psw, port=22)

    channel = self.ssh.invoke_shell()
    self.stdin = channel.makefile('wb')
    self.stdout = channel.makefile('r')
    self.shell = channel



def __del__(self):
    self.ssh.close()

def ftp(self):
    ftp_client = self.ssh.open_sftp()
    return ftp_client

def execute(self, cmd):
    """

    :param cmd: the command to be executed on the remote computer
    :examples:  execute('ls')
                execute('finger')
                execute('cd folder_name')
    """
    cmd = cmd.strip('\n')
    self.stdin.write(cmd + '\n')
    finish = 'end of stdOUT buffer. finished with exit status'
    echo_cmd = 'echo {} $?'.format(finish)
    self.stdin.write(echo_cmd + '\n')
    shin = self.stdin
    self.stdin.flush()

    shout = []
    sherr = []
    exit_status = 0
    for line in self.stdout:
        if str(line).startswith(cmd) or str(line).startswith(echo_cmd):
            # up for now filled with shell junk from stdin
            shout = []
        elif str(line).startswith(finish):
            # our finish command ends with the exit status
            exit_status = int(str(line).rsplit(maxsplit=1)[1])
            if exit_status:
                # stderr is combined with stdout.
                # thus, swap sherr with shout in a case of failure.
                sherr = shout
                shout = []
            break
        else:
            # get rid of 'coloring and formatting' special characters
            shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).
                         replace('\b', '').replace('\r', ''))

    # first and last lines of shout/sherr contain a prompt
    if shout and echo_cmd in shout[-1]:
        shout.pop()
    if shout and cmd in shout[0]:
        shout.pop(0)
    if sherr and echo_cmd in sherr[-1]:
        sherr.pop()
    if sherr and cmd in sherr[0]:
        sherr.pop(0)

    return shin, shout, sherr



server = ShellHandler(host_name, "admin", "admin")
print("Connected")
cmd = server.execute("pwd")

def parse(command):
if not command[1]:
    pass
else:
    for line in command[1]:
        line = line.rstrip()
        with open("results.txt", "a") as myfile:
            myfile.write("\n" + line)
if not command[2]:
    pass
else:
    for line in command[2]:
        line = line.rstrip()
        with open("results.txt", "a") as myfile:
            myfile.write("\n" + line)

parse(cmd)

如果你能指导我正确的方向,我将不胜感激!

这是一个快速修复。

对于 return stdout 永远需要的命令,将 echo FINISHED 添加到命令的末尾

例如:

cmd = server.execute("command; echo FINISHED")

然后在execute方法中的迭代开始添加如下条件if str(line).startswith("FINISHED"): break

这里是完整的代码

import paramiko
import re


class ShellHandler:

def __init__(self, host, user, psw):
    self.ssh = paramiko.SSHClient()
    self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    self.ssh.connect(host, username=user, password=psw, port=22)

    channel = self.ssh.invoke_shell()
    self.stdin = channel.makefile('wb')
    self.stdout = channel.makefile('r')
    self.shell = channel



def __del__(self):
    self.ssh.close()

def ftp(self):
    ftp_client = self.ssh.open_sftp()
    return ftp_client

def execute(self, cmd):
    """

    :param cmd: the command to be executed on the remote computer
    :examples:  execute('ls')
                execute('finger')
                execute('cd folder_name')
    """
    cmd = cmd.strip('\n')
    # self.stdin.write("sudo su " + '\n')
    # self.stdin.write("Changeme_1")
    self.stdin.write(cmd + '\n')
    finish = 'end of stdOUT buffer. finished with exit status'
    echo_cmd = 'echo {} $?'.format(finish)
    self.stdin.write(echo_cmd + '\n')
    shin = self.stdin
    self.stdin.flush()

    shout = []
    sherr = []
    exit_status = 0
    for line in self.stdout:
        if str(line).startswith("FINISHED"):
            break
        if str(line).startswith(cmd) or str(line).startswith(echo_cmd):
            # up for now filled with shell junk from stdin
            shout = []
        elif str(line).startswith(finish):
            # our finish command ends with the exit status
            exit_status = int(str(line).rsplit(maxsplit=1)[1])
            if exit_status:
                # stderr is combined with stdout.
                # thus, swap sherr with shout in a case of failure.
                sherr = shout
                shout = []
            break
        else:
            # get rid of 'coloring and formatting' special characters
            shout.append(re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]').sub('', line).
                         replace('\b', '').replace('\r', ''))

    # first and last lines of shout/sherr contain a prompt
    if shout and echo_cmd in shout[-1]:
        shout.pop()
    if shout and cmd in shout[0]:
        shout.pop(0)
    if sherr and echo_cmd in sherr[-1]:
        sherr.pop()
    if sherr and cmd in sherr[0]:
        sherr.pop(0)

    return shin, shout, sherr


server = ShellHandler(host, user, psw)
cmd = avamar.execute("command; echo FINISHED")