Re: Trying to run a sudo command from script

Discussion in 'Python' started by Paul Kölle, Jan 2, 2010.

  1. Paul Kölle

    Paul Kölle Guest

    Am 01.01.2010 23:55, schrieb Kent Tenney:
    > Howdy,

    Hi Kent,

    > 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)

    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(),
    stdin=PIPE, stdout=PIPE)

    p = Popen('/usr/bin/sudo /usr/bin/touch /tmp/foo2.txt', stdin=PIPE,
    stdout=PIPE, shell=True)

    The bad news is: It this gives me a password promt inside the
    interactive interpreter. Seems you can't catch stdout this way.

    hth
    Paul

    >
    > getting assorted errors with all variations I try.
    >
    > Googling says use pexpect, but I'd prefer a stdlib solution.
    >
    > Any help appreciated.
    >
    > Thanks,
    > Kent
    Paul Kölle, Jan 2, 2010
    #1
    1. Advertising

  2. Paul Kölle

    Aahz Guest

    In article <>,
    =?ISO-8859-1?Q?Paul_K=F6lle?= <> wrote:
    >
    >If you don't use shell=True you have to provide the full path to
    >commands (and split command and parameters as you do).


    Are you sure? Try this:

    print Popen(['ls'], stdin=PIPE, stdout=PIPE).communicate()[0]
    --
    Aahz () <*> http://www.pythoncraft.com/

    "If you think it's expensive to hire a professional to do the job, wait
    until you hire an amateur." --Red Adair
    Aahz, Jan 14, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sonu
    Replies:
    2
    Views:
    371
    Alex Popescu
    Aug 5, 2007
  2. Kent Tenney

    Trying to run a sudo command from script

    Kent Tenney, Jan 1, 2010, in forum: Python
    Replies:
    2
    Views:
    1,248
    Kent Tenney
    Jan 3, 2010
  3. Replies:
    1
    Views:
    122
    Eric Hodel
    Aug 18, 2007
  4. Esen Sagynov
    Replies:
    6
    Views:
    96
    Esen Sagynov
    Aug 4, 2010
  5. Une Bévue

    testing if a script is run via sudo

    Une Bévue, Oct 7, 2010, in forum: Ruby
    Replies:
    6
    Views:
    102
    Une Bévue
    Oct 7, 2010
Loading...

Share This Page