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.

Jim
  • 41
  • 5
  • print usually appends the output with a `\n`. Have you tried finding the substring `"No one home"` instead of an equality test? – DomTomCat Jun 10 '16 at 09: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. – tripleee Jun 10 '16 at 09: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. – Jim Jun 10 '16 at 09:33
  • What's `print_r($read)` give you? It should be the last line of output from the executed command, but do check. You could just rely on the output array and check `$full_output[0]` in this particular case. – Ilja Everilä Jun 10 '16 at 09:35
  • `print_r($read)` gives me `No one home`. – Jim Jun 10 '16 at 09:36
  • Sorry for asking the obvious, but just wanted to be sure. It seems that for reasons unknown to us the ping fails when run by PHP (and python through it). – Ilja Everilä Jun 10 '16 at 09:40
  • 1
    `ping -c 1 192.168.1.51 && echo "Welcome home" || echo "Nobody home"` ... though ditching the `echo`s and just examining the result code from `ping` is really the proper way to do it programmatically. – tripleee Jun 10 '16 at 09:41
  • You should try what @tripleee is recommending. Just skip the middleman. – Ilja Everilä Jun 10 '16 at 09:41
  • @IljaEverilä I added `print "test"` to the else statement of the python script, but then the php script gives me an error with `$full_output[0]` (undefined offset). I would expect a "No one home". @tripleee Is it really that simple? I assume it's python, how do I handle this with my php file? An empty .py file with that single line and a .php which echo's exec(pythonfile) isn't doing anything. – Jim Jun 10 '16 at 09:48
  • Tried running your example code and it works for me, both directly using the python script and running with the PHP script. I'm still somewhat sure the issue lies in how the PHP script is run: is it run by a web server running on your Pi? What tripleee suggested is just a shell command, i.e. `exec('ping ...')` in PHP. – Ilja Everilä Jun 10 '16 at 09:59
  • Ok I've got @tripleee's script running, thanks for that! It's a lot more compact and clean. However, I still have the problem that the ping works from the command line but does not work with a php script (giving me 'nobody home'). It is running on a webserver on my Pi, file permissions 777 (for now). Shebang is #!/usr/bin/php. – Jim Jun 10 '16 at 10:06
  • http://stackoverflow.com/questions/345794/php-exec-error-responses and I am not a PHP person but that would be where I would start. – tripleee Jun 10 '16 at 10:13
  • 2
    Also http://serverfault.com/questions/396030/when-is-chmod-777-justified (hint: never). – tripleee Jun 10 '16 at 10:17
  • Yeah I know 777 is not justified, I'll change it back when I'm done testing ;) Using `var_dump()` I can only find out that the string is correctly received in php, but why the results are 'nobody home' instead of 'Welcome home', I don't understand. I have no clue whether this is a php or python issue (probably both?) – Jim Jun 10 '16 at 10:24
  • Adding `2>&1` after the ip address gave me more information, it seems to be a sudo problem (and therefore this is not a duplicate of other topics). `sudo: no tty present and no askpass program specified`. I've tried editting sudoers (`www-data ALL = NOPASSWD: /path/to/phpfile.php `) but that doesn't help me. I feel I'm close, but what's the final step here? – Jim Jun 10 '16 at 13:24
  • It is solved! It seems that I had to specify the path to the ping command (ie /bin/ping) instead of the php file. Added `www-data ALL = NOPASSWD: /bin/ping` and now it works! (And I learned a lot, again) Thanks a lot for your help guys. – Jim Jun 10 '16 at 13:47

0 Answers0