All pastes #268739 Raw Edit

Modification of original sippeers

public php v1 · immutable
·published 2006-12-05 23:44 UTC
rendered paste body
#!/usr/bin/php -q<?# CHANGE THIS TO 1 TO SHOW SIP PEERS AND 0 TO NOT SHOW SIP PEERS$showsip = 1;# CHANGE THIS TO 1 TO SHOW IAX PEERS AND 0 TO NOT SHOW IAX PEERS$showiax = 1;ob_implicit_flush(false);error_reporting(0);set_time_limit(300);$stdin = fopen('php://stdin', 'r');$stdout = fopen( 'php://stdout', 'w' );function read() { global $stdin; $input = str_replace("\n", "", fgets($stdin, 4096)); return $input;}function write($line) { echo $line."\n";}function execute_agi( $command ){ GLOBAL $stdin, $stdout; fputs( $stdout, $command . "\n" ); fflush( $stdout ); $resp = fgets( $stdin, 4096 ); if ( preg_match("/^([0-9]{1,3}) (.*)/", $resp, $matches) )  {  if (preg_match('/result=([-0-9a-zA-Z]*)(.*)/', $matches[2], $match)) {   $arr['code'] = $matches[1];   $arr['result'] = $match[1];   if (isset($match[3]) && $match[3])    $arr['data'] = $match[3];   return $arr;  } else {   $arr['code'] = $matches[1];   $arr['result'] = 0;   return $arr;  } } else {  $arr['code'] = -1;  $arr['result'] = -1;  return $arr; }}function speak($text) { $token = md5(uniqid("")); $tmptext = "/tmp/tts-$token.txt"; $tmpwave = "/var/lib/asterisk/sounds/tts/tts-$token.wav"; $fd = fopen($tmptext, 'w'); fwrite($fd, $text); fclose($fd); system("flite -f $tmptext -o $tmpwave"); $tmpwave = "tts/tts-$token"; unlink($tmptext); return($tmpwave);}// ------ Code execution begins here// parse agi headers into array//while ($env=read()) {// $s = split(": ",$env);// $agi[str_replace("agi_","",$s0)] = trim($s1);// if (($env == "") || ($env == "\n")) {//   break;// }//}while ( !feof($stdin) ) { $temp = fgets( $stdin ); $temp = str_replace( "\n", "", $temp ); $s = explode( ":", $temp ); $agivar[$s[0]] = trim( $s[1] ); if ( ( $temp == "") || ($temp == "\n") ) {  break; }}$arg = $_SERVER["argv"][1];### MAIN CODE ###if ($showsip) { $text = 'SIP extensions and status report:'; $ret = `asterisk -rx "sip show peers" |awk '{split(\$1,ext,"/"); if (\$(NF-1) == "0") \$(NF-1) = ":: Error!: " ; if (\$NF == "ms)") \$NF = "milli seconds"; print ext[1] ", Status " \$(NF-1) " " \$NF }' |grep -w ^[24][0-9][0-9]`; $lines = split("\n", $ret); array_pop($lines); foreach ($lines as $line) {  $i++;  $text .= ": extension, $line:"; } $text .= ": There are, $i, total SIP extensions.";}if ($showiax) { $text .= ': I A X extensions and status report:'; $ret = `asterisk -rx "iax2 show peers" |awk '{split(\$1,ext,"/"); if (\$(NF-1) == "0") \$(NF-1) = ":: Error!: " ; if (\$NF == "ms)") \$NF = "milli seconds"; print ext[1] ", Status " \$(NF-1) " " \$NF }' |grep -w ^[24][0-9][0-9]`;# grep in above will only pass extensions in 2xx and 4xx range - modify as needed for other systems. $lines = split("\n", $ret); array_pop($lines);  $i = 0; foreach ($lines as $line) {  $i++;  $text .= ": extension, $line:"; } $text .= ": There are, $i, total I A X extensions.";}execute_agi("SET VARIABLE TMPWAVE " . speak($text));// clean up file handlers etc.fclose($stdin);fclose($stdout);exit;function debug($str) { $fp = fopen("/tmp/debug.txt", 'a'); fwrite($fp, $str . "\n"); fclose($fp);}?>