尝试编辑文件时出现 Paramiko 错误:"sudo: no tty present and no askpass program specified"
Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"
我正在使用 Paramiko 进行 SSH 并编辑配置文件。该文件本身需要 sudo 权限才能编辑。到目前为止,这还不是问题,因为我刚刚为我的脚本中的其他 sudo 命令完成了 echo <sudopw> | sudo <command>
。
但是,当我尝试使用 sed 编辑此文件时,没有任何反应。 stderr
产生: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
的解决方案,但似乎没有任何效果。任何编辑此文件的解决方案都会有所帮助。
编辑:这已经解决了!不要使用 get_pty
。在 "sudo".
之后使用 sudo 的 -S 选项
如果您阅读错误信息
sudo: no tty present and no askpass program specified
那么您可以轻松找到解决方案:将 -t
选项添加到您的 ssh 命令中:
-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.
之前已经讨论过:
- 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,有一些相关的问题,有几种不同的方法:
- 使用ssh的
get_pty
方法Channel
获取伪终端(类似于告诉ssh
这样做)
- 使用
sudo
的 -S
选项,并在标准输出上发送密码。
有关讨论,请在此处查看建议的答案:
- Paramiko and Pseudo-tty Allocation
- Nested SSH session with Paramiko
我正在使用 Paramiko 进行 SSH 并编辑配置文件。该文件本身需要 sudo 权限才能编辑。到目前为止,这还不是问题,因为我刚刚为我的脚本中的其他 sudo 命令完成了 echo <sudopw> | sudo <command>
。
但是,当我尝试使用 sed 编辑此文件时,没有任何反应。 stderr
产生: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
的解决方案,但似乎没有任何效果。任何编辑此文件的解决方案都会有所帮助。
编辑:这已经解决了!不要使用 get_pty
。在 "sudo".
如果您阅读错误信息
sudo: no tty present and no askpass program specified
那么您可以轻松找到解决方案:将 -t
选项添加到您的 ssh 命令中:
-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.
之前已经讨论过:
- 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,有一些相关的问题,有几种不同的方法:
- 使用ssh的
get_pty
方法Channel
获取伪终端(类似于告诉ssh
这样做) - 使用
sudo
的-S
选项,并在标准输出上发送密码。
有关讨论,请在此处查看建议的答案:
- Paramiko and Pseudo-tty Allocation
- Nested SSH session with Paramiko