Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
579 views
in Technique[技术] by (71.8m points)

python - Paramiko error when trying to edit file: "sudo: no tty present and no askpass program specified"

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.

When I try to edit this file using sed, though, nothing happens. stderr produces: sudo: no tty present and no askpass program specified

Here is my code:

stdin, stdout, stderr = client.exec_command
('echo <sudopassword> | sudo sed -i -e "\$aAllowUsers" /etc/ssh/sshd_config)')

I have tried solutions using invoke_shell but nothing seems to be working. Any solution to edit this file would be helpful.

EDIT: This has been solved! Don't use get_pty. Use the -S option of sudo right after "sudo".

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you read the error message

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
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:

Regarding Paramiko, there have been related questions, with a couple of different approaches:

  • use the get_pty method of the ssh Channel to obtain a pseudo-terminal (which is analogous to telling ssh to do this)
  • use the -S option of sudo, and send the password on your standard output.

For discussion, see the suggested answers here:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...