Getting output from the subprocess.Popen command into a webpage? (maybe an apache issue)

1.1k views Asked by At

Here is my program import subprocess

print "Content-type:text/html\r\n\r\n"
print "File starting to execute"
print "<br>"
proc = subprocess.Popen(["sudo", "python", "test3.py"], stdout=subprocess.PIPE)
output = proc.stdout.read()
print "output is %s" %output

print "<br>"
print "File Executed Awesomely"

So when I run it from the command line it works great, like follows->

[root@localhost html]# python test2.py
Content-type:text/html


File starting to execute
<br>

output is .
Sent 1 packets.

<br>
File Executed Awesomely
[root@localhost html]# 

that is perfect the ". Sent 1 packets." is what I want. But when I run it from the webpage, the webpage just has

File starting to execute
output is
File Executed Awesomely

so I originally thought this was because I was doing something wrong grabbing the output but I listened on the port with wireshark (my other program it calls send a packet) and it looks like no packets shows up via the webpage call, but it does when I call it on the command line (the same way). Looking at my apache error_log->

[Wed Jan 18 18:15:11 2012] [notice] suEXEC mechanism enabled (wrapper: /usr/sbin/suexec)
[Wed Jan 18 18:15:11 2012] [notice] Digest: generating secret for digest authentication ...
[Wed Jan 18 18:15:11 2012] [notice] Digest: done
[Wed Jan 18 18:15:11 2012] [warn] ./mod_dnssd.c: No services found to register
[Wed Jan 18 18:15:11 2012] [notice] Apache/2.2.17 (Unix) DAV/2 configured -- resuming normal operations

any suggestions on how I can fix it so my apache cgi-bin script runs the same way as the command line?

EDIT: looking at the log after a few calls it does this repeatedly

[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] :
[Wed Jan 18 18:22:37 2012] [error] [client 10.117.153.89] sorry, you must have a tty to run sudo 
1

There are 1 answers

5
Doncho Gunchev On BEST ANSWER

The message sorry, you must have a tty to run sudo is the key. First of all, letting your apache run sudo is dangerous to say the least, but if you really really want to do it... there's a way, edit /etc/sudoers (visudo) and locate the Defaults requiretty part (man sudoers).

NB: Never let apache run anything using sudo, specify exactly what it needs to do and nothing more!

BTW: It may still not work if you have SELinux enabled or other LSM module.