Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Trying to run a sudo command from script

Reply
Thread Tools

Trying to run a sudo command from script

 
 
Kent Tenney
Guest
Posts: n/a
 
      01-01-2010
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.

Any help appreciated.

Thanks,
Kent
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      01-01-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
 
Reply With Quote
 
 
 
 
Kent Tenney
Guest
Posts: n/a
 
      01-03-2010
On Fri, Jan 1, 2010 at 5:08 PM, Diez B. Roggisch <(E-Mail Removed)> wrote:
> 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....


Indeed, the requirements of this are way more complex than I guessed.
The following seems to work well, took some fiddling with EOF.

def sudo(command, password=None, prompt="Enter password "):

import pexpect

if not password:
import getpass
password = getpass.getpass(prompt)

command = "sudo " + command
child = pexpect.spawn(command)
child.expect(['ssword', pexpect.EOF])
child.sendline(password)
child.expect(pexpect.EOF)
# is this necessary?
child.close()

Thanks,
Kent

>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
testing if a script is run via sudo Une Bévue Ruby 6 10-07-2010 10:04 AM
ENV['someapp']" is nil when the code is run with "sudo" Esen Sagynov Ruby 6 08-04-2010 04:13 AM
Re: Trying to run a sudo command from script Paul Kölle Python 1 01-14-2010 04:28 AM
need help. want to use Net::sh to do sudo command wbsurfver@yahoo.com Ruby 1 08-18-2007 08:39 PM
how to run os.execv() to run command pslq dbname < gen.command Sonu Python 2 08-04-2007 11:25 PM



Advertisments