linux - 从网络服务器调用时,python 子进程命令未成功执行

标签 linux python-2.7 subprocess cgi lighttpd

我有一个名为 test.py 的示例文件

import subprocess, sys
from pyroute2 import netns
import subprocess32
import logging
cmd = "ping 192.168.121.1 -I enp5s0"

logFile = "TestLog.txt"
logging.basicConfig(filename = logFile,level=logging.DEBUG,
                    format='%(asctime)s [%(filename)s:%(lineno)s - %(funcName)s]%(levelname)s %(message)s',
                                        datefmt='%m/%d/%Y %I:%M:%S %p')

def ping():
    try:
        subprocess32.check_output(cmd, shell=True, timeout = 10)
    except subprocess32.TimeoutExpired as ex:
        logging.info("Duration completed")
        logging.info(ex.output)
    except Exception as ex:
        template = "ERROR: An exception of type {0} occurred. Arguments:{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        logging.info(message)

def addNamespace(namespace):
    setNs = "ip netns add %s"%(namespace)
    logging.info(setNs)
    proc = subprocess.Popen(setNs.split(' '))
    ret = proc.communicate()
    logging.info("Return Code:%d STDOUT/STDERR:%s"%(proc.returncode, str(ret)))
    logging.info(netns.listnetns())


if __name__ == '__main__':
   ping()
   addNamespace('b01s')

当我从命令行 python test.py 运行它时,我在日志文件中得到了预期的输出:

08/16/2017 11:25:52 AM [test.py:16 - ping]INFO Duration completed
08/16/2017 11:25:52 AM [test.py:17 - ping]INFO PING 192.168.121.1 (192.168.121.1) from 192.168.121.75 enp5s0: 56(84) bytes of data.
64 bytes from 192.168.121.1: icmp_seq=1 ttl=255 time=0.316 ms
64 bytes from 192.168.121.1: icmp_seq=2 ttl=255 time=0.256 ms
64 bytes from 192.168.121.1: icmp_seq=3 ttl=255 time=0.276 ms
64 bytes from 192.168.121.1: icmp_seq=4 ttl=255 time=0.261 ms
64 bytes from 192.168.121.1: icmp_seq=5 ttl=255 time=0.276 ms
64 bytes from 192.168.121.1: icmp_seq=6 ttl=255 time=0.278 ms
64 bytes from 192.168.121.1: icmp_seq=7 ttl=255 time=0.366 ms
64 bytes from 192.168.121.1: icmp_seq=8 ttl=255 time=0.278 ms
64 bytes from 192.168.121.1: icmp_seq=9 ttl=255 time=0.306 ms
64 bytes from 192.168.121.1: icmp_seq=10 ttl=255 time=0.268 ms

08/16/2017 11:25:52 AM [test.py:25 - addNamespace]INFO ip netns add b01s
08/16/2017 11:25:52 AM [test.py:28 - addNamespace]INFO Return Code:0 STDOUT/STDERR:(None, None)
08/16/2017 11:25:52 AM [test.py:29 - addNamespace]INFO ['b01s']

但是,当我通过 linux 上的 lighttpd 服务器调用相同的代码时,我得到以下信息:

08/16/2017 11:22:11 AM [test.py:21 - ping]INFO ERROR: An exception of type CalledProcessError occurred. Arguments:()
08/16/2017 11:22:11 AM [test.py:25 - addNamespace]INFO ip netns add b01s

我正在通过 cgi (lighttpd) 运行 python 脚本,以在 lighttpd 中配置 cgi:

在 modules.conf 中添加:server.modules += ( "mod_cgi") 在 cgi.conf 中:

cgi.assign    = ( ".pl"  => "/usr/bin/perl",
                                        ".py"  => "/usr/bin/python" )

                      $HTTP["url"] =~ "^/cgi-bin" {
   cgi.assign = ( ".py" => "/usr/bin/python" )

并确保日志文件属于 lighttpd 进程。

我在 CentOS 7.2 上运行它

编辑: 从 lighttpd 运行时,用户和组不是 root 而是 lighttpd。如果我使用 Popen 而不是 check_output 从执行的命令打印错误,我得到错误 ping: socket: Operation not permitted

这似乎是一个权限错误。那么我该如何授予 lighttpd root 权限呢?

最佳答案

这看起来像是权限问题。如果可以为您的 lighttpd 进程提供完全访问权限,请转到 \etc\sudoers 并添加行 lighttpd ALL=(ALL:ALL) NOPASSWD: ALL。这将授予 lighttpd 用户完全访问权限/特权,而无需提示输入密码。让我知道它是否有效。

关于linux - 从网络服务器调用时,python 子进程命令未成功执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707336/

相关文章:

python - 对 Python 脚本进行 deamonizing 后无法读取外部命令的 stdout 输出

python - 在带有嵌套字典的 Python 中,我应该检查类型吗?我还可以使用什么其他方法?

python - 有没有办法在 pyplot 极坐标图中指定角轴上的轴单位?

Python:为子进程提供输入

Python函数捕获子进程stdout和stderr到日志文件

java - 如何在 python 代码中获取 .jar 执行的输出?

c++ - 如何在 nvidia 驱动程序中使用 opengl?

c - 如何将c程序转换为cgi?

linux - 生成文件:439: recipe for target 'mysql.o' failed

Python 猴子修补对象未按预期工作