Lighttpd CGI Python无法运行系统进程

时间:2018-09-06 03:52:26

标签: python terminal command cgi lighttpd

我正在尝试从网络python脚本运行终端命令。 我尝试了很多事情,但是没有任何效果。例如:在sudoers中添加'www-data',使用bin的完整路径,使用sudo word运行命令,使用3个不同的系统调用(os.spawnl和子进程),并且没有其中的作品。 只读命令,例如“ ps aux”,仅可输出信息,而对文件的简单回显则无效。似乎需要许可才能这样做。我还能尝试什么?

输出示例:意外错误:(,CalledProcessError(2,'/ bin / echo hello> /var/www/html/cgi-bin/test2.htm'),)

在该示例中,/ var / www / html / cgi-bin /文件夹归“ www-data”所有,与服务器配置相同。

<!-- language: python -->
#!/usr/bin/python3
# coding=utf-8
import os
import sys
import subprocess
import cgi
import subprocess

SCRIPT_PATH = "/var/www/html/scripts/aqi3.py"
DATA_FILE = "/var/www/html/assets/aqi.json"
KILL_PROCESS = "ps aux | grep " + SCRIPT_PATH + " | grep -v \"grep\" | awk '{print $2}' | xargs kill -9"
START_PROCESS = "/usr/bin/python3 " + SCRIPT_PATH + " start > /dev/null 2>&1 &"
STOP_PROCESS = "/usr/bin/python3 " + SCRIPT_PATH + " stop > /dev/null 2>&1 &"


# Don't edit
def killProcess():
    os.spawnl(os.P_NOWAIT, KILL_PROCESS)
    try:
        os.spawnl(os.P_NOWAIT, "/bin/echo hello > /var/www/html/cgi-bin/test2.htm")
        proc = subprocess.Popen(['sudo', 'echo', 'hello > /var/www/html/cgi-bin/test3.htm'])
        print(subprocess.check_output("/bin/echo hello > /var/www/html/cgi-bin/test2.htm", shell=True, timeout = 10))
    except:
        print("Unexpected error:", sys.exc_info())

    print(KILL_PROCESS)


def stopSensor():
    killProcess()
    os.spawnl(os.P_NOWAIT, STOP_PROCESS)


def restartProcess():
    killProcess()
    print(START_PROCESS)
    print(os.spawnl(os.P_NOWAIT, START_PROCESS))


def main():
    arguments = cgi.FieldStorage()
    for key in arguments.keys():
        value = arguments[key].value
        if key == 'action':
            if value == 'stop':
                stopSensor()
                print("ok")
                return
            elif value == 'start' or value == 'restart':
                restartProcess()
                print("ok")
                return
            elif value == 'resetdata':
                try:
                    with open(DATA_FILE, 'w') as outfile:
                        outfile.write('[]')
                except:
                    print("Unexpected error:", sys.exc_info())

                print("ok")
                return

    print("?")


main()

1 个答案:

答案 0 :(得分:0)