简体   繁体   English

在plumbum上使用python运行sudo命令的问题

[英]Issue with running sudo command with python on plumbum

I'm using Python 2.7 with the latest plumbum package from mac ports. 我正在使用Python 2.7和来自mac ports的最新plumbum软件包。

In general, plumbum works great. 一般来说,铅非常有效。 Though I'm having a heck of a time getting a sudo'd command to work. 虽然我有一点时间得到一个sudo'd命令工作。 I've setup my /etc/sudoers for the commands I want to run without having to be prompted, so that's fine. 我已经为我想要运行的命令设置了我的/ etc / sudoers而不必提示,所以没关系。 I can run the commands manually without issue. 我可以手动运行命令而不会出现问题。

However, when I try the same from python using this: 但是,当我使用python尝试相同的时候:

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'] plumbum.commands.processes.ProcessExecutionError:命令行:['/ usr / bin / sudo',' - u userfoo']

Exit code: 1 退出代码:1

Stderr: | 斯特德尔:| sudo: unknown user: userfoo sudo:未知用户:userfoo

The user does exist, so not exactly sure what the deal is here. 用户确实存在,所以不确定这里的交易是什么。

Comments? 评论?

There is no "-u userfoo" user. 没有“-u userfoo”用户。 There is probably just "userfoo". 可能只是“userfoo”。 Note: no -u prefix. 注意:没有-u前缀。 Try: 尝试:

from plumbum.cmd import sudo

as_userfoo = sudo["-u", sudo_user]
print(as_userfoo("whoami"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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