![](/img/trans.png)
[英]How does sympy work? How does it interact with the interactive Python shell, and how does the interactive Python shell work?
[英]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 的标准输入? 抱歉,我代码不多。 提前致谢!
我导入了一个文件, interactive.py ,在 Paramiko 的 GitHub 上找到。 导入后,我只需要将代码更改为:
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
... ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.