[Asterisk-Users] Polycom Reboot Script - Please wiki-size me

John Baker johnb at listbrokers.com
Thu Jan 22 23:00:30 MST 2004


Oops, the cut and paste got the better of me.

This one might be a little better...

#!/usr/bin/perl
#
# PolyReboot.pl
#
# Reboots a Polycom 500 or 600 phone
#
use Net::Ping;
use Socket;

$polycompath = '/home/poly/';    # Where you keep your config files
$arp         = '/sbin/arp';          # Location of arp command
$sipserver   = '192.168.XXX.XXX';      # IP of asterisk server

$phone = shift;

checkphone("$phone");
touch( arp2config("$phone") );

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

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

     # ARP table needs our phone
     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 (<ARP>) {
         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 touch {    # We need to touch the config file 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 = "0";
     $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: <sip:$sip_from\@$local_ip>
 To: <sip:$sip_to\@$phone_ip>
 Event: check-sync
 Date: $httptime
 Call-ID: $call_id\@$local_ip
 CSeq: 1300 NOTIFY
 Contact: <sip:$sip_from\@$local_ip>
 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 );

     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;

----- Original Message ----- 
From: "John Baker" <johnb at listbrokers.com>
To: <asterisk-users at lists.digium.com>
Sent: Thursday, January 22, 2004 11:38 PM
Subject: [Asterisk-Users] Polycom Reboot Script - Please wiki-size me


> With my thanks to Brian West and his offering in the thread,
>
> "Subject: Re: [Asterisk-Users] Remote reload Cisco 7960"
>
> I offer PolyReboot.pl, a perl script for rebooting Polycom IP Phones
>
> PolyReboot.pl takes an IP address as a single argument and reboots the
> phone.
>
> You must have a cfg file in the Polycom style, i.e., 00ab00cd00ef.cfg -
all
> lower case.  Further,
> you need to use ftp for your remote cfg loading, because with ftp
> synchronization, the phones
> check the ctimes of the cfg files against what's in their cache when
> receiving a check-sync in
> order to determine if rebooting is necessary.
>
> I tested this script.  It works on my Polycoms.  I have the latest bootrom
> and the
> SIP 1.1.0 software version.
>
> I'm not much of a perl person, and all of this code is heavily borrowed,
> (Thanks again, Brian), so
> someone else can prettify it as they see fit.
>
> #!/usr/bin/perl
> #
> # PolyReboot.pl
> #
> # Reboots a Polycom 500 or 600 phone
> #
> use Net::Ping;
> use Socket;
>
> $polycompath = '/home/poly/';    # Where you keep your config files
> $arp         = '/sbin/arp';          # Location of arp command
> $sipserver   = '192.168.XXX.XXX';      # IP of asterisk server
>
> $phone = shift;
>
> checkphone("$phone");
> touch( arp2config("$phone") );
>
> reboot_sip_phone( "$phone", "$sipserver", "Reboot" );
>
> sub checkphone {  # Checks for existence of phone, makes sure
>                             # it's in arp table
>     $activephone = shift;
>
>     # ARP table needs our phone
>     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 (<ARP>) {
>         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 touch {    # We need to touch the config file 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 = "0";
>     $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: <sip:$sip_from\@$local_ip>
> To: <sip:$sip_to\@$phone_ip>
> Event: check-sync
> Date: $httptime
> Call-ID: $call_id\@$local_ip
> CSeq: 1300 NOTIFY
> Contact: <sip:$sip_from\@$local_ip>
> 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 );
>
>     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;
>
> Enjoy,
>
> John
>
> _______________________________________________
> Asterisk-Users mailing list
> Asterisk-Users at lists.digium.com
> http://lists.digium.com/mailman/listinfo/asterisk-users
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-users
>




More information about the asterisk-users mailing list