简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM