简体   繁体   English

使用sudo在python中运行linux子进程

[英]Running a linux subprocess in python with sudo

So, this is kind of confusing but essentially I'm using Django and I want to instantiate a subprocess to run a perl script. 因此,这有点令人困惑,但实际上我正在使用Django,并且我想实例化一个子进程来运行perl脚本。 I've read that this can be done with 我读过这可以做到

arg = "/some/file/path/"
pipe = subprocess.Popen(["./uireplace", arg], stdin=subprocess.PIPE)

which works when I call it in the appropriate function in views.py but the script requires a sudo. 当我在views.py中的适当函数中调用它时,它可以工作,但是脚本需要一个sudo。 I then call this 然后我称这个

pipe = subprocess.Popen(["sudo","./uireplace", arg], stdin=subprocess.PIPE)

which works when I run it in python from a terminal but this doesn't work when it's called by a random user on the web. 当我从终端在python中运行它时,它可以工作,但是当网络上的随机用户调用它时,它不起作用。 Is there any way to be able to automatically enter in a username and password for that sudo? 有什么方法可以自动输入该sudo的用户名和密码? The issue is that this can't be done with a prompt so it simply fails. 问题是无法通过提示完成此操作,因此它只会失败。

Solves this problem on the OS level. 在操作系统级别解决此问题。 Giving any user from the web the right to use sudo does not sound right. 向网络上的任何用户授予使用sudo的权利听起来并不正确。 Just make ./uireplace executable. 只需使./uireplace可执行即可。 There are lots of options for chmod to fine tune this. chmod有很多选项可以进行微调。

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

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