PHP to Ping an array of IPs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jat32
Forum Newbie
Posts: 1
Joined: Mon Feb 20, 2006 7:45 pm
Location: New York

PHP to Ping an array of IPs

Post by jat32 »

I've scoured the web for PHP code showing how to ping an array of IP addresses and have a bunch of good examples. However, none of the code allows me to ping IP addresses outside my server.

The following bare-bones code will only ping a domain name local to the server the PHP code is stored on. I would like to be able to ping a series of external public IP addresses (eg. 66.235.203.72) which I cannot do with the following code:

This works:

Code: Select all

<?php
$ip_address = "www.thehostgroup.com"; 
exec("ping -c 1 $ip_address", $out, $var);
if ($var == 0)
{
	print "Host is up";
}
else
{
	print "Host is down";
}
?>
This does not work- says host is down when it is really up:

Code: Select all

<?php
$ip_address = "66.235.203.72"; 
exec("ping -c 1 $ip_address", $out, $var);
if ($var == 0)
{
	print "Host is up";
}
else
{
	print "Host is down";
}
?>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Works fine for me

Code: Select all

# ping -c 1 66.235.203.72
PING 66.235.203.72 (66.235.203.72) 56(84) bytes of data.
64 bytes from 66.235.203.72: icmp_seq=0 ttl=50 time=79.4 ms

--- 66.235.203.72 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 79.470/79.470/79.470/0.000 ms, pipe 2
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Dubious
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Are you using Linux or windows? That looks like it should work fine on a *nix server.
Post Reply