尝试编辑文件时出现paramiko错误:"sudo:不存在tty且未指定askpass程序" [英] Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"
问题描述
我正在使用Paramiko进行SSH并编辑配置文件.文件本身需要sudo权限才能进行编辑.到目前为止,这还不是问题,因为我已经为脚本中的其他sudo命令完成了echo <sudopw> | sudo <command>
.
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
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.
这已经在前面讨论过了:
This has been discussed before:
- How to fix 'sudo: no tty present and no askpass program specified' error?
- sudoers NOPASSWD: sudo: no tty present and no askpass program specified
关于帕拉米科(Paramiko),存在一些相关的问题,有几种不同的解决方法:
Regarding Paramiko, there have been related questions, with a couple of different approaches:
- 使用
get_pty
Channel的a>获取伪终端的方法(类似于告诉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 and Pseudo-tty Allocation
- Nested SSH session with Paramiko
这篇关于尝试编辑文件时出现paramiko错误:"sudo:不存在tty且未指定askpass程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!