pythonplumbum

Issue with running sudo command with python on plumbum


I'm using Python 2.7 with the latest plumbum package from mac ports.

In general, plumbum works great. Though I'm having a heck of a time getting a sudo'd command to work. I've setup my /etc/sudoers for the commands I want to run without having to be prompted, so that's fine. I can run the commands manually without issue.

However, when I try the same from python using this:

sudo["/usr/local/some-magic-command here"]
sudo("-u " + sudoUser) # sudo user is userfoo

I receive the following error:

plumbum.commands.processes.ProcessExecutionError: Command line: ['/usr/bin/sudo', '-u userfoo']

Exit code: 1

Stderr: | sudo: unknown user: userfoo

The user does exist, so not exactly sure what the deal is here.

Comments?


Solution

  • There is no "-u userfoo" user. There is probably just "userfoo". Note: no -u prefix. Try:

    from plumbum.cmd import sudo
    
    as_userfoo = sudo["-u", sudo_user]
    print(as_userfoo("whoami"))