PDA

View Full Version : make ping with PHP



guif
10-07-2008, 04:49 PM
hi! I put this PHP file into my apache server:



<?php
$ip = $_SERVER['127.0.0.1'];
exec("ping -n 4 $ip 2>&1", $output, $retval);
if ($retval != 0) {
echo "no!";
}
else
{
echo "yes!"; }
?>

I would like to see that's my server is good or dead, and it returns: NO!

why???

Twey
10-07-2008, 05:27 PM
Because $_SERVER['127.0.0.1'] doesn't exist you just meant '127.0.0.1', I'm sure. If you had put error_reporting(E_ALL); at the top of your script, you would have got a handy warning about this. I recommend that you do so for all PHP scripts.

guif
10-08-2008, 07:58 AM
well, i would like to monitoring diferetns servers.
Why not's possible????
127.0.0.1 is the ip of all lan targets...

Twey
10-08-2008, 12:45 PM
No, 127.0.0.1, or 'localhost', is the standard address of the current machine. If you wish to monitor other servers, you will have to replace that with the remote machine's IP address.

guif
10-08-2008, 01:20 PM
I change this code and I put this:


IP: 10.140.206.123 (
<?php
$ping_ex = exec("ping -n 1 10.110.206.123", $ping_result, $pr);
if (count($ping_result) > 1){
echo "<img src=\"on.png\">";
} else{
echo "<img src=\"off.png\">";
}
?> )


IP: 127.0.0.1 (
<?php
$ping_ex = exec("ping -n 1 127.0.0.1", $ping_result, $pr);
if (count($ping_result) > 1){
echo "<img src=\"on.png\">";
} else{
echo "<img src=\"off.png\">";
}
?> )

In the first case the resulst are NO and the second case are YES.
But... the result is YES in two cases. Why???

Twey
10-08-2008, 02:51 PM
Because now you're just checking that there's output, rather than what actually happened.
<?php
function ping($host) {
exec(sprintf('ping -c 1 -W 5 %s', escapeshellarg($host)), $res, $rval);
return $rval === 0;
}

$hosts_to_ping = array('10.140.206.123', '127.0.0.1');
?>

<ul>
<?php foreach ($hosts_to_ping as $host): ?>
<li>
<?php echo $host; ?>
<?php $up = ping($host); ?>
(<img src="<?php echo $up ? 'on' : 'off'; ?>"
alt="<?php echo $up ? 'up' : 'down'; ?>">)
</li>
<?php endforeach; ?>
</ul>

guif
10-09-2008, 11:16 AM
yeah!
finally I put this code:


<?php
$str = exec("ping -n 1 -w 1 100.100.100.100", $input, $result);
if ($result == 0){
echo "<img src=\"on.png\">";
}else{
echo "<img src=\"off.png\">";
}
?>

It's run correctly, but i would like to insert this code into a javascript function because I insert many times this PHP and the page is very hard to load.
I would like to insert a button:


<input type="submit" onClick="para();">

and when I press this button, the javascript runs.
Is it possible??

Dr_Nick
11-13-2008, 06:28 AM
What you wish to do could be possible by using an iframe.

It could originally point to a blank file so it wont have to load the ping. Then javascript can be used to change the address of the iframe


<script type="text/javascript">

function Reload(address) {
var f = document.getElementById("iframe1");
f.src = address;
}

</script>
Just call that function when u want to ping. e.g.

<input type="button" onClick="Reload('ping.php?ip=100.100.100.100');">


You could also make this iframe invisible when not used...

<div id="div1" style="display: none;">
<iframe goes here>
</div>
And toggle visibility with:


<script language="javascript">
<!--

var state = 'none';

function showhide(layer_ref) {

if (state == 'block') {
state = 'none';
}
else {
state = 'block';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
//-->
</script>
<a href="javascript:showhide('div1');">Show/hide</a>

Hope this helps!
Nick