Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to use paramiko to ssh into a bunch a remote nodes and run some command line with root priviledge

I have ssh key in my home directory and so i don't need to input password when I ssh into those remote nodes

but when running the following script:

    def connect(hostname):
                    ssh = paramiko.SSHClient()
                    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())               
                    ssh.connect(hostname, username='niky', pkey=paramiko.RSAKey.from_private_key(open('id_rsa'), 'passwd'), timeout = 240.0)                return ssh          



    def run(hostname):
            ssh = connect(hostname)
            (stdin, stdout, stderr) = ssh.exec_command("sudo ls")
            res = stderr.readlines()
            print hostname+': '+''.join(str(elem) for elem in res)+'\n'

    run(remote.nity.com)

I got the following error:

remote.nity.com: sudo: no tty present and no askpass program specified

if I don't add sudo before ls everything works fine what are potential reasons ? thanks!

share|improve this question
    
the user who runs the sudo must not have been allowed to run the command(s) you are running, with NOPASSWORD directive in sudoers file –  MelBurslan Apr 10 '13 at 4:34

2 Answers 2

In the stock sudoers configuration, the following line is usually present:

Defaults requiretty

This is both secure and what you need in the majority of the use cases.

In your case, you need to override this default for a specific user, so you would write below:

Defaults:niky !requiretty

Also, you need to define a line allowing niky to call sudo without password:

niky remote.nity.com = (root)NOPASSWD: /bin/ls

This line means that the user niky is allowed to execute /bin/ls as root in remote.nity.com without requiring a password.

Further reference can be found in here.

share|improve this answer

FYI - may help you in your search - note you get the SAME error in plain SSH if you encapsulate one SSH command inside another, ex:

localhost$ ssh user1@host1.example.com -C 'sudo su - anotheruser ssh user2@host2.example2.com /run/this/executable'

(Why not SSH directly to the target box? Well, maybe SSH keys are only setup between host1 and host2, or the network is routed to prohibit host2 access w/o travelling through host1. Maybe you're not allowed to touch sudoers or any other file on host2... common in Production envs.).

In any case, you are able to run non-sudo commands using the above, but prefixing with sudo causes "no tty present".

How do you fix this with bare ssh command? Pass in the -t, ex: localhost$ ssh user1@host1.example.com -C 'sudo su - anotheruser ssh -t user2@host2.example2.com /run/this/executable'

Now the remote sudo runs fine inside SSH, no complaints about tty. The -t allocated it.

So the question is, how does one emulate the "-t" option inside the Paramiko object? There's your answer.

This blog attempts to explain, but perhaps could have spent more time on the 'sudo' example: http://jessenoller.com/2009/02/05/ssh-programming-with-paramiko-completely-different/

(Apologies for my point in the right direction instead of a perfect answer... I'm still searching for that info myself actually).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.