Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 20th, 2012  |  syntax: None  |  size: 3.31 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  #!/usr/bin/perl -w
  2.  
  3.  # Will simple send a SIP NOTIFY header to an IP address. Built to support Polycoms.
  4.  # Confirmed working in a Switchvox environment 11/14/2006
  5.  
  6.  $polycompath = '/tftpboot/';    # Where you keep your config files
  7.  $arp         = '/sbin/arp';          # Location of arp command
  8.  $sipserver   = '192.168.1.50';      # IP of asterisk server
  9.  
  10.  use Net::Ping;
  11.  use Socket;
  12.  
  13.  $phone = shift;
  14.  if ( ! $phone ) { print "\n\nSorry - you need to include the extension that you wish to reboot.\n\n"; exit;}
  15.  
  16.  $ip = arp2config($phone);
  17.  checkphone($ip);
  18.  reboot_sip_phone($ip, $sipserver, $phone);
  19.  
  20.  sub checkphone {
  21.      # Checks for existence of phone, makes sure
  22.      # it's in arp table
  23.      $activephone = shift;
  24.  
  25.      # Populate ARP table
  26.      print "Checking ARP table.\n";
  27.      $p = Net::Ping->new("icmp");
  28.      if ( $p->ping( $activephone, 2 ) ) {
  29.          print "$activephone is ";
  30.          print "reachable.\n";
  31.      }
  32.      else { die "Polycom at ", $activephone, " is not reachable!"; }
  33.      sleep(1);
  34.      $p->close();
  35.  }
  36.  
  37.  sub arp2config {
  38.      # Gets mac address from arp table, converts
  39.      # to a polycom config filename, makes sure
  40.      # the config file exists, matches an extension
  41.      # and returns the IP if we find a match
  42.  
  43.      my $return_ip = "";
  44.      open( ARP, "$arp -an|" ) || die "Couldn't open arp table: $!\n";
  45.      print "checking for polycom config name...", "\n";
  46.      while (<ARP>) {
  47.          chomp;
  48.         my ($j1, $ip, $j2, $addr) = split(' ');
  49.          $addr =~ s/.* ([\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+:[\d\w]+).*/$1/;
  50.          $addr =~ s/://g;
  51.          $addr = lc($addr) . '.cfg';
  52.          $ip =~ s/.*?(\d+\.\d+\.\d+\.\d+).*/$1/;
  53.         print "Found ip = $ip  @  mac = $addr \n";
  54.         my $file = $polycompath.$addr;
  55.  
  56.         # Now scan the cfg file (if it exists) and look for the phone extension within the file
  57.         if ( -e $file ) {
  58.                 if ( `grep $phone $file 2>/dev/null` ) {
  59.                         print "\nYee Haa - $file was a MATCH for $phone\n\n";
  60.                         #`touch $file`; # Not needed anymore - since firmware 2.* looks for checksum
  61.                         `perl -i -p -e 's/Date: /Date:  /' $file`;
  62.                         `perl -i -p -e 's/Date:    /Date: /' $file`;
  63.                         $return_ip = $ip;
  64.                 }
  65.         }
  66.         if ( $return_ip ) { return $return_ip; exit;}
  67.      }
  68.  }
  69.  
  70.  sub reboot_sip_phone {
  71.      # Send the phone a check-sync to reboot it
  72.      $phone_ip = shift;
  73.      $local_ip = shift;
  74.      $sip_to   = shift;
  75.      $sip_from = "asterisk";
  76.      $tm       = time();
  77.      $call_id  = $tm . "msgto$sip_to";
  78.      $httptime = `date -R`;
  79.      chomp($httptime);
  80.      $MESG     = "NOTIFY sip:$sip_to\@$phone_ip:5060 SIP/2.0
  81.  Via: SIP/2.0/UDP $local_ip
  82.  From: <sip:$sip_from\@$local_ip>
  83.  To: <sip:$sip_to\@$phone_ip>
  84.  Event: check-sync
  85.  Date: $httptime
  86.  Call-ID: $call_id\@$local_ip
  87.  CSeq: 102 NOTIFY
  88.  Contact: <sip:$sip_from\@$local_ip>
  89.  Content-Length: 0
  90.  
  91.  ";
  92.  
  93.      $proto = getprotobyname('udp');
  94.      socket( SOCKET, PF_INET, SOCK_DGRAM, $proto );
  95.      $iaddr = inet_aton("$phone_ip");
  96.      $paddr = sockaddr_in( 5060, $iaddr );
  97.      bind( SOCKET, $paddr );
  98.      $port = 5060;
  99.  
  100.      $hisiaddr = inet_aton($phone_ip);
  101.      $hispaddr = sockaddr_in( $port, $hisiaddr );
  102.  
  103.      if ( send( SOCKET, $MESG, 0, $hispaddr ) ) {
  104.          print "reboot of phone ", "$phone_ip", " was successful", "\n";
  105.      } else {
  106.         print "reboot of phone ", "$phone_ip", " failed", "\n";
  107.      }
  108.  
  109.  #print "This was the SIP header:\n\n".$MESG."\n\n";
  110.  
  111.  }