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".
If you read the error message
then you can easily find the solution: add the
-t
option to your ssh command:This has been discussed before:
Regarding Paramiko, there have been related questions, with a couple of different approaches:
get_pty
method of the sshChannel
to obtain a pseudo-terminal (which is analogous to tellingssh
to do this)-S
option ofsudo
, and send the password on your standard output.For discussion, see the suggested answers here: