Verify if IP works or not - Intranet

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

Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Verify if IP works or not - Intranet

Post by Red Blaze »

I'm working on an intranet webpage, and I can't figure out something. I got a list of Devices with IPs and I want PHP to verify if they are active or not. Except, my boss keeps forgetting I'm NOT a networking/programming major. I'd really like a hand in this. I tried googleling but I only found PC Software. =/

~RB
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

fsockopen() could be used. In order to actually verify you will probably need to talk to the device a little bit to see if it responds.
User avatar
aeav
Forum Newbie
Posts: 9
Joined: Fri Apr 28, 2006 7:01 am
Location: Brasil
Contact:

Post by aeav »

or you can use these functions:
exec("ping xxx.xxx.xxx.xxx");
shell_exec("ping xxx.xxx.xxx.xxx");

Then you just have to check with the IF...
But if the safe_mod is enabled you'll have to use the mod "safe_mode_exec_dir"
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Let your boss define the condition to determine if "the device is active"

What happens with devices on a saturated network where packages get lost?
And a fsockopen or ping request fails?
Or the device simply ignores icmp requests?
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I found this piece of code:

Code: Select all

<?php $up = @fsockopen("$ip", 80, $errno, $errstr, 30);  
if($up) 
{  
   echo 'Online';  
} 
else 
{
   echo 'Offline'; 
} ?>
The $ip part I got, but I assume $errno and $errstr have to mean something. I tried it the way it stands, and the page just doesn't load.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

I got a list of Devices with IPs and I want PHP to verify if they are active or not.
If these are remote IP over the internet then it can't be reliably done due to firewalls.
If these are on a local network fsockopen and `ping $ip` etc commands should be good.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

ole wrote:
I got a list of Devices with IPs and I want PHP to verify if they are active or not.
If these are remote IP over the internet then it can't be reliably done due to firewalls.
If these are on a local network fsockopen and `ping $ip` etc commands should be good.
The problem with fsockopen is that you need to be able to connect to an open port on the other machine. Using a simple icmp ping would be better (but don't forget you can set a firewall to ignore icmp packets). You'll need to do some string comparisons on the result of your exec()'s too.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Red Blaze

What do you consider up? Apache (MS ISS) is up? PHP is up? MySQL (MSSQL/ORACLE) is up?

If you are checking for the above things. You could write a simple web service. When that web service is called it connects and disconnects from your database. Then it returns a predetermined response. If you get that predetermined response back you know Apache/PHP/MySQL is up and running. If you don't. Something is wrong and needs attention.

If you're in the Linux environment you could even setup a simple CRON job with cURL to check the status on a regular basis. If you're in the Windows enviroment, well I can't help you there because I'm not as familiar with Windows.

- Eric
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

@ole:
These are in an internal/local only. I think that's why it didn't want to work.

@EricS:
These devices are printers, computers, etc. The system is going to be Linux, so hopefully I could do a cron job.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Most devices talk http://www.php.net/snmp these days...
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

I've been trying to use this bit of code:

Code: Select all

$ip = $row_records['IP'];
		if($ip == "Not Available"){
		echo 'Offline';
		}else{
		exec("ping $ip"); 
		}
It works for the first three, but then it tells me this:
Fatal error: Maximum execution time of 30 seconds exceeded in ********/index.php on line 99
I think I'm forcing the server with that, but... really, I barely know what I'm doing. ~_~
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Ping makes a lot of trips before it end itself, quite a few more than you need to determine if the device is up or not. Try this:

Code: Select all

exec("ping -c 3 $ip");
Also, you can increase the time that PHP will execute a script before it throws the error that you are getting.

Reference for ini_set() http://us3.php.net/manual/en/function.ini-set.php

Reference for ini configurations http://us3.php.net/manual/en/ini.php#ini.list

- Eric
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

That makes everything load, however, when I echo it, it doesn't display anything. Is it just ignoring it? I found out just now that it's actually Windows and not Linux. My apologies. >_<

EDIT: When I echo this:

Code: Select all

echo exec("ping $ip");


It displays this first:
Minimum = 0ms, Maximum = 0ms, Average = 0ms
Then it does this in another row:
Packets: Sent = 4, Received = 0, Lost = 4 (100% loss)
Then the third row, it gives me the Time Exceeded error.
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post by EricS »

Looks to me like the host your trying to ping is unreachable from your server.

Have you logged into the web server and tried pinging the devices from there?

Also you might try a test script to make sure everything is okay with your server configuration by pinging a host you know should work. Such as

Code: Select all

echo exec("ping http://www.yahoo.com");
- Eric
Red Blaze
Forum Commoner
Posts: 40
Joined: Mon Mar 27, 2006 3:45 pm

Post by Red Blaze »

The IPs I'm using are local IPs. They can only be accessed inside the building. And the server is inside the building. I tried it with a few sites, and I get different echo results that work with different maximums and minimums and averages.
Post Reply