Discussion: Polycom reboot hardphone script


 
Comments Filter

Re: Re: Reboot script..revised

there was a slight bug...here is the corrected version...

#!/usr/bin/perl -w

use Net::Ping;
use Socket;

$polycompath = '/home/poly/';    # Where you keep your polycom files
$arp         = '/usr/sbin/arp';          # Location of arp command
$sipserver   = '192.168.52.1';      # IP of asterisk server

$phone = shift;

checkphone("$phone");
touch( arp2config("$phone") );
$thisextension=get_extension($phone);

reboot_sip_phone( "$phone", "$sipserver", get_extension($phone) );

sub checkphone {                     # Checks for existence of phone, makes sure
         				# it's in arp table
     $activephone = shift;

     # Populate ARP table
     print "Checking ARP table.\n";
     $p = Net::Ping->new("icmp");
     if ( $p->ping( $activephone, 2 ) ) {
         print "$activephone is ";
         print "reachable.\n";
     }
     else { die "Polycom at ", $activephone, " is not reachable!"; }
     sleep(1);
     $p->close();

}

sub arp2config {    # Gets mac address from arp table, converts
                     # to a polycom config filename, makes sure
                     # the config file exists
     $arpip = shift;
     open( ARP, "$arp -an|" ) || die "Couldn't open arp table: $!\n";
     print "checking for polycom config name...", "\n";
     while () {
         chomp;
         $addr = $_;
         $ip   = $_;
         $addr =~ s/.* ([\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+).*/$1/;
         $addr =~ s/://g;
         $addr = lc($addr) . '.cfg';
         $ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/;
         if ( $ip eq $arpip ) {
             last;
         }
     }

     $polycomconfig = "$polycompath" . "$addr";

     unless ( -e "$polycomconfig" ) {
         print "sorry, polycom config file ", "$polycomconfig",
           " is not found.\n\n";
         exit;
     }

     return $polycomconfig;
}

sub get_extension {  # This returns the extension of the Sip
		     # phone IP address

     my @sippeers = `asterisk -rx \'sip show peers\'`;

     foreach $testline (@sippeers) {
	   $extension=$testline;
           if ( $testline =~m{$_[0]}){
             $extension =~ m{(^.*)/};
             $extension = $1
             last;
         }
     }
	print "extension: $extension\n";
     return $extension;
}

sub touch {    # We need to touch the config files or the phone
                # won't reboot - it depends on time synchronization

     print "touching config file ", $polycomconfig, "\n";
     my $now = time;
     local (*TMP);
     foreach my $file (@_) {
         utime( $now, $now, $file )
           || open( TMP, ">>$file" )
           || die ("$0: Couldn't touch file: $!\n");
     }
}

sub reboot_sip_phone {    # Send the phone a check-sync to reboot it
     $phone_ip = shift;

     $local_ip = shift;
     $sip_to   = shift;
     $sip_from = "asterisk";
     $tm       = time();
     $call_id  = $tm . "msgto$sip_to";
     $httptime = `date -R`;
     $MESG     = "NOTIFY sip:$sip_to\@$phone_ip:5060 SIP/2.0
Via: SIP/2.0/UDP $local_ip
From: 
To: 
Event: check-sync
Date: $httptime
Call-ID: $call_id\@$local_ip
CSeq: 1300 NOTIFY
Contact: 
Content-Length: 0

";

     $proto = getprotobyname('udp');
     socket( SOCKET, PF_INET, SOCK_DGRAM, $proto );
     $iaddr = inet_aton("$phone_ip");
     $paddr = sockaddr_in( 5060, $iaddr );
     bind( SOCKET, $paddr );
     $port = 5060;

     $hisiaddr = inet_aton($phone_ip);
     $hispaddr = sockaddr_in( $port, $hisiaddr );
print "SIP message sent:\n$MESG\n";
     if ( send( SOCKET, $MESG, 0, $hispaddr ) ) {
         print "reboot of phone ", "$phone_ip", "(extension:","$extension",")"," was successful", "\n";
     }
     else { print "reboot of phone ", "$phone_ip", " failed", "\n"; }

}
exit;


by bmacauley, Tuesday 12 of June, 2012 (01:51:08 UTC)
Re: Reboot script..revised

I had quite a few problems getting this script(s) to work on Asterisk-1.0.7 on Debian 3.1.
On further investigation it turned out that the regex routine in the 'get_extension' sub-routine wasn't picking up the extension number. Also the arp command on Debian is in the '/usr/sbin/ directory.

My revised script is as follows...

#!/usr/bin/perl -w

use Net::Ping;
use Socket;

$polycompath = '/home/PlcmSpIp/';    # Where you keep your polycom files
$arp         = '/usr/sbin/arp';          # Location of arp command
$sipserver   = '192.168.52.1';      # IP of asterisk server

$phone = shift;

checkphone("$phone");
touch( arp2config("$phone") );
$thisextension=get_extension($phone);

reboot_sip_phone( "$phone", "$sipserver", get_extension($phone) );

sub checkphone {                     # Checks for existence of phone,     #makes sure
         				# it's in arp table
     $activephone = shift;

     # Populate ARP table
     print "Checking ARP table.\n";
     $p = Net::Ping->new("icmp");
     if ( $p->ping( $activephone, 2 ) ) {
         print "$activephone is ";
         print "reachable.\n";
     }
     else { die "Polycom at ", $activephone, " is not reachable!"; }
     sleep(1);
     $p->close();

}

sub arp2config {    # Gets mac address from arp table, converts
                     # to a polycom config filename, makes sure
                     # the config file exists
     $arpip = shift;
     open( ARP, "$arp -an|" ) || die "Couldn't open arp table: $!\n";
     print "checking for polycom config name...", "\n";
     while () {
         chomp;
         $addr = $_;
         $ip   = $_;
         $addr =~ s/.* ([\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+).*/$1/;
         $addr =~ s/://g;
         $addr = lc($addr) . '.cfg';
         $ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/;
         if ( $ip eq $arpip ) {
             last;
         }
     }

     $polycomconfig = "$polycompath" . "$addr";

     unless ( -e "$polycomconfig" ) {
         print "sorry, polycom config file ", "$polycomconfig",
           " is not found.\n\n";
         exit;
     }

     return $polycomconfig;
}

sub get_extension {  # This returns the extension of the Sip
		     # phone IP address

     my @sippeers = `asterisk -rx \'sip show peers\'`;

     foreach $testline (@sippeers) {
	   $extension=$testline;
           if ( $testline =~m{$_[0]}){
             $extension =~ m{(^.*)/};
             last;
         }
     }
	print "extension: $extension\n";
     return $extension;
}

sub touch {    # We need to touch the config files or the phone
                # won't reboot - it depends on time synchronization

     print "touching config file ", $polycomconfig, "\n";
     my $now = time;
     local (*TMP);
     foreach my $file (@_) {
         utime( $now, $now, $file )
           || open( TMP, ">>$file" )
           || die ("$0: Couldn't touch file: $!\n");
     }
}

sub reboot_sip_phone {    # Send the phone a check-sync to reboot it
     $phone_ip = shift;

     $local_ip = shift;
     $sip_to   = shift;
     $sip_from = "asterisk";
     $tm       = time();
     $call_id  = $tm . "msgto$sip_to";
     $httptime = `date -R`;
     $MESG     = "NOTIFY sip:$sip_to\@$phone_ip:5060 SIP/2.0
Via: SIP/2.0/UDP $local_ip
From: 
To: 
Event: check-sync
Date: $httptime
Call-ID: $call_id\@$local_ip
CSeq: 1300 NOTIFY
Contact: 
Content-Length: 0

";

     $proto = getprotobyname('udp');
     socket( SOCKET, PF_INET, SOCK_DGRAM, $proto );
     $iaddr = inet_aton("$phone_ip");
     $paddr = sockaddr_in( 5060, $iaddr );
     bind( SOCKET, $paddr );
     $port = 5060;

     $hisiaddr = inet_aton($phone_ip);
     $hispaddr = sockaddr_in( $port, $hisiaddr );
print "SIP message sent:\n$MESG\n";
     if ( send( SOCKET, $MESG, 0, $hispaddr ) ) {
         print "reboot of phone ", "$phone_ip", " was successful", "\n";
     }
     else { print "reboot of phone ", "$phone_ip", " failed", "\n"; }

}
exit;



by bmacauley, Tuesday 12 of June, 2012 (01:51:22 UTC)
Easy Reboot Script

The version of asterisk I am running (Asterisk CVS-HEAD-04/27/05-08:07:15) has the capability to reboot phones built right in.

-- sip_notify.conf --

; Reboot Polycom Phone
[polycom-check-cfg]
Event=>check-sync
Content-Length=>0

; Untested (Reboot Sipura Phone)
[sipura-check-cfg]
Event=>resync
Content-Length=>0

; Untested (Reboot GrandStream Phone)
[grandstream-check-cfg]
Event=>sys-control

; Untested (Reboot Cisco Phone)
[cisco-check-cfg]
Event=>check-sync
Content-Length=>0


-- At the Asterisk Console do the following...

  • CLI> sip notify polycom-check-cfg xxx

where xxx is the peer name


Note: xxx-check-cfg are all contexts in brackets.




by TheAeroGroup, Tuesday 12 of June, 2012 (01:51:44 UTC)

Page Changes | Comments

 

Featured -

Search: