在 Paramiko 中运行交互式命令
我正在尝试通过 paramiko 运行交互式命令。 cmd 执行尝试提示输入密码,但我不知道如何通过 paramiko 的 exec_command 提供密码,并且执行挂起。 如果 cmd 执行需要交互输入,是否有办法将值发送到终端?
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("psql -U factory -d factory -f /tmp/data.sql")
有谁知道如何解决这个问题? 谢谢。
I'm trying to run an interactive command through paramiko. The cmd execution tries to prompt for a password but I do not know how to supply the password through paramiko's exec_command and the execution hangs. Is there a way to send values to the terminal if a cmd execution expects input interactively?
ssh = paramiko.SSHClient()
ssh.connect(server, username=username, password=password)
ssh_stdin, ssh_stdout, ssh_stderr = ssh.exec_command("psql -U factory -d factory -f /tmp/data.sql")
Does anyone know how this can addressed? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
完整的 paramiko 发行版附带了许多优秀的演示。
在 demos 子目录中,
demo.py
和interactive.py
具有完整的交互式 TTY 示例,这对于您的情况来说可能有点过头了。在上面的示例中,
ssh_stdin
的作用类似于标准 Python 文件对象,因此只要通道仍然打开,ssh_stdin.write
就应该可以工作。我从来不需要写入标准输入,但文档建议命令退出后通道就会关闭,因此使用标准
stdin.write
方法发送密码可能不会工作。 通道本身有较低级别的 paramiko 命令,可以为您提供更多控制 - 请参阅SSHClient.exec_command
方法已针对所有详细细节实现。The full paramiko distribution ships with a lot of good demos.
In the demos subdirectory,
demo.py
andinteractive.py
have full interactive TTY examples which would probably be overkill for your situation.In your example above
ssh_stdin
acts like a standard Python file object, sossh_stdin.write
should work so long as the channel is still open.I've never needed to write to stdin, but the docs suggest that a channel is closed as soon as a command exits, so using the standard
stdin.write
method to send a password up probably won't work. There are lower level paramiko commands on the channel itself that give you more control - see how theSSHClient.exec_command
method is implemented for all the gory details.我在尝试使用 Paramiko 的一个分支 ssh 进行交互式 ssh 会话时遇到了同样的问题。
我四处寻找并发现这篇文章:
更新的链接(链接生成 404 之前的最后版本):http://web.archive.org/web/20170912043432/http://jessenoller .com/2009/02/05/ssh-programming-with-paramiko-completely- Different/
要继续您的示例,您可以执行
本文更深入的内容,描述围绕 exec_command 的完全交互式 shell。 我发现这比源代码中的示例更容易使用。
原始链接:http ://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely- Different/
I had the same problem trying to make an interactive ssh session using ssh, a fork of Paramiko.
I dug around and found this article:
Updated link (last version before the link generated a 404): http://web.archive.org/web/20170912043432/http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/
To continue your example you could do
The article goes more in depth, describing a fully interactive shell around exec_command. I found this a lot easier to use than the examples in the source.
Original link: http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/
为什么如果直接使用而不检查 stdout.channel.recv_ready(): stdout.read() 会挂起,而 stdout.channel.exit_status_ready():
对于我的情况,在远程服务器上运行命令后,会话正在等待用户输入,输入“q”后,将关闭连接。
但在输入 'q' 之前,stdout.read() 将等待 EOF,如果缓冲区较大,似乎此方法不起作用。
我在 while 中尝试了 stdout.readline() ,它也有效。
stdin, stdout, stderr = ssh.exec_command('/Users/lteue/Downloads/uecontrol')
stdout.read() 将挂起
Why the stdout.read() will hang if use dierectly without checking stdout.channel.recv_ready(): in while stdout.channel.exit_status_ready():
For my case ,after run command on remote server , the session is waiting for user input , after input 'q' ,it will close the connection .
But before inputting 'q' , the stdout.read() will waiting for EOF,seems this methord does not works if buffer is larger .
I tried stdout.readline() in while , it works also.
stdin, stdout, stderr = ssh.exec_command('/Users/lteue/Downloads/uecontrol')
stdout.read() will hang
您需要 Pexpect 才能两全其美(expect 和 ssh 包装器)。
You need Pexpect to get the best of both worlds (expect and ssh wrappers).
我不熟悉 paramiko,但这可能有效:
有关标准输入的信息:
http://docs.python.org/library/sys.html?highlight=stdin#sys.stdin
I'm not familiar with paramiko, but this may work:
For information on stdin:
http://docs.python.org/library/sys.html?highlight=stdin#sys.stdin
看一下示例并以类似的方式执行
(来自 http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely- Different/):
Take a look at example and do in similar way
(sorce from http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/):
您可以使用此方法发送您想要的任何确认消息,例如“确定”或密码。 这是我的解决方案并举了一个例子:
You can use this method to send whatever confirmation message you want like "OK" or the password. This is my solution with an example: