0

New to Python:

I have below block of code that is supposed to loop on remote hosts.

    import paramiko
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.load_system_host_keys()
    ssh.connect(host, 22, username, password, timeout=5)
    stdin, stdout, stderr = ssh.exec_command('sudo hostname')

I get:

sudo: no tty present and no askpass program specified

I tried

stdin, stdout, stderr = ssh.exec_command('sudo -S hostname')

AND

stdin, stdout, stderr = ssh.exec_command('sudo hostname',  get_pty=True)

But the code is getting stock forever (not working).

Any suggestions?

  • Do you have `requiretty` in your /etc/sudoers file? you can turn that off. But you will have to deal with the password. I assume you can't login as root? – tdelaney Apr 15 '18 at 00:41
  • requiretty is not in /etc/sudoers on the machine where i'm running the script from but I am not sure about the remote targets – user1248238 Apr 15 '18 at 01:11

1 Answers1

2

You should to send your password for sudo to the stdin:

import paramiko

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.load_system_host_keys()
ssh.connect(host, 22, username, password, timeout=5)

stdin, stdout, stderr = ssh.exec_command('sudo hostname', get_pty=True)
stdin.write('password\n')  # Password for sudo
stdin.flush()
microcoder
  • 141
  • 7