I have a working python script which pings my mobile phone, when I run this on a Raspberry Pi from command line I get the right output. However, when I call this script from a PHP file, I get the wrong output.
This is the Python script:
#!/usr/bin/python
import subprocess
import time
def checkIfHome(ip):
output = subprocess.Popen(["ping","-c", "1", ip],stdout = subprocess.PIPE,shell=False)
check = output.communicate()[0]
check = output.returncode
return check
ipAddress1 = "192.168.1.51"
residentHome1 = checkIfHome(ipAddress1)
if residentHome1 == 0:
print "Welcome Home!"
else:
print "No one home"
This is the php script:
error_reporting(E_ALL);
ini_set('display_errors', 1);
$read = exec("python /var/www/html/thuis.py", $full_output);
if ($read=="No one home"){
echo "No one home";
};
if ($read=="Welcome Home!"){
echo "Someone home";
}
All I get is "No one home" while the Python script shows "Welcome Home!" if it's run directly from command line. I think it has to do with some delay or something in the Python script, but I'm not sure.
Can someone shed some light on this? Another, better way to trace the presence of a mobile device within a network with php would also be fine.