PHP ping

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
pagey
Forum Newbie
Posts: 2
Joined: Thu Jun 24, 2004 5:01 am

PHP ping

Post by pagey »

Hi

This is my problem:

I currently developing an Iplist site for my company. One of the features I want is an automatic ping. This means, when you like to add an IP nr you should be able to see it it has been alive for some time. I want to maka a seach for an IP and find out for how long it has been alive or iif its not currently being used. The Iplist server should ping the entire 192.168.0 network everyday and put the information in a table in Mysql. I am using a Linux server so what Im thinking of is to somehow use the crontab. From the crontab format the information and parse it to my DB.

Please help me with this. Im pretty new to PHP and dont know a lot about the features.

By the way do someone know of an IPlist solution with these functions:
Add/Remove IP
Add/Remove Net
See if the hosts has been alive for a while
Automatic Reverse DNS lookup
Show Nat mapping to external addresses
Info on who checks out the IP/Net
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Here's a start for you:

Code: Select all

<?php

for ($i = 1; $i <= 255; $i++) 
{ 
    if(ping("192.168.0".$i))
    {
        //ping sucessful
        //insert into database
    }
    else
    {
        //ping failed
        //insert into database
    }    
} 



function ping($ip) 
{ 
    $str=exec("ping -c 1 -w ".$ip.",$a,$a1);

    if(strlen($str)>1)
    {
	return true; 
    }
    else 
    {
	return false; 
    }
}
?>
/Edit: Added a bracket.
Last edited by Grim... on Thu Jun 24, 2004 8:42 am, edited 4 times in total.
pagey
Forum Newbie
Posts: 2
Joined: Thu Jun 24, 2004 5:01 am

Post by pagey »

Thanks man.

Gonna try it out as soon as Ihave the opportunity
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Grim... wrote:Here's a start for you:

Code: Select all

&lt;?php

for ($i = 1; $i &lt;= 255; $i++) 
{ 
    if(ping("192.168.0.".$i)
    {
        //ping sucessful
        //insert into database
    }
    else
    {
        //ping failed
        //insert into database
    }    
} 



function ping($ip) 
{ 
    $str=exec("ping -c 1 -w ".$ip.",$a,$a1);

    if(strlen($str)&gt;1)
    {
	return true; 
    }
    else 
    {
	return false; 
    }
}
?&gt;
Apart from the syntax errors, many sysadmins restrict access to ping so it would be doubtful that your code would work. Also ping returns exit code 1 if it did not receive a reply, 2 on error and 0 for everything else, so checking the exit code would be a better way to go.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Errors?

I've spotted the lacking bracket, but where's the other one(s)?
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Code: Select all

$str=exec("ping -c 1 -w ".$ip.",$a,$a1);
????
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Eek!

Well spotted ;)
Post Reply