我有一些 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交互的方法?上述方法可行,但在交互式shell中会出现两个提示符(matt@kali:~$和来自raw_input的$),如测试运行的屏幕截图所示,这很丑陋。我想知道如何写入shell的stdin?抱歉,我不怎么编程。提前感谢!
![enter image description here](https://istack.dev59.com/5gQtg.webp)