Jump to content

Recommended Posts

been searchin the site/web  and found code thats simple but doesnt work.

 

I have a personal web server running with various things and a place to upload or down load files from out side my lan.

 

I am lookin for a basic code that can use the servers system host file to ping the rest of the network computers. Simple as Computer a: on or off, computer b,c,d,e and so on. no need for time just a single request to find out if its on or not.

 

thanks

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/
Share on other sites

thanks...

 

code that works for me

 

<?php include("html/sidebar.html"); ?>

  <div id="mainContent">
    <h1> Main Content </h1>
    <p><?php
 $ping_ex = exec("ping -c4 127.0.0.1", $ping_result, $pr);

if (count($ping_result) > 1){
echo 'Ping online - response';
} else {
echo 'Ping offline - response';
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967370
Share on other sites

only one problem.  regardless of the state thecomputer is i get online no matter what

 

if (count($ping_result) > 1){

 

What is the output if it cannot be found? "Host not reachable" IS > 1. You can't compare it that easily, you'll need to check if it is a number or a string.

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967401
Share on other sites

Personally I don't like using exec, using sockets or fsockets works fine,

example

<?php
$URL = "www.phpfreaks.com";

$X = Ping($URL,300);
if($X == 0){
echo 'Ping offline - response';
}else{
echo 'Ping online - response';
}


function Ping($WebServer,$timeout=10,$Port = 80){
Set_Time_Limit(0);  //Time for script to run .. not sure how it works with 0 but you need it
Ignore_User_Abort(True); //this will force the script running at the end

$handle = @fsockopen($WebServer, $Port,$errno,$errstr,$timeout);
if (!$handle){
	//echo "Failed to open ProxyServer $WebServer errno=$errno,errstr=$errstr<br>";
	return 0;
}
else {
	$status = socket_get_status($handle);

	//Time the responce
	list($usec, $sec) = explode(" ", microtime(true));
	$start=(float)$usec + (float)$sec;


	$timeout=120;
	stream_set_timeout($handle,$timeout);
	//send somthing
	ini_set('display_errors','0');
	$write=fwrite($handle,"echo this\n");
	if(!$write){
		return 0;
	}

	stream_set_blocking($handle,0);
	//Try to read. the server will most likely respond with a "ICMP Destination Unreachable" and end the read. But that is a responce!
	fread($handle,1024);
	fclose($handle);
	ini_set('display_errors','1');

	//Work out if we got a responce and time it
	list($usec, $sec) = explode(" ", microtime(true));
	$laptime=((float)$usec + (float)$sec)-$start;
	if($laptime>$timeout) return 0;
	return $laptime;
}
}

?>

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967421
Share on other sites

Oh but if you use the exec then you can check it like this

$ping_ex = exec("ping 127.0.0.1", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
if(stripos($R,"Reply from") !== false) $valid = true;
}
if($valid){ echo "On-line"; }else{ echo "Off-line";}

 

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967422
Share on other sites

Oh but if you use the exec then you can check it like this

$ping_ex = exec("ping 127.0.0.1", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
if(stripos($R,"Reply from") !== false) $valid = true;
}
if($valid){ echo "On-line"; }else{ echo "Off-line";}

 

this doesnt work.  and cant figure out why.  no matter what ip i give it says off line.

 

thanks for the help

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967424
Share on other sites

Try his for debugging

<?php
$ping_ex = exec("ping 127.0.0.1", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
if(stripos($R,"Reply from") !== false) $valid = true;
}
if($valid){
echo "On-line";
}else{
echo "Off-line";
echo "$ping_ex - ".implode("<br>\n",$ping_result);
}

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967425
Share on other sites

<?php
$ping_ex = exec("ping -c4 127.0.0.1", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
   if(stripos($R,"Reply from") !== false) $valid = true;
}
if($valid){
   echo "On-line";
}else{
   echo "Off-line";
   echo "$ping_ex - ".implode("<br>\n",$ping_result);
}
?>

 

output:

(debian os)

 

Off-linertt min/avg/max/mdev = 0.017/0.022/0.029/0.006 ms - PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.

64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.029 ms

64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.026 ms

64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.017 ms

64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.017 ms

 

--- 127.0.0.1 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

rtt min/avg/max/mdev = 0.017/0.022/0.029/0.006 ms

 

output 2:

(winblows)

Off-linertt min/avg/max/mdev = 0.059/0.074/0.081/0.012 ms - PING 192.168.1.86 (192.168.1.86) 56(84) bytes of data.

64 bytes from 192.168.1.86: icmp_seq=1 ttl=128 time=0.059 ms

64 bytes from 192.168.1.86: icmp_seq=2 ttl=128 time=0.081 ms

64 bytes from 192.168.1.86: icmp_seq=3 ttl=128 time=0.081 ms

64 bytes from 192.168.1.86: icmp_seq=4 ttl=128 time=0.077 ms

 

--- 192.168.1.86 ping statistics ---

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

rtt min/avg/max/mdev = 0.059/0.074/0.081/0.012 ms

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967428
Share on other sites

replace

if(stripos($R,"Reply from") !== false) $valid = true;

 

with

if(preg_match('/(\d+) packets transmitted, \1/i', $R)) $valid = true;

 

all its going to do it check that the transmitted and received are the same

4 packets transmitted, 4 received, 0% packet loss, time 2997ms

 

or

if(preg_match('/\d+ packets transmitted, \d+ received, 0% packet loss/i', $R)) $valid = true;

to check no packets where lost

 

~OR~

 

you could do this

if(preg_match('/([1-9]\d*) packets transmitted/i', $R)) $valid = true;

to check at least 1 packet made it

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967430
Share on other sites

replace

if(stripos($R,"Reply from") !== false) $valid = true;

 

with

if(preg_match('/(\d+) packets transmitted, \1/i', $R)) $valid = true;

 

changing code worked

 

<?php
$ping_ex = exec("ping -c4 127.0.0.1", $ping_result, $pr);

$valid = false;
foreach($ping_result as $R){
   if(preg_match('/(\d+) packets transmitted, \1/i', $R)) $valid = true;
}
if($valid){
   echo "On-line";
}else{
   echo "Off-line";
   echo "$ping_ex - ".implode("<br>\n",$ping_result); <!-- remove to remove debug -->
}
?>

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967432
Share on other sites

I don't know the solution to this but the code supplied is pinging the loopback address of the server; therefore will only test if the server is online. Are you using static IP addresses on your computers or using DHCP to setup dynamic addresses?

 

I am lookin for a basic code that can use the servers system host fileto ping the rest of the network computers. Simple as Computer a: on oroff, computer b,c,d,e and so on. no need for time just a single requestto find out if its on or not.

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967453
Share on other sites

I don't know the solution to this but the code supplied is pinging the loopback address of the server; therefore will only test if the server is online. Are you using static IP addresses on your computers or using DHCP to setup dynamic addresses?

 

 

static dhcp for 6 machines

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967456
Share on other sites

also noticed a small issue with the code.

 

if you use this many times on one page the code for some reason list any machine after the first one found online will show online.

example

 

computer a: off-line    (is off)

computer b: online    (is on)

computer c: online    (is off)

computer d: online  (is off)

computer e: online (is off) lol is unpluged from the wall even atm.

 

so i am thinking i may have to make a page with code for one computer at a time and see if same affect when include each to one page.

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967462
Share on other sites

Have you tried the fsocket one, instead (that's my preferred method)

 

Yeah and from the looks of the code its pinging the http port.  sence i only have one webserver in the house i couldnt see it working at all.

 

 

also noticed a small issue with the code.

 

if you use this many times on one page the code for some reason list any machine after the first one found online will show online.

example

 

computer a: off-line    (is off)

computer b: online    (is on)

computer c: online    (is off)

computer d: online  (is off)

computer e: online (is off) lol is unpluged from the wall even atm.

 

so i am thinking i may have to make a page with code for one computer at a time and see if same affect when include each to one page.

 

yeap no change. o well

Link to comment
https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967476
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.