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