![]() |
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. |
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?
|
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). |
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 |
In php, you'd use something like
passthru("ping $ip"); |
Quote:
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 |
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. |
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!! |
Heh. Simpler PHP script. Thanks to aixccapt99 for the passthru command.
Code:
<html><head><title>Poing!</title></head>http://nkuvu.homeip.net/ping_test.php |
Quote:
Code:
<html><head><title>Poing!</title></head> |
Er. Yeah.
That's what I meant. :) |
I have developed this a script bit further:
Code:
<html><head><title>Poing!</title></head> |
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? |
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. |
Hmm. That's because your call to ping isn't quite right. From PHP.net:
Quote:
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");http://nkuvu.homeip.net/ping_testing...ixed_table.php |
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. |
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. |
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. :) |
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.
|
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. |
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.
|
I ran into that oddity once, and couldn't reproduce it.
The results from a ping just a minute ago: Quote:
|
Quote:
I added Redirect directives to my httpd.conf file, as follows: Code:
Redirect permanent /foo.txt http://nkuvu.homeip.net/ping_testing/foo.txtBut I am surprised that people are still looking at this thread for such simple scripts... |
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.