Ping an IP address


1 #!/usr/local/bin/php -q
2 <?php
3 set_time_limit(0);
4 require('phpagi.php');
5
6 $agi = new AGI();
7
8 $agi->answer();
9
10
11 // Play the "Enter the host you wish to ping, followed by the pound sign" prompt
12 // and then play the beep.
13 $agi->stream_file('ping');
14 $result = $agi->get_data('beep', 3000, 20);
15 $ip = str_replace('*', '.', $result['result']);
16
17 /* Danger Will Robinson! This does NOT properly escape the ping command!
18 * Someone could subvert your system if you don't fix this! - NO WARRANTY :P */
19 $execstr = "/bin/ping -c 5 -q -w 9 $ip|grep transmitted";
20
21 // be polite.
22 $agi->stream_file('thanks', '#');
23
24 $p = popen($execstr, 'r');
25 if($p == FALSE)
26 {
27 $agi->text2wav("Failed to ping $ip");
28 $agi->conlog("Failed to ping $execstr");
29 }
30 else
31 {
32 $str = '';
33 while(!feof($p))
34 {
35 $r = fgets($p, 1024);
36 if(!$r) break;
37 $str .= $r;
38 }
39
40 // a minor hack.
41 $str = str_replace('ms', 'milli-seconds', $str);
42
43 // have festival read back the ping results.
44 $agi->text2wav("$ip - $str");
45 }
46
47 $agi->hangup();
48 ?>

Documentation generated on Wed, 25 May 2005 14:29:38 -0600 by phpDocumentor 1.2.3