尝试编辑文件时出现 Paramiko 错误:“sudo:不存在 tty 且未指定 askpass 程序" [英] Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"
问题描述
我正在使用 Paramiko 进行 SSH 并编辑配置文件.文件本身需要 sudo 权限才能编辑.到目前为止,这还不是问题,因为我刚刚完成了 echo <sudopw>|sudo
用于我脚本中的其他 sudo 命令.
I am using Paramiko to SSH and edit a config file. The file itself needs sudo permissions to edit. This hasn't been a problem so far, as I've just done echo <sudopw> | sudo <command>
for other sudo commands in my script.
但是,当我尝试使用 sed 编辑此文件时,没有任何反应.stderr
产生:sudo: no tty present and no askpass program specified
When I try to edit this file using sed, though, nothing happens. stderr
produces: sudo: no tty present and no askpass program specified
这是我的代码:
stdin, stdout, stderr = client.exec_command
('echo <sudopassword> | sudo sed -i -e "\$aAllowUsers" /etc/ssh/sshd_config)')
我尝试过使用 invoke_shell
的解决方案,但似乎没有任何效果.任何编辑此文件的解决方案都会有所帮助.
I have tried solutions using invoke_shell
but nothing seems to be working. Any solution to edit this file would be helpful.
这已经解决了!不要使用 get_pty
.在sudo"之后立即使用 sudo 的 -S 选项.
This has been solved! Don't use get_pty
. Use the -S option of sudo right after "sudo".
推荐答案
如果你阅读了错误信息
sudo: no tty present and no askpass program specified
然后您可以轻松找到解决方案:将 -t
选项添加到您的 ssh 命令:
then you can easily find the solution: add the -t
option to your ssh command:
-t
强制伪终端分配.这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时.多个 -t
选项强制 tty 分配,即使 ssh 没有本地 tty.
-t
Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple-t
options force tty allocation, even if ssh has no local tty.
这之前已经讨论过:
关于 Paramiko,有一些相关的问题,有几种不同的方法:
Regarding Paramiko, there have been related questions, with a couple of different approaches:
- 使用
get_pty
sshChannel
获取伪终端的方法(类似于告诉ssh
这样做) - 使用
sudo
的-S
选项,并将密码发送到您的标准输出.
- use the
get_pty
method of the sshChannel
to obtain a pseudo-terminal (which is analogous to tellingssh
to do this) - use the
-S
option ofsudo
, and send the password on your standard output.
有关讨论,请参阅此处的建议答案:
For discussion, see the suggested answers here:
这篇关于尝试编辑文件时出现 Paramiko 错误:“sudo:不存在 tty 且未指定 askpass 程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!