that cgi script is returning html for a full page, and doesn't require post. It probably is not meant for the ajax call you are trying to do. try putting a simple link in the main html file to start
<a href='/cgi-bin/test1.py'>
<img style="height: 63px"src="http://images.clipartpanda.com/stop-sign-clipart-119498958977780800stop_sign_right_font_mig_.svg.hi.png">
</a>
If you want to switch to a post, maybe a simple form could also do it.
<form method='post' action='/cgi-bin/test1.py'>
<input type="submit" value="stop">
</form>
I think the ajax call might have the wrong path in it, and a few other issues. This might work. If the alert says 200, it ran.
function bot_stop() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4) {
alert(this.status);
}
};
xmlhttp.open('POST','/cgi-bin/test1.py',true);
xmlhttp.send(null);
}
If you are only accessing the script by ajax, you can change it so it just prints a simple "OK" instead of the full html page. Something like this...
#! /usr/bin/python
import os
# TODO: maybe check that this is a post and not a get before restarting
os.system("sudo service motion stop")
os.system("sudo service motion stop")
os.system("sudo motion")
os.system("sudo service motion start")
print "Content-type: text/plain"
print ""
print "OK"
--------------- next issue-----------
you might not be able to run those commands with sudo if the server user doesn't have permissions set up. Python isn't really needed in the above script, so it can be replaced by this:
#! /bin/bash
sudo service motion stop
sudo service motion stop
sudo motion
sudo service motion start
echo Content-type: text/plain
echo
echo OK
to see what user you are running these scripts as maybe point the browser at the script (called /var/www/cgi-bin/whoami.sh)
(when you create new scripts remember to chmod +x <scriptname>
)
#! /bin/bash
echo Content-type: text/plain
echo
whoami
and then point the browser at http://192.168.1.24/cgi-bin/whoami.sh
to see what happens when you try to sudo a different command try this
#! /bin/bash
echo Content-type: text/plain
echo
sudo echo 1
echo 2
if sudo is the issue, on the command line you can sudo su <whatever user you get from the whoami script>
and see what happens when you try the script as them.
---- and if sudo permissions are the problem ---
a solution could be to put the four restart commands into their own script, and give the lighttpd user sudo permissions to run that script without a password like this: https://serverfault.com/questions/294661/how-to-grant-sudo-rights-only-to-specific-script-files. (or find another way to make those commands work without sudo - could be the subject of another question)
#!
is backwards in the code above.
When you run cgi scripts on the command line to test them, try running them like this
./test1.py
not
python test1.py
and that will help make sure that the interpreter and permissions are set up correctly. You may have to fix the permissions like this chmod +x test1.py
In some cases the server will use this information to run the script. In other cases I case you configure the interpreter for the extension and none of this part matters.