Page 1 of 2

Verify if IP works or not - Intranet

Posted: Fri Apr 28, 2006 4:22 pm
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

Posted: Fri Apr 28, 2006 4:34 pm
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.

Posted: Fri Apr 28, 2006 4:40 pm
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"

Posted: Fri Apr 28, 2006 4:44 pm
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?

Posted: Fri Apr 28, 2006 4:51 pm
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.

Posted: Fri Apr 28, 2006 6:49 pm
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.

Posted: Sat Apr 29, 2006 1:33 am
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.

Posted: Sat Apr 29, 2006 12:06 pm
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

Posted: Mon May 01, 2006 9:43 am
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.

Posted: Mon May 01, 2006 10:03 am
by timvw
Most devices talk http://www.php.net/snmp these days...

Posted: Mon May 01, 2006 10:37 am
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. ~_~

Posted: Mon May 01, 2006 10:48 am
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

Posted: Mon May 01, 2006 10:56 am
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.

Posted: Mon May 01, 2006 11:10 am
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

Posted: Mon May 01, 2006 11:28 am
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.