Trying to run a sudo command from script

Discussion in 'Python' started by Kent Tenney, Jan 1, 2010.

  1. Kent Tenney

    Kent Tenney Guest

    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
    Kent Tenney, Jan 1, 2010
    #1
    1. Advertising

  2. 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
    Diez B. Roggisch, Jan 1, 2010
    #2
    1. Advertising

  3. Kent Tenney

    Kent Tenney Guest

    On Fri, Jan 1, 2010 at 5:08 PM, Diez B. Roggisch <> 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
    >
    Kent Tenney, Jan 3, 2010
    #3
    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:
    369
    Alex Popescu
    Aug 5, 2007
  2. Paul Kölle
    Replies:
    1
    Views:
    398
  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