Force-reboot Cisco phones 79xx from Asterisk

Here are the steps on how to push the Cisco phone 7942G to reboot.

Step 1) Authentication URL

If you want to Push an XML Object to a Cisco IP Phone, it first tries to authenticate your request against a Call Manager. If you don’t have a Call Manager (e.g. you’re using Asterisk), then you can use “<authenticationURL></authenticationURL>” in the SEP<MAC>.cnf.xml. The phone can then be directed to any web server which handles the authentication. In my case I set the authenticationURL to:

<authenticationURL>http://AsteriskPBX/cisco/aut.php</authenticationURL>

Step 2) Authorization PHP script

Now it’s up to you, what your script answers according to UserID and Password. A simple authentication script, always allowing access, would be:

<?php
echo "AUTHORIZED";
?>

Step 3) Running the PHP script to reboot the phone.

Since rebooting the phone is just a matter of pressing Settings  -> **#**, all we have to do is tell the phone to dial these digits and it will reboot. (This script assumes that you already have the authorization script set up in SEP<MAC>.cnf.xml and that it is allowing the username and password at the top to reboot the phone.

<? 

$username = "cisco";
$password = "cisco"; 

$operation = $argv[1];
$exten = $argv[2]; 

//echo $operation."||".$exten."\n";
$ips = array();
exec("asterisk -r -x \"sip show peers\"",$ips); 

//print_r($ips);
$ip = false;
foreach($ips as $key => $data){
//echo "key: $key || exten: $exten || value: $data \n";
    if(strpos($data,$exten) !== false){
        $data = preg_split("/[\s]+/", $data);
        $ip = $data[1];
        $data = preg_split("/[\/]+/", $data[0]);
        //echo $data[0]." and ip is ".$ip."\n";
    }
}
if(!$ip)
    die("Couldn't find extension: ".$exten."\n"); 

//echo $ip; exit(0);
$actions['reboot'][0] = array(0 => "Key:Settings", 1 => "2");
$actions['reboot'][1] = array(0 => "Key:KeyPadStar", 1 => ".3");
$actions['reboot'][2] = array(0 => "Key:KeyPadStar", 1 => ".3");
$actions['reboot'][3] = array(0 => "Key:KeyPadPound", 1 => ".3");
$actions['reboot'][4] = array(0 => "Key:KeyPadStar", 1 => ".3");
$actions['reboot'][5] = array(0 => "Key:KeyPadStar", 1 => ".3"); 

$actions['setimage'][0] = array(0 => "Key:Settings", 1 => ".3");
$actions['setimage'][1] = array(0 => "Key:KeyPad1", 1 => ".3");
$actions['setimage'][2] = array(0 => "Key:KeyPad2", 1 => "5");
$actions['setimage'][3] = array(0 => "Key:KeyPad2", 1 => "3");
$actions['setimage'][4] = array(0 => "Key:Soft1", 1 => "7");
$actions['setimage'][5] = array(0 => "Key:Soft2", 1 => "1");
$actions['setimage'][6] = array(0 => "Key:Soft3", 1 => "1");
$actions['setimage'][7] = array(0 => "Key:Soft3", 1 => "1"); 

//$actions['setalttftp'][0] = array(0 => "Key:Settings", 1=> "1");
//$actions['setalttftp'][1] = array(0 => "Key:KeyPad2", 1=> "1");
//$actions['setalttftp'][1] = array(0 => "Key:KeyPadStar", 1 => ".3");
//$actions['setalttftp'][2] = array(0 => "Key:KeyPadStar", 1 => ".3");
//$actions['setalttftp'][3] = array(0 => "Key:KeyPadPound", 1 => "5");
//$actions['setalttftp'][4] = array(0 => "Key:Settings", 1=> "1"); 

$actions['setalttftp'][0] = array(0 => "Key:Settings", 1=> "1");
//$actions['setalttftp'][1] = array(0 => "Key:KeyPad2", 1=> "1");
$actions['setalttftp'][1] = array(0 => "Key:KeyPad2", 1 => "1");
$actions['setalttftp'][2] = array(0 => "Key:KeyPad2", 1 => "0");
$actions['setalttftp'][3] = array(0 => "Key:KeyPad24", 1 => "0");
$actions['setalttftp'][4] = array(0 => "Key:Soft2", 1 => "5");
$actions['setalttftp'][5] = array(0 => "Key:Soft3", 1 => "5");
$actions['setalttftp'][6] = array(0 => "Key:Settings", 1=> "1"); 

foreach($actions[$operation] as $key => $data){
    echo push2phone($ip,$data[0],$username,$password);
    sleep($data[1]);
} 

/* this is the function that does all of the dirty work. */
/* from http://www.voip-info.org/wiki-Cisco+79XX+XML+Push */ 

function push2phone($ip, $uri, $uid, $pwd){
  $auth = base64_encode($uid.":".$pwd);
  $xml  = "<CiscoIPPhoneExecute><ExecuteItem Priority=\"0\" URL=\"".$uri."\"/></CiscoIPPhoneExecute>";
  echo $xml."\n";
  $xml  = "XML=".urlencode($xml); 

  $post  = "POST /CGI/Execute HTTP/1.0\r\n";
  $post .= "Host: $ip\r\n";
  $post .= "Authorization: Basic $auth\r\n";
  $post .= "Connection: close\r\n";
  $post .= "Content-Type: application/x-www-form-urlencoded\r\n";
  $post .= "Content-Length: ".strlen($xml)."\r\n\r\n"; 

  $fp = fsockopen ( $ip, 80, $errno, $errstr, 30);
  if(!$fp){ echo "$errstr ($errno)<br>\n"; }
  else
  {
    fputs($fp, $post.$xml);
    flush();
    while (!feof($fp))
    {
     $response .= fgets($fp, 128);
     flush();
    }
  }
  return $response;
  }
?>

So all you need to do is run this php script, either stand-alone or using Asterisk AGI/AMI:

# php phonePush.php reboot

 

Resources:

Remotely Reboot a Cisco 79xx Phone (without SIP NOTIFY)
Cisco 79XX XML Push
Asterisk phone cisco 79×1 xml configuration files for SIP
Remotely Reboot your Cisco 79xx Phone even with Asterisk PBX
Cisco XML/SIP via Active Directory Auto Provisioning

 

 

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *