如何与 Paramiko 的互动 shell 会话互动?

how to interact with Paramiko's interactive shell session?

我有一些 Paramiko 代码,我在其中使用 invoke_shell 方法在远程服务器上请求交互式 ssh shell 会话。此处概述了方法:invoke_shell()

以下是相关代码的摘要:

sshClient = paramiko.SSHClient()
sshClient.connect('127.0.0.1', username='matt', password='password')
channel = sshClient.get_transport().open_session()
channel.get_pty()
channel.invoke_shell()

while True:
    command = raw_input('$ ')
    if command == 'exit':
        break

    channel.send(command + "\n")

    while True:
        if channel.recv_ready():
            output = channel.recv(1024)
            print output
        else:
            time.sleep(0.5)
            if not(channel.recv_ready()):
                break

sshClient.close()

我的问题是:是否有更好的方式与 shell 互动?上面的工作,但它有两个提示(matt@kali:~$ 和 raw_input 中的 $)很难看,如测试 运行 和交互式 shell 的屏幕截图所示].我想我需要帮助写入 shell 的标准输入?抱歉,我代码不多。提前致谢!

我导入了在 Paramiko 的 GitHub 上找到的文件 interactive.py。导入后,我只需要将代码更改为:

try:
    import interactive
except ImportError:
    from . import interactive

...
...

channel.invoke_shell()
interactive.interactive_shell(channel)
sshClient.close()

您可以尝试在调用远程 shell:

后禁用 echo
channel.invoke_shell()
channel.send("stty -echo\n")

while True:
    command = raw_input() # no need for `$ ' anymore
    ... ...