Ping an IP address


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

Documentation generated on Wed, 20 Jul 2005 15:21:32 +0000 by phpDocumentor 1.2.3