Trying to run a sudo command from script
paul at subsignal.org
Sat Jan 2 11:13:51 CET 2010
Am 01.01.2010 23:55, schrieb Kent Tenney:
> 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)
If you don't use shell=True you have to provide the full path to
commands (and split command and parameters as you do). So eather of this
works for me:
p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo.txt'.split(),
p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo2.txt', stdin=PIPE,
The bad news is: It this gives me a password promt inside the
interactive interpreter. Seems you can't catch stdout this way.
> getting assorted errors with all variations I try.
> Googling says use pexpect, but I'd prefer a stdlib solution.
> Any help appreciated.
More information about the Python-list