0
votes

I have a series of bash scripts that rely on sudo to ssh/scp some host with root privileges.

So I made a python script sshOK that uses pexpect to handle all the nitty gritty of sshing a host, such as answering yes to store keys and return true if the host is available for ssh and all is OK.

However, I'm asked for the password every time with sudo inside the python pexpect.spawn, instead of once every five minutes when the sudo is called with inside the bash scripts.

Is there some way to call

child=pexpect.spawn('sudo ssh somehost', Pty=get_parent_pty())

or similar inside my python script sshOK that makes the shell I'm running sshOK in remember that autorization? From what I've RTFM'ed about sudo the pexpect call needs to share pseudo terminal to inherit the credentials or something like that. The desired behavior is:

[foo@bar bin]$ sshOK somehost
[sudo] password for foo:
Try ssh            on somehost   testing connection              [--OK--]
[foo@bar bin]$ sshOK somehost
Try ssh            on somehost   testing connection              [--OK--]
[foo@bar bin]$

and not

[foo@bar bin]$ sshOK somehost
[sudo] password for foo:
Try ssh            on somehost   testing connection              [--OK--]
[foo@bar bin]$ sshOK somehost
[sudo] password for foo:
Try ssh            on somehost   testing connection              [--OK--]
[foo@bar bin]$

The goal is to replace the bash scripts later on with more python and pexpect, but right now the hurdle is to be able to call the same script more than once without having to type the password ad nauseam.

The alternative is to extract the parts that use pexpect to a separate script, add that script to sudoers and let the whole script run elevated. Adding ssh keys for every user is not an option.

1
Changed it for you :) - robertklep
err... the real question is why you need to call sudo when running ssh in the first place? Instead, put the ssh key for the current user into the remote authorized_keys file instead? - Wes Hardaker
You might want to avoid adding more than a handful public keys to authorized_keys on a large number of clients. One alternative is to use shared identity file to use with ssh -i, but some might consider that file to be compromised once it is readable by more than one user. We choose to allow a small number of users sudo access to ssh and scp alone. That way we can grant access by adding one user name to one group on the springboard computer. If you use cfengine or puppet to manage states, or have some other smart way of synchronizing authorized_keys that leaps back as a good alternative. - Klaus Wik
You could also just let them sudo ssh without requiring a password. - Cairnarvon
If we did not ask for a password at one time there would be problems on the political and legal layers of the OSI protocol. Sorry, thats a real flippant way for me to say "No, those are several hundred computers where you as root have access to possibly sensitive files and we must make at least half an effort to make sure you are one of us" ;) - Klaus Wik

1 Answers

1
votes

It seems that the correct answer is...

no

All the communication happens in separate and completely disjoint [pt]ty's. Credentials can not be transfered and the pty created by pexpect can not be linked to the tty the script is run in.