【问题标题】:Implement an interactive shell over ssh in Python using Paramiko?使用 Paramiko 在 Python 中通过 ssh 实现交互式 shell?
【发布时间】:2023-04-04 01:25:01
【问题描述】:

我想编写一个程序(在 Windows 7 上的 Python 3.x 中),通过 ssh 在远程 shell 上执行多个命令。查看 paramikos 的 exec_command() 函数后,我意识到它不适合我的用例(因为执行命令后通道会关闭),因为命令依赖于环境变量(由先前的命令设置)并且不能连接成一个exec_command() 调用,因为它们将在程序中的不同时间执行。

因此,我想在同一个频道中执行命令。我研究的下一个选项是使用 paramikos 的 invoke_shell() 函数实现交互式 shell:

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

channel = ssh.invoke_shell()

out = channel.recv(9999)

channel.send('cd mivne_final\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

channel.send('cd ..\n')
channel.send('cd or_fail\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

channel.send('cd ..\n')
channel.send('cd simulator\n')
channel.send('ls\n')

while not channel.recv_ready():
    time.sleep(3)

out = channel.recv(9999)
print(out.decode("ascii"))

ssh.close() 

这段代码有一些问题:

  1. 第一个print 并不总是打印ls 输出(有时它只打印在第二个print 上)。
  2. 第一个cdls 命令总是出现在输出中(我通过recv 命令得到它们,作为输出的一部分),而下面的所有cdls 命令是有时打印,有时不打印。
  3. 第二个和第三个cdls 命令(打印时)总是出现在第一个ls 输出之前。

我对这种“不确定性”感到困惑,非常感谢您的帮助。

【问题讨论】:

  • 如果用python标签替换关注者最少的标签,你会得到更多帮助,假设这真的是python代码。祝你好运。
  • 你必须使用paramiko吗?我发现使用fabric 更容易。您只需设置env 变量,例如userpasswordhost_string,然后您可以执行各种操作,例如使用:get 从远程主机下载文件,put 发送文件和run发出命令。您可以像这样链接命令,例如:run('cd .. && cd simulator && ls').
  • @kchomski 不幸的是,fabric 与 python 3.x 不兼容,所以它不是一个选项。无论如何,据我所见,Fabric 只是 paramiko 的包装器,不允许我在同一通道中运行“非链式”命令。我最终希望在 shell 命令之间运行很多逻辑。
  • @misha:对不起,我忽略了你正在使用 Python 3.x
  • 查看netmiko 它专门用于网络设备,但您也可以在Linux 上使用它。它适用于 Python 3,并基于 Paramiko 构建,但会为您处理大量缓冲

标签: python shell ssh paramiko interactive


【解决方案1】:
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')

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

    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

【讨论】:

  • 如何向 execute() 发送多个命令?我试图做一个 for 循环:for command in commands: object.execute(command) 用于命令列表,但它只执行 2 个命令,然后我必须重新启动 shell。
  • 如果我的命令同时生成 std​​out 和 stderr,我希望它们作为单独的文件怎么办?
  • @YaroslavBulatov 我没试过,但我认为你可以声明 self.stderr = channel.makefile_stderr('r'),与声明标准输入和标准输出的方式类似(注意到 makefile_stderr 方法)。然后,假设您可以访问 stderr,因为类文件对象应该与该通道的 stderr 相关联。
  • 您可以通过以下方式避免标准输出清理: - 通过发送 cmd "export PS1="\n"" 删除命令提示符 - 通过发送 cmd "stty -echo" 避免回显标准输入
  • 优雅!,我在cmd.strip('\n') 下方添加了self.stdin.write("sudo su " + '\n') 以将用户更改为root。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-23
相关资源
最近更新 更多