Paramiko Error When Trying to Edit File: "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

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:

  • How to fix 'sudo: no tty present and no askpass program specified' error?
  • sudoers NOPASSWD: sudo: no tty present and no askpass program specified

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:

  • Paramiko and Pseudo-tty Allocation
  • Nested SSH session with Paramiko

Python + Paramiko + Error: sudo: no tty present and no askpass program specified

I am able to get the solution for this problem.

I have used send(..) instead of execute_command(...).

SETUP:

self.client = paramiko.client.SSHClient()
self.client.set_missing_host_key_policy(paramiko.client.AutoAddPolicy())
self.client.connect(hostname=self.str_host_name, port=22,
username=self.str_user_name, password=self.str_user_pass)
self.transport = paramiko.Transport(self.str_host_name, 22)
self.transport.connect(username=self.str_user_name, password=self.str_user_pass)

EXECUTION:

if self.shell:
self.shell.send(str_sudo_command + "\n")

if self.shell is not None:
time.sleep(2)
self.str_sudo_command_result += str(self.shell.recv(1024).decode('utf-8'))
self.str_sudo_command_result = str(self.str_sudo_command_result).strip()
self.str_sudo_command_result = self.str_sudo_command_result.splitlines()
if len(self.str_sudo_command_result) > 0:
if "[sudo] password for " in self.str_sudo_command_result[-1]:
self.str_sudo_command_result = ""
self.shell.send(self.str_user_pass + "\n")
time.sleep(2)
else:
while True:
result = str(self.str_sudo_command_result)
result = result.splitlines()
time.sleep(2)
if self.str_result_end_line not in result[-1]:
while self.shell.recv_ready():
self.str_sudo_command_result += str(self.shell.recv(9999).decode('utf-8'))
else:
break

Suggestions and corrections are welcomed.

unable to sudo su using paramiko

tried incorporating sudo in all the commands that I need to execute via paramiko, and that seems to be working fine. code that worked fine:

stdin, stdout, stderr = ssh.exec_command('sudo -S chown -R test_user:test_user /tmp/test.txt')

sudoers NOPASSWD: sudo: no tty present and no askpass program specified

sudo permissions are about the user/group you are changing from not the user you are changing to.

So are those permission lines are letting the testuser user and the testgroup group run any command (as anyone) without a password.

You need to give permission to the user running the script to run commands as the testuser user for what you want.

Assuming that's what you meant to allow that is.

How to run sudo with Paramiko? (Python)

check this example out:

ssh.connect('127.0.0.1', username='jesse', 
password='lol')
stdin, stdout, stderr = ssh.exec_command(
"sudo dmesg")
stdin.write('lol\n')
stdin.flush()
data = stdout.read.splitlines()
for line in data:
if line.split(':')[0] == 'AirPort':
print line

Example found here with more explanations:
http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

Hope it helps!

Paramiko and Pseudo-tty Allocation

I think you want the invoke_shell method of the SSHClient object (I'd love to give a URL but the paramiko docs at lag.net are frame-heavy and just won't show me a specific URL for a given spot in the docs) -- it gives you a Channel, on which you can do exec_command and the like, but does that through a pseudo-terminal (complete with terminal type and numbers of rows and columns!-) which seems to be what you're asking for.



Related Topics



Leave a reply



Submit