gpt4 book ai didi

python - 通过 CGI (Python) 运行 sudo 命令

转载 作者:行者123 更新时间:2023-11-30 23:29:11 24 4
gpt4 key购买 nike

我正在使用 Selenium 为 Web 应用程序编写测试套件。

在此过程中,我需要测试应用程序的行为,以防某个服务正在运行或未运行。

我想创建一个对 Python 脚本的 cgi 调用,以打开和关闭该服务。

我知道 cgi 调用是在网络服务器(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) )

可以解决这个问题,但他们没有,我得到返回代码 256。

我该怎么做才能在不接触 Apache 运行上下文的情况下执行此调用?

至于安全性:这只会在测试机器上运行。

最佳答案

Apache 运行的用户需要位于/etc/sudoers 文件中,或者属于 sudo 组,但我猜通常不会。您还需要使其不要求输入密码,该密码在/etc/sudoers 中配置

对于 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

关于python - 通过 CGI (Python) 运行 sudo 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21251573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com