# Ping an IP address

1. #!/usr/local/bin/php -q
2. <?php
3. set_time_limit(0);
4. require('phpagi.php');
6. $agi = new AGI();
8. $agi->answer();
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']);
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";
21. // be polite.
22. $agi->stream_file('thanks', '#');
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. }
40. // a minor hack.
41. $str = str_replace('ms', 'milli-seconds', $str);
43. // have festival read back the ping results.
44. $agi->text2wav("$ip - $str");
45. }
47. $agi->hangup();
48. ?>

Documentation generated on Thu, 30 Sep 2010 02:22:03 -0700 by phpDocumentor 1.4.2