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. // be polite.
  21. $agi->stream_file('thanks', '#');
  22. $p = popen($execstr, 'r');
  23. if($p == FALSE)
  24. {
  25. $agi->text2wav("Failed to ping $ip");
  26. $agi->conlog("Failed to ping $execstr");
  27. }
  28. else
  29. {
  30. $str = '';
  31. while(!feof($p))
  32. {
  33. $r = fgets($p, 1024);
  34. if(!$r) break;
  35. $str .= $r;
  36. }
  37.  
  38. // a minor hack.
  39. $str = str_replace('ms', 'milli-seconds', $str);
  40. // have festival read back the ping results.
  41. $agi->text2wav("$ip - $str");
  42. }
  43.  
  44. $agi->hangup();
  45. ?>

Documentation generated on Wed, 16 Nov 2005 12:49:23 -0700 by phpDocumentor 1.3.0RC3