Paramiko 和伪 tty 分配
我正在尝试使用 Paramiko 连接到远程主机并执行许多文本文件替换。
i, o, e = client.exec_command("perl -p -i -e 's/" + initial + "/"
+ replaced + "/g'" + conf);
其中一些命令需要作为 sudo 运行,这会导致:
sudo:抱歉,您必须有一个 tty 才能 运行须藤
我可以使用 -t 开关和 ssh 强制进行伪 tty 分配。
使用 paramiko 可以做同样的事情吗?
I'm trying to use Paramiko to connect to a remote host and execute a number of text file substitutions.
i, o, e = client.exec_command("perl -p -i -e 's/" + initial + "/"
+ replaced + "/g'" + conf);
Some of these commands need to be run as sudo, which results in:
sudo: sorry, you must have a tty to
run sudo
I can force pseudo-tty allocation with the -t switch and ssh.
Is it possible to do the same thing using paramiko?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
其实很简单。只是:
Actually it's quite simple. Just:
以下代码对我有用:
这只是通过在线查看一些示例而组装而成的......不确定它是否是“正确”的方式。
The following code works for me:
This was just assembled from looking at a few examples online... not sure if its the "right" way.
我认为您需要
SSHClient
对象的invoke_shell
方法(我很乐意提供一个 URL,但 paramiko 文档位于 lag.net 是框架重的,只是不会向我显示文档中给定位置的特定 URL) - 它为您提供了Channel
,您可以在其上执行exec_command
等操作,但通过伪终端(包含终端类型以及行数和列数!-)来执行此操作,这似乎成为你所要求的。I think you want the
invoke_shell
method of theSSHClient
object (I'd love to give a URL but the paramiko docs at lag.net are frame-heavy and just won't show me a specific URL for a given spot in the docs) -- it gives you aChannel
, on which you can doexec_command
and the like, but does that through a pseudo-terminal (complete with terminal type and numbers of rows and columns!-) which seems to be what you're asking for.根据 sudo 手册页:
您可以写入标准输入,因为它是一个带有 write() 的文件对象:
According to the sudo manpage:
You can write to the stdin because it is a file object with write():