Skip to content

Instantly share code, notes, and snippets.

@iloveitaly
Created February 18, 2012 20:44
Show Gist options
  • Save iloveitaly/1860966 to your computer and use it in GitHub Desktop.
Save iloveitaly/1860966 to your computer and use it in GitHub Desktop.
Some tools for managing polycom phones + asterisk systems
# this assumes you have a default directory defined
find /tftpboot/logs/ | ack 'logs/([^-]+)' --output '$1' | sed '/^$/d' | while read mac
do
cp -f "000000000000-directory.xml" "$mac-directory.xml"
done
#!/bin/bash
# Reboots all polycom phones using the polycom-reboot.pl script
# originally found here: http://www.voip-info.org/storage/users/892/38892/images/1157/polycom_reboot.pl.txt
# sed '1,1d' - removes first line
arp | sed '1,1d' | while read line
do
ip=`echo $line | awk '{print $1}'`
mac=`echo $line | awk '{print $3}' | sed 's/://g' | tr '[:upper:]' '[:lower:]'`
if [[ -e /tftpboot/$mac.cfg ]]; then
perl ~/reboot-polycom.pl "$mac"
fi
done
exit 0
# one liner
arp | sed '1,1d' | while read line; do; ip=`echo $line | awk '{print $1}'`; mac=`echo $line | awk '{print $3}' | sed 's/://g' | tr '[:upper:]' '[:lower:]'`; if [[ -e /tftpboot/$mac.cfg ]]; then; perl ~/reboot-polycom.pl "$mac"; fi; done
#!/usr/bin/perl -w
# Will simple send a SIP NOTIFY header to an IP address. Built to support Polycoms.
# Confirmed working in a Switchvox environment 11/14/2006
$polycompath = '/tftpboot/'; # Where you keep your config files
$arp = '/sbin/arp'; # Location of arp command
$sipserver = '192.168.1.50'; # IP of asterisk server
use Net::Ping;
use Socket;
$phone = shift;
if ( ! $phone ) { print "\n\nSorry - you need to include the extension that you wish to reboot.\n\n"; exit;}
$ip = arp2config($phone);
checkphone($ip);
reboot_sip_phone($ip, $sipserver, $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, matches an extension
# and returns the IP if we find a match
my $return_ip = "";
open( ARP, "$arp -an|" ) || die "Couldn't open arp table: $!\n";
print "checking for polycom config name...", "\n";
while (<ARP>) {
chomp;
my ($j1, $ip, $j2, $addr) = split(' ');
$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/;
print "Found ip = $ip @ mac = $addr \n";
my $file = $polycompath.$addr;
# Now scan the cfg file (if it exists) and look for the phone extension within the file
if ( -e $file ) {
if ( `grep $phone $file 2>/dev/null` ) {
print "\nYee Haa - $file was a MATCH for $phone\n\n";
#`touch $file`; # Not needed anymore - since firmware 2.* looks for checksum
`perl -i -p -e 's/Date: /Date: /' $file`;
`perl -i -p -e 's/Date: /Date: /' $file`;
$return_ip = $ip;
}
}
if ( $return_ip ) { return $return_ip; exit;}
}
}
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`;
chomp($httptime);
$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: 102 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";
}
#print "This was the SIP header:\n\n".$MESG."\n\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment