The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Networking (http://hintsforums.macworld.com/forumdisplay.php?f=14)
-   -   Perl / PHP Script to Ping (http://hintsforums.macworld.com/showthread.php?t=16954)

waked1 11-03-2003 01:22 PM

Perl / PHP Script to Ping
 
OK, bit of a long shot posting this here but I looked everywhere else and couldn't find anything useful.
What I'm trying to do is have a script accessible via a webserver that will ping a machine and reply with a simple 'up' or 'down'.
My reason for doing this is that I have some machines at home behind a NAT router and I want to be able to see if any of them are on or off (big brother is watching and all that) from either work or uni. The webserver will be located inside the home network with port 80 forwarded to it. I know I could do it by ssh'ing into it and pinging manually but everything but port 80 is blocked from work.
Any assistance would be gratefully received.

nkuvu 11-03-2003 01:46 PM

Do you already have your webserver up and running? If so, have you modified the httpd.conf (I'm assuming that you're using or planning on using Apache that comes with OS X) so that Perl or PHP run?

nkuvu 11-03-2003 02:09 PM

If your webserver is already running, take a look at this ultra-simplified script I just hacked out:
http://nkuvu.homeip.net/foo.txt

Note that I parsed the results of (gasp!) Windows ping, since I'm not in front of my OS X box at the moment. If you want to give me the output of the ping from OS X (for three cases: an available IP, an unavailable IP, and a bad (or unreachable) IP) I'd be happy to modify the script to suit.

And every line that starts with a # is a comment, which shows you what I parsed out.

Also note that I am assuming that your desired IP address is going to be constant. If you want a user input IP that would be a bit more difficult (though of course not impossible).

waked1 11-03-2003 05:42 PM

Thanks for the quick reply. I tried amending the pattern matching for the X output but with no joy.
The results I get for ping are
[bob:~] waked1% ping -c 1 10.0.1.20
PING 10.0.1.20 (10.0.1.20): 56 data bytes

--- 10.0.1.20 ping statistics ---
1 packets transmitted, 0 packets received, 100% packet loss

[bob:~] waked1% ping -c 1 10.0.1.1
PING 10.0.1.1 (10.0.1.1): 56 data bytes
64 bytes from 10.0.1.1: icmp_seq=0 ttl=64 time=1.172 ms

--- 10.0.1.1 ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 1.172/1.172/1.172 ms

[bob:~] waked1% ping badger.mushroom
ping: unknown host badger.mushroom

Thanks again,
Dunc

aixccapt99 11-03-2003 05:54 PM

In php, you'd use something like

passthru("ping $ip");

nkuvu 11-04-2003 12:27 PM

Quote:

Originally posted by aixccapt99
In php, you'd use something like

passthru("ping $ip");
Unfortunately my PHP is very poor, so I have no idea how to go from there. Can you say parsing?

Anyway, I modified the script a bit. It's still a hack. But you can see the mods here:
http://nkuvu.homeip.net/ping_test.cgi

Note that I don't have it in a CGI executable directory so you'll actually see the source. But I think it's set up so that you can really execute it...

Other than using Windows line endings (now fixed) it's working fine. The executable script:
http://nkuvu.homeip.net/cgi-bin/ping_test.cgi

nkuvu 11-04-2003 12:56 PM

And if I get really ambitious and bored, I'll change things so you can enter a desired IP to ping.

The problem with that is security -- you have to ensure that the user input is not filled with escape codes and stuff.

waked1 11-04-2003 02:04 PM

Thanks very much, that was exactly what I was looking for. What I'm going to do is use server side includes to run the script (with set IP's) in various areas of the page.
Time to look up on expressions methinks!!

nkuvu 11-04-2003 02:16 PM

Heh. Simpler PHP script. Thanks to aixccapt99 for the passthru command.

Code:

<html><head><title>Poing!</title></head>
<body>
<?php
$ip = "192.168.1.1";
passthru("ping -c 1 $ip");
?>
</body>

Who needs PHP parsing? This just shows you the result of the ping command directly.

http://nkuvu.homeip.net/ping_test.php

aixccapt99 11-04-2003 06:14 PM

Quote:

Originally posted by nkuvu
Code:

<html><head><title>Poing!</title></head>
<body>
<?php
$ip = "192.168.1.1";
passthru("ping -c 1 $ip");
?>
</body>


And even better, force well-formatted output:
Code:

<html><head><title>Poing!</title></head>
<body>
<pre>
<?php
$ip = "192.168.1.1";
passthru("ping -c 1 $ip");
?> </pre>
</body>


nkuvu 11-04-2003 07:09 PM

Er. Yeah.

That's what I meant. :)

ulrichm 11-05-2003 05:34 AM

I have developed this a script bit further:
Code:

<html><head><title>Poing!</title></head>
<body>
<table><tr>
<?php

$numberping = 1;
$ip = "192.168.1.1";
$name = gethostbyaddr($ip);

$str = exec("ping -c $numberping -w 1 $ip",$a,$a1);
$length = strlen($str);
if($length > 1) { print "<td bgcolor='green'>IP: ".$ip." (".$name.") is up</td>"; }
else { print "<td bgcolor='red'>IP: ".$ip." (".$name.") is DOWN</td>"; }

?>
</tr></table>
</body>
</html>

I hope it helps!

nkuvu 11-05-2003 12:59 PM

It doesn't seem to work.

http://nkuvu.homeip.net/ping_table.php
compare to
http://nkuvu.homeip.net/ping_test.php

And where do $a and $a1 come from?

ulrichm 11-05-2003 02:41 PM

I do not know why it does not work for you. Might it have something to do with looking up the dns?
The $a and $a1 come from a more extensive script I use now, but are irrelavant for this little script.

nkuvu 11-05-2003 02:57 PM

Hmm. That's because your call to ping isn't quite right. From PHP.net:
Quote:

string exec ( string command [, array output [, int return_var]])

...

If the output argument is present, then the specified array will be filled with every line of output from the command.
So the exec command was sending the results of the ping command to $a. Which doesn't exist for this script.

Also, the -w switch isn't supported on my version of ping. The manpage for ping from my system:
http://nkuvu.homeip.net/ping_testing/ping.txt

Note that I fixed the script, using the line
Code:

$str = exec("ping -c $numberping $ip");
and put it up at
http://nkuvu.homeip.net/ping_testing...ixed_table.php

nkuvu 11-05-2003 03:26 PM

Note that I've done a little cleanup, so the links are as follows (listed in order of appearance ;) ):
http://nkuvu.homeip.net/ping_testing/foo.txt
http://nkuvu.homeip.net/ping_testing/ping_test.cgi
http://nkuvu.homeip.net/cgi-bin/ping_test.cgi (this is the same)
http://nkuvu.homeip.net/ping_testing/ping_test.php
http://nkuvu.homeip.net/ping_testing/ping_table.php
http://nkuvu.homeip.net/ping_testing/ping.txt
http://nkuvu.homeip.net/ping_testing...ixed_table.php

I'd edit the entries, but there's a 1440 minute limit on editing.

ulrichm 11-05-2003 03:48 PM

Nkuvu: I had already thought that you (re-)move them, because I looked shortly before you posted the "new" urls, and I got an error page, which was very original I must say! :-)

I hope that waked1 (and others) now has an almost perfect script to work with.

nkuvu 11-05-2003 04:28 PM

Yeah, I was in the process of moving and re-moving (as in moving again, not deleting) and changing names and so on.

Heh. And I've spent way too much time on such a simple script. :)

ulrichm 11-05-2003 04:59 PM

But it was fun :-), and I hope it helped many others who just need such a file and build it into their website to check the status of their server or mac behind a router. If you do, please leave a line here.

nkuvu 11-05-2003 07:18 PM

Hmm, I guess I was bored at work today. ;) :D

The working form:
http://nkuvu.homeip.net/cgi-bin/ping_form.cgi

The source for both CGI scripts:
http://nkuvu.homeip.net/ping_testing/ping_form.cgi
http://nkuvu.homeip.net/ping_testing/ping_go.cgi

There are two checks for tainted input. The first accepts numeric IP addresses only (one to three digits, a dot, one to three digits, a dot, one to three digits, a dot, one to three digits). So I used the following to test that check. General results in parens:
192.168.1.1 (up)
192.168.1.113 (down)
192.168.1.1130 (Urk. Error message generated)
19w2.168.1.113 (Urk. Error message generated)
19w2.16\n8.1.113 (Urk. Error.)
w192.168.1.1 (No results provided by ping*)

The second taint check looks for any characters a-z, 0-9, a literal -, or dots. This allows input like 'google.com' but stops 'google.com\n\n..\scripts\blah\blah\blah'. Note that the input has to start with a letter, upper or lower case. '-google.com' provides an error.

I think that the taint checks are pretty good, but if someone sees a hole of any sort let me know.



* This test case fails the first test, but passes the second.

ulrichm 11-06-2003 02:30 AM

nkuvu: Your script only works for IPs in the 192.168.1.x subnet. Anything outside, I just tried my IP, do result in packet loss. Furthermore, you might want to limit the entry to 1<x<254. Because 255 is the broadcast address and can find comps on your network, which you might want outsiders not too see. Just my 2€ct.

nkuvu 11-06-2003 11:02 AM

I ran into that oddity once, and couldn't reproduce it.

The results from a ping just a minute ago:
Quote:

PING google.com (216.239.37.99): 56 data bytes
64 bytes from 216.239.37.99: icmp_seq=0 ttl=52 time=94.843 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0% packet loss
round-trip min/avg/max = 94.843/94.843/94.843 ms
But the 254 limit is a good idea, thanks. I'll be working on this off and on, so don't be surprised if the script is broken when you try. :)

nkuvu 05-03-2004 09:00 AM

Quote:

Originally Posted by nkuvu
Note that I've done a little cleanup, so the links are as follows...

For those of you still following the old links (and I know there are a few, because I've seen the 404 responses in my logs) I've made some adjustments. Of course if you read this far down into the thread you'll have the correct links already from the post I just quoted. So this is more to show off how nifty Apache is.

I added Redirect directives to my httpd.conf file, as follows:
Code:

Redirect permanent /foo.txt http://nkuvu.homeip.net/ping_testing/foo.txt
Redirect permanent /ping_test.cgi http://nkuvu.homeip.net/ping_testing/ping_test.cgi
Redirect permanent /ping_test.php http://nkuvu.homeip.net/ping_testing/ping_test.php
Redirect permanent /ping_table.php http://nkuvu.homeip.net/ping_testing/ping_table.php

So if you attempt to access the file http://nkuvu.homeip.net/foo.txt what you actually get is http://nkuvu.homeip.net/ping_testing/foo.txt without any errors or complaints. You'll still get a 404 for the ones that are no longer in cgi-bin, but that's because they're not there. Wanna see how these scripts work? Put 'em on your own machine and run them there. :P

But I am surprised that people are still looking at this thread for such simple scripts...

nkuvu 05-18-2005 07:07 PM

Jiminy. Freakin. Crickets.

OK, so this thread is pretty old. It's been dead for two years. And yet I am still getting requests for these scripts. But I've since changed my Apache configuration a bit, so now the cgi files that I had in the ping_testing directory were being 403'ed. So all of the links to the cgi source files redirect to txt files instead. Same result as before, different extension.

If you follow a link to a CGI executable file (as in, the link points to my cgi-bin directory) you get redirected to the source. As I mentioned last year: Wanna see how these scripts work? Put 'em on your own machine and run them there. :P

As I've mentioned on my website, if you have questions or problems with these scripts, please let me know. The only reason I know about the 403 errors was due to examination of my web logs. If you're following a link that gives you a 403 or 404, let me know. I still have these files, but they may have moved.


All times are GMT -5. The time now is 08:59 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.