Birdmansplace Posted November 29, 2009 Share Posted November 29, 2009 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 More sharing options...
redarrow Posted November 29, 2009 Share Posted November 29, 2009 read this please cheers. http://www.webmasterworld.com/php/3544745.htm Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967367 Share on other sites More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 only one problem. regardless of the state thecomputer is i get online no matter what Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967377 Share on other sites More sharing options...
oni-kun Posted November 29, 2009 Share Posted November 29, 2009 Quote 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 Quote 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. how would i go about doing that? Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967411 Share on other sites More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 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 More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 Quote 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 More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 <?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 More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 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 Quote 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 Quote 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 thanks MadTechie! Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967433 Share on other sites More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 Your welcome, as a note the line if(stripos($R,"Reply from") !== false) $valid = true; worked on my WinXP machine (running wampserver) Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967434 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 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? Quote 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 More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 Quote 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 More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 If you read the whole thread your see that this has already been solved and the loopback IP was used as an example. Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967457 Share on other sites More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 Quote If you read the whole thread your see that this has already been solved and the loopback IP was used as an example. Fair enough. Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967459 Share on other sites More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 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 More sharing options...
Andy-H Posted November 29, 2009 Share Posted November 29, 2009 Can you shouw the code you are running to get that result? Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967465 Share on other sites More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 Have you tried the fsocket one, instead (that's my preferred method) Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967470 Share on other sites More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 Quote 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. Quote 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 More sharing options...
MadTechie Posted November 29, 2009 Share Posted November 29, 2009 Humm.. well if ping is responding, then its not turned off! I assume you also get a response from terminal. Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967480 Share on other sites More sharing options...
Birdmansplace Posted November 29, 2009 Author Share Posted November 29, 2009 in terminal the computers that are on are responding to ping are replying. the ones that are not on are coming back destination host unreachable. i have out a note by each computer that is actually on or off. http://birdmansplace.com/birdman/ Link to comment https://forums.phpfreaks.com/topic/183282-simple-ping-code/#findComment-967482 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.