0

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.

16
  • print usually appends the output with a \n. Have you tried finding the substring "No one home" instead of an equality test? Commented Jun 10, 2016 at 9:21
  • Uh, what's the point of using Python just to spawn ping? A simple shell script would be simpler, more straightforward, and more efficient. Commented Jun 10, 2016 at 9:31
  • I've also tried print_r($full_output);, which returns Array ( [0] => No one home ). @Tripleee, how would you recommend to write such a shell script? I'm more of a website-guy than a network-guy. Commented Jun 10, 2016 at 9:33
  • 1
    ping -c 1 192.168.1.51 && echo "Welcome home" || echo "Nobody home" ... though ditching the echos and just examining the result code from ping is really the proper way to do it programmatically. Commented Jun 10, 2016 at 9:41
  • 2
    Also serverfault.com/questions/396030/when-is-chmod-777-justified (hint: never). Commented Jun 10, 2016 at 10:17

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.