简体   繁体   English

通过CGI(Python)运行sudo命令

[英]Running sudo command via CGI (Python)

I am writing a test suite for a web application using Selenium. 我正在使用Selenium为Web应用程序编写测试套件。

In the course of which I need to test behaviour of the app in case a certain service is running or not. 在此过程中,我需要测试应用程序的行为,以防某个服务正在运行。

I wanted to create a cgi call to a Python script turning that service on and off. 我想创建一个cgi调用Python脚本来打开和关闭该服务。

I know that the cgi call is in the context of the webserver (Apache) however thought that issuing sudo calls like so: 我知道cgi调用是在webserver(Apache)的上下文中,但是认为发出sudo调用是这样的:

import subprocess
import os
command = 'sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'
pwd = 'pwd123'
test1 = subprocess.Popen( command, shell=True, stdin=subprocess.PIPE)
test1.communicate(input=pwd)
test2 = os.system( 'echo %s|%s' % (pwd,command) )

would do the trick, well they don't I get return code 256. 会做的伎俩,他们不会得到返回代码256。

What can I do to have this call be executed w/o touching the context in which Apache runs? 如果不接触Apache运行的上下文,我该怎么做才能执行此调用?

As for security: this will only run on a test machine. 至于安全性:这只能在测试机器上运行。

It could potentially be a pathing issue.. 它可能是一个路径问题..

Have you tried writing out the full path like this: 你有没有尝试写出这样的完整路径:

command = '/usr/bin/sudo -S launchctl unload /Library/LaunchAgents/com.my.daemon.plist'

command should be a list, not a string. 命令应该是一个列表,而不是一个字符串。 Try with: 试试:

command = ['sudo', '-S', 'launchctl', 'unload', '/Library/LaunchAgents/com.my.daemon.plist']

The user that Apache runs as needs to be in the /etc/sudoers file, or belong to the sudo group, which I guess it usually doesn't. Apache运行的用户需要位于/ etc / sudoers文件中,或者属于sudo组,我猜它通常不会。 You also need to make it not ask for a password, which is configured in /etc/sudoers 您还需要让它不要求在/ etc / sudoers中配置的密码

For Ubuntu, check these out: https://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line 对于Ubuntu,请查看以下内容: https//askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line

https://askubuntu.com/questions/147241/execute-sudo-without-password https://askubuntu.com/questions/147241/execute-sudo-without-password

不能以这种方式运行sudo - sudo需要一个控制终端才能运行。

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

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