PHP Check if system is online

From: ANT_THOMAS15 Nov 2011 14:51
To: ALL1 of 9
I want to write a little bit of PHP to check if 2 systems on my network are online.

Currently I've used this
php code:
$output = shell_exec('ping -n 1 192.168.1.90');
echo "<pre>$output</pre>";

and it just shows the ping output. It works, does the job, but it's ugly.

code:
Pinging 192.168.1.90 with 32 bytes of data:
Reply from 192.168.1.90: bytes=32 time<1ms TTL=64
 
Ping statistics for 192.168.1.90:
    Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms


Any better suggestions?

Something to give

192.168.1.90 - Online
or Offline
From: Dan (HERMAND)15 Nov 2011 23:42
To: ANT_THOMAS 2 of 9
From: ANT_THOMAS15 Nov 2011 23:45
To: Dan (HERMAND) 3 of 9
I'm currently using the last suggestion on there, should really try the earlier ones too.
From: Peter (BOUGHTONP)16 Nov 2011 00:16
To: ANT_THOMAS 4 of 9
quote:
the last suggestion on there

The "last" on depends on how you're sorting them, which may well be different to the rest of us.


Here's the code used at backup.tehforum.co.uk - no idea if it's the best or even a good way of doing it, but it works well enough:
php code:
<?php
	function ping($host, $port, $timeout)
	{
		$tB = microtime(true);
		$fP = fSockOpen($host, $port, $errno, $errstr, $timeout);
		if (!$fP) { return '<strong style="color: red;">failed</strong>'; }
		$tA = microtime(true);
		return '<strong style="color:green;">success in '.round((($tA - $tB) * 1000), 0).' milliseconds</strong>';
	}
?>
 
<p>Pinging 178.79.137.101... <? echo @ping("178.79.137.101", 80, 10); ?>.</p>
 
<p>Pinging www.tehforum.co.uk... <? echo @ping("www.tehforum.co.uk", 80, 10); ?>.</p>
 
<p>Verifying forum is online...<?
	if (strpos(strtolower(file_get_contents("http://www.tehforum.co.uk/forum")),'<title>teh forum</title>') !== false)
	{
		echo '<strong style="color:green;">success</strong>';
	}
	else
	{
		echo '<strong style="color:red;">failed</strong>';
	}
?>.</p>
EDITED: 16 Nov 2011 00:16 by BOUGHTONP
From: ANT_THOMAS16 Nov 2011 00:59
To: Peter (BOUGHTONP) 5 of 9

Well I did quote the one I've used in the first post....

 

And thanks for Teh Backup code, I shall give that a go :D

From: ANT_THOMAS16 Nov 2011 01:02
To: ANT_THOMAS 6 of 9
Damn it, ping doesn't work with my Windows Apache server.
From: CHYRON (DSMITHHFX)16 Nov 2011 02:12
To: ANT_THOMAS 7 of 9
quote:
doesn't work with my Windows


the rest is redundant...
From: Lucy (X3N0PH0N)16 Nov 2011 03:36
To: ANT_THOMAS 8 of 9
If it's a web server you want to check then why not just use file() or file_get_contents() to try to retrieve something you know to be there?

(This may be bad practise, I dunno)
From: ANT_THOMAS16 Nov 2011 15:04
To: Lucy (X3N0PH0N) 9 of 9
If I can't get a ping solution working I might have to install some webserver on the two systems in question and use the method you suggest. They're both running ubuntu and one is a VM, so to be honest that one probably doesn't really need to be checked.