Trying to run a sudo command from script

Diez B. Roggisch deets at nospam.web.de
Sat Jan 2 00:08:58 CET 2010


Kent Tenney schrieb:
> Howdy,
> 
> A script running as a regular user sometimes wants
> to run sudo commands.
> 
> It gets the password with getpass.
> pw = getpass.getpass()
> 
> I've fiddled a bunch with stuff like
> proc = subprocess.Popen('sudo touch /etc/foo'.split(), stdin=subprocess.PIPE)
> proc.communicate(input=pw)
> 
> getting assorted errors with all variations I try.
> 
> Googling says use pexpect, but I'd prefer a stdlib solution.

pexpect is pure python, and it's needed. There is no easy way around the 
issue, so if you insist on not using pexpect, you re-invent the wheel 
and write the exact same code - just more error-prone, because of 
wheel-reinvention....

Diez



More information about the Python-list mailing list