| 1 | <?php\r |
| 2 | \r |
| 3 | function ping($host) {\r |
| 4 | $package = "\x08\x00\x19\x2f\x00\x00\x00\x00\x70\x69\x6e\x67";\r |
| 5 | \r |
| 6 | /* create the socket, the last '1' denotes ICMP */ \r |
| 7 | $socket = socket_create(AF_INET, SOCK_RAW, 1);\r |
| 8 | \r |
| 9 | /* set socket receive timeout to 1 second */\r |
| 10 | socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 3, "usec" => 0)); //<-- Change TimeOut Here <--\r |
| 11 | \r |
| 12 | /* connect to socket */\r |
| 13 | socket_connect($socket, $host, null);\r |
| 14 | \r |
| 15 | /* record start time */\r |
| 16 | list($start_usec, $start_sec) = explode(" ", microtime());\r |
| 17 | $start_time = ((float) $start_usec + (float) $start_sec);\r |
| 18 | \r |
| 19 | socket_send($socket, $package, strlen($package), 0);\r |
| 20 | \r |
| 21 | if(@socket_read($socket, 255)) {\r |
| 22 | list($end_usec, $end_sec) = explode(" ", microtime());\r |
| 23 | $end_time = ((float) $end_usec + (float) $end_sec);\r |
| 24 | \r |
| 25 | $total_time = $end_time - $start_time;\r |
| 26 | \r |
| 27 | return $total_time;\r |
| 28 | } else {\r |
| 29 | return false;\r |
| 30 | }\r |
| 31 | \r |
| 32 | socket_close($socket);\r |
| 33 | }\r |
| 34 | \r |
| 35 | //READLINE\r |
| 36 | function readline ( $fp ) {\r |
| 37 | //echo $prompt;\r |
| 38 | \r |
| 39 | $t = "";\r |
| 40 | $c = "";\r |
| 41 | \r |
| 42 | while(1) {\r |
| 43 | $c = fgetc($fp);\r |
| 44 | if ($c == "\n") { // && ($c == "\r") //May be later on oher systems\r |
| 45 | return $t;\r |
| 46 | } else {\r |
| 47 | $t = ($t.$c);\r |
| 48 | }\r |
| 49 | }\r |
| 50 | \r |
| 51 | return $t;\r |
| 52 | \r |
| 53 | }\r |
| 54 | //READLINE END\r |
| 55 | \r |
| 56 | //Code\r |
| 57 | \r |
| 58 | //echo (ping ("192.168.2.1"));\r |
| 59 | $fp = fopen("hosts.txt", "r+"); //host list file (hosts separated by newline, ends with two empty lines)\r |
| 60 | \r |
| 61 | $fhost = "EMPTY";\r |
| 62 | while ($fhost != "") {\r |
| 63 | \r |
| 64 | $ping = "";\r |
| 65 | $fhost = trim(readline($fp));\r |
| 66 | \r |
| 67 | if ( $fhost != "" ) {\r |
| 68 | echo("HOST: ".$fhost);\r |
| 69 | \r |
| 70 | try {\r |
| 71 | $ping = ping($fhost);\r |
| 72 | } catch(string $err) {}\r |
| 73 | \r |
| 74 | if ( ($ping != "") && ($ping > "0") ) {\r |
| 75 | echo(" - UP PING: ".$ping." sec.\n");\r |
| 76 | } else {\r |
| 77 | echo (" - TIMED OUT\n");\r |
| 78 | }\r |
| 79 | }\r |
| 80 | \r |
| 81 | }\r |
| 82 | \r |
| 83 | ?>\r |