[BACK]Return to Trango.pm CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / lib / Net / Telnet

Diff for /trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm between version 1.8 and 1.16

version 1.8, 2006/06/29 01:39:52 version 1.16, 2006/09/07 04:00:07
Line 1 
Line 1 
 package Net::Telnet::Trango;  package Net::Telnet::Trango;
 # $RedRiver: Trango.pm,v 1.7 2006/06/28 22:33:18 andrew Exp $  # $RedRiver: Trango.pm,v 1.14 2006/09/07 02:49:34 andrew Exp $
 use strict;  use strict;
 use warnings;  use warnings;
 use base 'Net::Telnet';  use base 'Net::Telnet';
   
 =pod  =pod
   
 =head1 NAME  =head1 NAME
   
 Net::Telnet::Trango - Perl extension for accessing the Trango telnet interface  Net::Telnet::Trango - Perl extension for accessing the Trango telnet interface
   
 =head1 SYNOPSIS  =head1 SYNOPSIS
   
   use Net::Telnet::Trango;    use Net::Telnet::Trango;
   my $t = new Net::Telnet::Trango ( Timeout => 5 );    my $t = new Net::Telnet::Trango ( Timeout => 5 );
     
   $t->open( Host => $fox ) or die "Error connecting: $!";    $t->open( Host => $fox ) or die "Error connecting: $!";
   
   $t->login('password') or die "Couldn't log in: $!";    $t->login('password') or die "Couldn't log in: $!";
     
   # Do whatever    # Do whatever
     
   $t->exit;    $t->exit;
   $t->close;    $t->close;
   
 =head1 DESCRIPTION  =head1 DESCRIPTION
   
 Perl access to the telnet interface on Trango Foxes, SUs and APs.  Perl access to the telnet interface on Trango Foxes, SUs and APs.
   
 Another handy feature is that it will parse the output from certain commands that is in the format "[key1] value1 [key2] value2" and put those in a hashref that is returned.  This makes using the output from things like sysinfo very easy to do.  Another handy feature is that it will parse the output from certain commands that is in the format "[key1] value1 [key2] value2" and put those in a hashref that is returned.  This makes using the output from things like sysinfo very easy to do.
   
 =head2 EXPORT  =head2 EXPORT
   
 None  None
   
 =cut  =cut
   
 our $VERSION = '0.01';  our $VERSION = '0.01';
   
 my %PRIVATE = (  my %PRIVATE = (
   is_connected => 0,    is_connected => 0,
   logged_in => 0,    logged_in => 0,
 );  );
   
 =pod  =pod
   
 =item new  =item new
   
 Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'  Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'
   
 =cut  It also takes an optional parameter 'Decode'.  If not defined it
   defaults to 1, if it is set to 0, it will not decode the output and
 sub new  instead return an array of the lines that were returned from the
 {  command.
   my $class = shift;  
   =cut
   my %args;  
   if (@_ == 1) {  sub new
     $args{'Host'} = shift;  {
   } else {    my $class = shift;
     %args = @_;  
   }    my %args;
     if (@_ == 1) {
   $args{'Prompt'}  ||= '/#> *$/';      $args{'Host'} = shift;
     } else {
   foreach my $key (keys %args) {      %args = @_;
     $PRIVATE{$key} = $args{$key};    }
   }  
     $args{'Prompt'}  ||= '/#> *$/';
   my $self = $class->SUPER::new(%args);  
   bless $self if ref $self;    foreach my $key (keys %args) {
       $PRIVATE{$key} = $args{$key};
   return $self;    }
 }    $PRIVATE{'Decode'} = 1 unless defined $PRIVATE{'Decode'};
     delete $args{'Decode'};
 #  _password <new password> <new password>  
 #  ? [command]    my $self = $class->SUPER::new(%args);
 #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...    bless $self if ref $self;
 #  arp -bcast <on|off>  
 #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...    return $self;
 #  bye  }
 #  cf2cf ap [default|<size>]  
 #  date  #  _password <new password> <new password>
 #  date <month> <day> <year>  #  ? [command]
 #  freq scantable  #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...
 #  freq channeltable  #  arp -bcast <on|off>
 #  freq writescan [<ch#> <h|v>]  #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...
 #  freq writechannel [<ch#> <freq>] ...  #  bye
 #  freq <ch #> <h|v>  #  cf2cf ap [default|<size>]
 #  help [command]  #  date
 #  heater [<on temp> <off temp>]  #  date <month> <day> <year>
 #  ipconfig [<new ip> <new subnet mask> <new gateway>]  #  freq scantable
 #  log [<# of entries, 1..179>]  #  freq channeltable
 #  log <sum> <# of entries, 1..179>  #  freq writescan [<ch#> <h|v>]
 #  logout  #  freq writechannel [<ch#> <freq>] ...
 #  opmode [ap [y]]  #  freq <ch #> <h|v>
 #  password  #  help [command]
 #  ping <ip addr>  #  heater [<on temp> <off temp>]
 #  polar <h|v>  #  ipconfig [<new ip> <new subnet mask> <new gateway>]
 #  power <setism|setunii> <max|min|<dBm>>  #  log [<# of entries, 1..179>]
 #  reboot  #  log <sum> <# of entries, 1..179>
 #  restart  #  logout
 #  remarks [<str>]  #  opmode [ap [y]]
 #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  password
 #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  ping <ip addr>
 #  sysinfo  #  polar <h|v>
 #  set suid <id>  #  power <setism|setunii> <max|min|<dBm>>
 #  set apid <id>  #  reboot
 #  set baseid <id>  #  restart
 #  set defaultopmode [<ap|su> <min,0..10>]  #  remarks [<str>]
 #  set defaultopmode off  #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  set snmpcomm [<read | write | trap (id or setall)> <str>]  #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  set mir [on|off]  #  sysinfo
 #  set mir threshold <kbps>  #  set suid <id>
 #  set rssitarget [<ism|unii> <dBm>]  #  set apid <id>
 #  set serviceradius [<ism | unii> <miles>]  #  set baseid <id>
 #  ssrssi <ch #> <h|v>  #  set defaultopmode [<ap|su> <min,0..10>]
 #  su [<suid>|all]  #  set defaultopmode off
 #  su changechannel <all|suid> <ch#> <h|v>  #  set snmpcomm [<read | write | trap (id or setall)> <str>]
 #  su ipconfig <suid> <new ip> <new subnet> <new gateway>  #  set mir [on|off]
 #  su [live|poweroff|priority]  #  set mir threshold <kbps>
 #  su <ping|info|status> <suid>  #  set rssitarget [<ism|unii> <dBm>]
 #  su powerleveling <all|suid>  #  set serviceradius [<ism | unii> <miles>]
 #  su reboot <all|suid>  #  ssrssi <ch #> <h|v>
 #  su restart <all|suid>  #  su [<suid>|all]
 #  su testrflink <all|suid> [r]  #  su changechannel <all|suid> <ch#> <h|v>
 #  su testrflink <setlen> [64..1600]  #  su ipconfig <suid> <new ip> <new subnet> <new gateway>
 #  su testrflink <aptx> [20..100]  #  su [live|poweroff|priority]
 #  su sw <suid|all> <sw #> <on|off>  #  su <ping|info|status> <suid>
 #  sudb [dload | view]  #  su powerleveling <all|suid>
 #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>  #  su reboot <all|suid>
 #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>  #  su restart <all|suid>
 #  sudb delete <all|<suid>>  #  su testrflink <all|suid> [r]
 #  sudb modify <suid> <cir|mir> <kbps>  #  su testrflink <setlen> [64..1600]
 #  sudb modify <suid> <su2su> <group id,hex>  #  su testrflink <aptx> [20..100]
 #  sudb view  #  su sw <suid|all> <sw #> <on|off>
 #  sulog [lastmins | sampleperiod <1..60>]  #  sudb [dload | view]
 #  sulog [<# of entry,1..18>]  #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>
 #  survey <ism|unii> <time, sec> <h|v>  #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>
 #  sw [<sw #> <on|off>]  #  sudb delete <all|<suid>>
 #  temp  #  sudb modify <suid> <cir|mir> <kbps>
 #  tftpd [on|off]  #  sudb modify <suid> <su2su> <group id,hex>
 #  time  #  sudb view
 #  time <hour> <min> <sec>  #  sulog [lastmins | sampleperiod <1..60>]
 #  save <mainimage|fpgaimage> <current chksum> <new chksum>  #  sulog [<# of entry,1..18>]
 #  save <systemsetting|sudb>  #  survey <ism|unii> <time, sec> <h|v>
 #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>  #  sw [<sw #> <on|off>]
 #  updateflash <systemsetting|sudb>  #  temp
   #  tftpd [on|off]
 =pod  #  time
   #  time <hour> <min> <sec>
 =head1 METHODS  #  save <mainimage|fpgaimage> <current chksum> <new chksum>
   #  save <systemsetting|sudb>
 =head2 ACCESSORS  #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>
   #  updateflash <systemsetting|sudb>
 =over  
   =pod
 =item Host  
   =head1 METHODS
 returns the name of the host that you are accessing  
   =head2 ACCESSORS
 =item firmware_version  
   =over
 returns the firmware version on the trango if available otherwise undef.    
 Available after a successful open()  =item Host
 This is usually only set internally  
   returns the name of the host that you are accessing
 =item host_type  
   =item firmware_version
 returns the type of host from the login banner for example M5830S or M5300S.    
 Available after a successful open()  returns the firmware version on the trango if available otherwise undef.
 This is usually only set internally  Available after a successful open()
   This is usually only set internally
 =item is_connected  
   =item host_type
 returns 1 after a successful open() otherwise undef  
 This is usually only set internally  returns the type of host from the login banner for example M5830S or M5300S.
   Available after a successful open()
 =item logged_in  This is usually only set internally
   
 returns 1 after a successful login() 0 if it failed and undef if  =item is_connected
 login() was never called  
 This is usually only set internally  returns 1 after a successful open() otherwise undef
   This is usually only set internally
 =item login_banner  
   =item logged_in
 returns the banner that is displayed when first connected at login.  Only set after a successful open()  
   returns 1 after a successful login() 0 if it failed and undef if
 This is usually only set internally  login() was never called
   This is usually only set internally
 =item last_lines  
   =item login_banner
 returns the output from the last cmd() that was run as an array ref  
 This is usually only set internally  returns the banner that is displayed when first connected at login.  Only set after a successful open()
   
 =back  This is usually only set internally
   
 =head2 ALIASES  =item last_lines
   
 =over  returns the output from the last cmd() that was run as an array ref
   This is usually only set internally
 =item bye  
   =back
 alias of exit()  
   =head2 ALIASES
 =item restart  
   =over
 alias of reboot()  
   =item bye
 =back  
   alias of exit()
 =head2 COMMANDS  
   =item restart
 Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>, as such they accept the same options as C<cmd()>.  Specifically they take a named paramater "args", for example:  
 C<tftpd(args =E<gt> 'on')> would enable tftpd  alias of reboot()
   
 =over  =back
   
 =item tftpd  =head2 COMMANDS
   
 Returns a hash ref of the decoded output from the command.  Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>, as such they accept the same options as C<cmd()>.  Specifically they take a named paramater "args", for example:
   C<tftpd(args =E<gt> 'on')> would enable tftpd
 Also see enable_tftpd() and disable_tftpd() as those check for correct output  
   =over
 =item ver  
   =item tftpd
 Returns a hash ref of the decoded output from the command.  
   Returns a hash ref of the decoded output from the command.
 =item sysinfo  
   Also see enable_tftpd() and disable_tftpd() as those check for correct output
 Returns a hash ref of the decoded output from the command.  
   =item ver
 =item exit  
   Returns a hash ref of the decoded output from the command.
 exits the command session with the trango and closes the connection  
   =item sysinfo
 =item reboot  
   Returns a hash ref of the decoded output from the command.
 reboots the trango and closes the connection  
   =item exit
 =item sulog  
   exits the command session with the trango and closes the connection
 returns an array ref of hashes containing each log line.  
   =item reboot
 =item save_sudb  
   reboots the trango and closes the connection
 returns true on success, undef on failure  
   =item sulog
 =cut  
   returns an array ref of hashes containing each log line.
   
 my $success = 'Success.';  =item save_sudb
 my %COMMANDS = (  
   tftpd       => { decode => 'all',   expect => $success },  returns true on success, undef on failure
   ver         => { decode => 'all' },  
   sysinfo     => { decode => 'all',   expect => $success },  =item syslog
   updateflash => { decode => 'all',   expect => $success },  
   sulog       => { decode => 'sulog', expect => $success },  returns the output from the syslog command
   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },  
   reboot      => { no_prompt => 1, cmd_disconnects => 1 },  =item pipe
   save_sudb   => { String => "save sudb", expect => $success },  
   #su password???  returns the output from the pipe command
   #_bootloader  
   #temp  =item maclist
   #heater  
 );  returns the output from the maclist command
   
 my %ALIASES = (  =item maclist_reset
   bye     => 'exit',  
   restart => 'reboot',  resets the maclist.  No useful output.
 );  
   =item eth_list
 my %ACCESS = map { $_ => 1 } qw(  
   firmware_version  returns the output from the eth list command
   host_type  
   Host  =cut
   is_connected  
   logged_in  
   login_banner  my $success = 'Success.';
   Timeout  my %COMMANDS = (
   last_lines    tftpd       => { decode => 'all',   expect => $success },
   last_vals    ver         => { decode => 'all' },
 );    sysinfo     => { decode => 'all',   expect => $success },
     updateflash => { decode => 'all',   expect => $success },
 sub AUTOLOAD    sulog       => { decode => 'sulog', expect => $success },
 {    'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
   my $self = shift;    reboot      => { no_prompt => 1, cmd_disconnects => 1 },
     save_sudb   => { String => 'save sudb', expect => $success },
   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/    syslog      => { expect => $success },
     or die "Weird: $AUTOLOAD";    'pipe'      => { }, # XXX needs a special decode
     maclist     => { decode => 'maclist' },
   if (exists $ALIASES{$method}) {    maclist_reset => { String => 'maclist reset', expect => 'done' },
     $method = $ALIASES{$method};    eth_link    => { String => 'eth link', expect => $success },
     return $self->$method(@_);    # eth r, w and reset???
   }    #su password???
     #_bootloader
   if (exists $COMMANDS{$method}) {    #temp
     $method = shift if (@_ == 1);    #heater
     $COMMANDS{$method}{'String'} ||= $method;  );
     return $self->cmd(%{ $COMMANDS{$method} }, @_);  
   }  my %ALIASES = (
     bye     => 'exit',
   if (exists $ACCESS{$method}) {    restart => 'reboot',
     my $prev = $PRIVATE{$method};  );
     ($PRIVATE{$method}) = @_ if @_;  
     return $prev;  my %ACCESS = map { $_ => 1 } qw(
   }    firmware_version
     host_type
   $method = "SUPER::$method";    Host
   return $self->$method(@_);    is_connected
 }    logged_in
     login_banner
 =pod    Timeout
     last_lines
 =item open    last_vals
   );
 Calls Net::Telnet::open() then makes sure you get a password prompt so you are ready to login() and parses the login banner so you can get host_type() and firmware_version()  
   sub AUTOLOAD
 =cut  {
     my $self = shift;
 sub open  
 {    my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
   my $self = shift;      or die "Weird: $AUTOLOAD";
   
   unless ( $self->SUPER::open(@_) ) {    if (exists $ALIASES{$method}) {
     #$! = "Couldn't connect to " . $self->Host . ":  $!";      $method = $ALIASES{$method};
     return undef;      return $self->$method(@_);
   }    }
   
   ## Get to login prompt    if (exists $COMMANDS{$method}) {
   unless ($self->waitfor(      $method = shift if (@_ == 1);
       -match => '/password: ?$/i',      $COMMANDS{$method}{'String'} ||= $method;
       -errmode => "return",      return $self->cmd(%{ $COMMANDS{$method} }, @_);
     ) ) {    }
     #$! = "problem connecting to host (" . $self->Host . "): " .  
     #    $self->lastline;    if (exists $ACCESS{$method}) {
     return undef;      my $prev = $PRIVATE{$method};
   }      ($PRIVATE{$method}) = @_ if @_;
       return $prev;
   $self->parse_login_banner($self->lastline);    }
   
   $self->is_connected(1);    $method = "SUPER::$method";
     return $self->$method(@_);
   return $self->is_connected;  }
 }  
   =pod
 =pod  
   =item open
 =item login  
   Calls Net::Telnet::open() then makes sure you get a password prompt so you are ready to login() and parses the login banner so you can get host_type() and firmware_version()
 Calls open() if not already connected, then sends the password and sets logged_in() if successful  
   =cut
 =cut  
   sub open
 sub login  {
 {    my $self = shift;
   my $self = shift;  
     unless ( $self->SUPER::open(@_) ) {
   unless ($self->is_connected) {      #$! = "Couldn't connect to " . $self->Host . ":  $!";
     $self->open or return undef;      return undef;
   }    }
   
   my $password = shift;    ## Get to login prompt
     unless ($self->waitfor(
   $self->print($password);        -match => '/password: ?$/i',
   unless ($self->waitfor(        -errmode => "return",
     -match => $self->prompt,      ) ) {
     -errmode => "return",      #$! = "problem connecting to host (" . $self->Host . "): " .
   ) ) {      #    $self->lastline;
     #$! = "login ($self->Host) failed: " . $self->lastline;      return undef;
     return undef;    }
   }  
     $self->parse_login_banner($self->lastline);
   $self->logged_in(1);  
     $self->is_connected(1);
   return $self->logged_in;  
 }    return $self->is_connected;
   }
 =pod  
   =pod
 =item parse_login_banner  
   =item login
 Takes a login banner (what you get when you first connect to the Trango) or reads what is already in login_banner() then parses it and sets host_type() and firmware_version() as well as login_banner()  
   Calls open() if not already connected, then sends the password and sets logged_in() if successful
 =cut  
   =cut
 sub parse_login_banner  
 {  sub login
   my $self = shift;  {
     my $self = shift;
   if (@_) {  
     $self->login_banner(@_);    unless ($self->is_connected) {
   }      $self->open or return undef;
     }
   my $banner = $self->login_banner;  
     my $password = shift;
   my ($type, $ver) = $banner =~  
     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;    $self->print($password);
     unless ($self->waitfor(
   $self->login_banner($banner);      -match => $self->prompt,
   $self->host_type($type);      -errmode => "return",
   $self->firmware_version($ver);    ) ) {
       #$! = "login ($self->Host) failed: " . $self->lastline;
   return 1;      return undef;
 }    }
   
 =pod    $self->logged_in(1);
   
 =item su_password    return $self->logged_in;
   }
 C<su_password('all'|suid, 'new_password')>  
   =pod
 =cut  
   =item parse_login_banner
 sub su_password  
 {  Takes a login banner (what you get when you first connect to the Trango) or reads what is already in login_banner() then parses it and sets host_type() and firmware_version() as well as login_banner()
   my $self     = shift;  
   my $su       = shift || '!';  =cut
   my $new_pass = shift || '';  
   sub parse_login_banner
   unless (defined $su) {  {
     warn "No su passed!"    my $self = shift;
     #return undef;  
   }    if (@_) {
       $self->login_banner(@_);
   unless (defined $new_pass) {    }
     warn "No new password!"    
     #return undef;    my $banner = $self->login_banner;
   }  
     my ($type, $ver) = $banner =~
   return $self->cmd(String => 'su password ' .      /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
                      $su . ' ' .  
                      $new_pass . ' ' .    $self->login_banner($banner);
                      $new_pass,    $self->host_type($type);
                      expect => $success,    $self->firmware_version($ver);
                     );  
 }    return 1;
   }
   
 =pod  =pod
   
 =item sudb_view  =item su_password
   
 returns a reference to an array of hashes each containing:  C<su_password('all'|suid, 'new_password')>
   
   suid  =cut
   type  
   cir  sub su_password
   mir  {
   mac    my $self     = shift;
     my $su       = shift || '!';
 =cut    my $new_pass = shift || '';
   
 sub sudb_view    unless (defined $su) {
 {      warn "No su passed!"
   my $self = shift;      #return undef;
     }
   my @lines = $self->cmd( String => 'sudb view', expect => $success );  
     unless (defined $new_pass) {
   return undef unless @lines;      warn "No new password!"
       #return undef;
   my @sus;    }
   foreach (@lines) {  
     if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {    return $self->cmd(String => 'su password ' .
       my %s = (                       $su . ' ' .
         suid => $1,                       $new_pass . ' ' .
         type => $2,                       $new_pass,
         cir  => $3,                       expect => $success,
         mir  => $4,                      );
         mac  => $5,  }
       );  
   =pod
           $s{'mac'} =~ s/\s//g;  
           $s{'mac'} = uc($s{'mac'});  =item su_ipconfig
   
       push @sus, \%s;  C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
     }  
   }  =cut
   
   return \@sus;  sub su_ipconfig
 }  {
           my $self        = shift;
 =pod  
           my $suid        = shift;
 =item sudb_add          my $new_ip      = shift;
           my $new_subnet  = shift;
 Takes the following paramaters          my $new_gateway = shift;
   
         suid : numeric,          return undef unless $suid =~ /^\d+$/;
         type : (reg|pr)          return undef unless $new_ip;
         cir  : numeric,          return undef unless $new_subnet;
         mir  : numeric,          return undef unless $new_gateway;
         mac  : Almost any format, it will be reformatted,  
           # su ipconfig <suid> <new ip> <new subnet> <new gateway>
 and returns true on success or undef otherwise.          return $self->cmd(String => 'su ipconfig ' .
                        $suid       . ' ' .
 You should save_sudb() after calling this, or your changes  will be lost                       $new_ip     . ' ' .
 when the AP is rebooted.                       $new_subnet . ' ' .
                        $new_gateway,
 =cut                       expect => $success,
                       );
 sub sudb_add  }
 {  
         my $self = shift;  =pod
         my $suid = shift;  
         my $type = shift;  =item sudb_view
         my $cir  = shift;  
         my $mir  = shift;  returns a reference to an array of hashes each containing:
         my $mac  = shift;  
     suid
         if ($suid =~ /\D/) {    type
                 return undef;    cir
         }    mir
     mac
         unless (lc($type) eq 'reg' || lc($type) eq 'pr') {  
                 warn "Invalid type '$type'!";  =cut
                 return undef;  
         }  sub sudb_view
   {
         if ($cir =~ /\D/) {    my $self = shift;
                 warn "Invalid CIR '$cir'!";  
                 return undef;    my @lines = $self->cmd( String => 'sudb view', expect => $success );
         }  
     return undef unless @lines;
         if ($mir =~ /\D/) {  
                 warn "Invalid MIR '$mir'!";    unless ($PRIVATE{'Decode'}) {
                 return undef;      return @lines;
         }    }
   
         my $new_mac = $mac;    my @sus;
         $new_mac =~ s/[^0-9A-Fa-f]//;    foreach (@lines) {
         unless (length $new_mac == 12) {      next unless $_;
                 warn "Invalid MAC '$mac'!";      if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
                 return undef;        my %s = (
         }          suid => $1,
         $new_mac = join ' ', $new_mac =~ /../g;          type => $2,
           cir  => $3,
         my $string = 'sudb add ' .          mir  => $4,
                 $suid . ' ' .          mac  => $5,
                 $type . ' ' .        );
                 $cir  . ' ' .  
                 $mir  . ' ' .            $s{'mac'} =~ s/\s//g;
                 $new_mac;            $s{'mac'} = uc($s{'mac'});
   
         push @sus, \%s;
         return $self->cmd( String => $string, expect => $success );      }
 }    }
   
 =pod    return \@sus;
   }
 =item sudb_delete  
   =pod
 Takes either 'all' or the  suid of the su to delete  
 and returns true on success or undef otherwise.  =item sudb_add
   
 You should save_sudb() after calling this, or your changes  will be lost  Takes the following paramaters
 when the AP is rebooted.  
           suid : numeric,
 =cut          type : (reg|pr)
           cir  : numeric,
 sub sudb_delete          mir  : numeric,
 {          mac  : Almost any format, it will be reformatted,
         my $self = shift;  
         my $suid = shift;  and returns true on success or undef otherwise.
   
         if (lc($suid) ne 'all' || $suid =~ /\D/) {  You should save_sudb() after calling this, or your changes  will be lost
                 return undef;  when the AP is rebooted.
         }  
   =cut
         return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );  
 }  sub sudb_add
   {
           my $self = shift;
 =pod          my $suid = shift;
           my $type = shift;
 =item sudb_modify          my $cir  = shift;
           my $mir  = shift;
 Takes either the  suid of the su to delete          my $mac  = shift;
 as well as what you are changing, either "cir, mir or su2su"  
 and returns true on success or undef otherwise.          if ($suid =~ /\D/) {
                   return undef;
 cir and mir also take a value to set the cir/mir to.          }
   
 su2su takes a group id parameter that is in hex.          unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
                   warn "Invalid type '$type'!";
 You should save_sudb() after calling this, or your changes  will be lost                  return undef;
 when the AP is rebooted.          }
   
 =cut          if ($cir =~ /\D/) {
                   warn "Invalid CIR '$cir'!";
 sub sudb_modify                  return undef;
 {          }
         my $self  = shift;  
         my $suid  = shift;          if ($mir =~ /\D/) {
         my $opt   = shift;                  warn "Invalid MIR '$mir'!";
         my $value = shift;                  return undef;
           }
         if ($suid =~ /\D/) {  
                 return undef;          my $new_mac = $mac;
         }          $new_mac =~ s/[^0-9A-Fa-f]//;
           unless (length $new_mac == 12) {
         if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {                  warn "Invalid MAC '$mac'!";
                 if ($value =~ /\D/) {                  return undef;
                         return undef;          }
                 }          $new_mac = join ' ', $new_mac =~ /../g;
         } elsif (lc($opt) eq 'su2su') {  
                 if ($value =~ /[^0-9A-Za-f]/) {          my $string = 'sudb add ' .
                         return undef;                  $suid . ' ' .
                 }                  $type . ' ' .
         } else {                  $cir  . ' ' .
                 return undef;                  $mir  . ' ' .
         }                  $new_mac;
   
         my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;  
           return $self->cmd( String => $string, expect => $success );
         return $self->cmd( String => $string, expect => $success );  }
 }  
   =pod
 =pod  
   =item sudb_delete
 =item enable_tftpd  
   Takes either 'all' or the  suid of the su to delete
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing  and returns true on success or undef otherwise.
   
 =cut  You should save_sudb() after calling this, or your changes  will be lost
   when the AP is rebooted.
 sub enable_tftpd  
 {  =cut
   my $self = shift;  
   sub sudb_delete
   my $vals = $self->tftpd( args => 'on' );  {
           my $self = shift;
   if ($vals->{'Tftpd'} eq 'listen') {          my $suid = shift;
     return $vals;  
   } else {          if (lc($suid) ne 'all' || $suid =~ /\D/) {
     return undef;                  return undef;
   }          }
 }  
           return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
 =pod  }
   
 =item disable_tftpd  =pod
   
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'  =item sudb_modify
   
 =cut  Takes either the  suid of the su to delete
   as well as what you are changing, either "cir, mir or su2su"
 sub disable_tftpd  and returns true on success or undef otherwise.
 {  
   my $self = shift;  cir and mir also take a value to set the cir/mir to.
   
   my $vals = $self->tftpd( args => 'off' );  su2su takes a group id parameter that is in hex.
   
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {  You should save_sudb() after calling this, or your changes  will be lost
     return $vals;  when the AP is rebooted.
   } else {  
     return undef;  =cut
   }  
 }  sub sudb_modify
   {
 =pod          my $self  = shift;
           my $suid  = shift;
 =item cmd          my $opt   = shift;
           my $value = shift;
 This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.  
           if ($suid =~ /\D/) {
 Normally returns the last lines from from the command                  return undef;
           }
 Also accepts these options:  
           if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
 I<decode>                  if ($value =~ /\D/) {
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output                          return undef;
                   }
 I<cmd_disconnects>          } elsif (lc($opt) eq 'su2su') {
 - if this is true, it then sets logged_in() to false, then it will close() the connection and then sets is_connected() to false                  if ($value =~ /[^0-9A-Za-f]/) {
                           return undef;
 I<expect>                  }
 - if this is set (usually to 'Success.') it will check for that in the last line of output and if it does not, will return undef because the command probably failed          } else {
                   return undef;
 I<args>          }
 - a string containing the command line options that are passed to the command  
           my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
 =cut  
           return $self->cmd( String => $string, expect => $success );
 sub cmd  }
 {  
   my $self = shift;  =pod
   
   my @valid_net_telnet_opts = qw(  =item enable_tftpd
     String  
     Output  runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
     Cmd_remove_mode  
     Errmode  =cut
     Input_record_separator  
     Ors  sub enable_tftpd
     Output_record_separator  {
     Prompt    my $self = shift;
     Rs  
     Timeout    my $vals = $self->tftpd( args => 'on' );
   );  
     if ($vals->{'Tftpd'} eq 'listen') {
   my %cfg;      return $vals;
   if (@_ == 2) {    } else {
     $cfg{'String'} = shift;      return undef;
   } elsif (@_ > 2) {    }
     %cfg = @_;  }
   }  
   =pod
   $cfg{'Timeout'} ||= $self->Timeout;  
   =item disable_tftpd
   unless ($cfg{'String'}) {  
     #$! = "No command passed";  runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
     #warn "No command passed\n";  
     return undef;  =cut
   }  
   sub disable_tftpd
   unless ($self->is_connected) {  {
     #$! = "Not connected";    my $self = shift;
     #warn "Not connected\n";  
     return undef;    my $vals = $self->tftpd( args => 'off' );
   }  
     if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
   unless ($self->logged_in) {      return $vals;
     #$! = "Not logged in";    } else {
     #warn "Not logged in\n";      return undef;
     return undef;    }
   }  }
   
   =pod
   my %cmd;  
   foreach (@valid_net_telnet_opts) {  =item cmd
     if (exists $cfg{$_}) {  
       $cmd{$_} = $cfg{$_};  This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.
     }  
   }  Normally returns the last lines from from the command
   if ($cfg{'args'}) {  
     $cmd{'String'} .= ' ' . $cfg{'args'};  Also accepts these options:
   }  
   my @lines;  I<decode>
   unless ($cfg{'no_prompt'}) {  - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output
     @lines = $self->SUPER::cmd(%cmd);  
   } else {  I<cmd_disconnects>
     $self->print($cmd{'String'});  - if this is true, it then sets logged_in() to false, then it will close() the connection and then sets is_connected() to false
     @lines = $self->lastline;  
   }  I<expect>
   - if this is set (usually to 'Success.') it will check for that in the last line of output and if it does not, will return undef because the command probably failed
   $self->last_lines(\@lines);  
   I<args>
   my $vals = 1;  - a string containing the command line options that are passed to the command
   if ($cfg{'decode'}) {  
     if ($cfg{'decode'} eq 'each') {  =cut
       $vals = _decode_each_line(@lines);  
     } elsif ($cfg{'decode'} eq 'sulog') {  sub cmd
       $vals = _decode_sulog(@lines);  {
     } else {    my $self = shift;
       $vals = _decode_lines(@lines);  
     }    my @valid_net_telnet_opts = qw(
   }      String
       Output
   $self->last_vals($vals);      Cmd_remove_mode
       Errmode
       Input_record_separator
   my $last = $self->lastline;      Ors
       Output_record_separator
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {      Prompt
     if ($cfg{'cmd_disconnects'}) {      Rs
       $self->logged_in(0);      Timeout
       $self->close;    );
       $self->is_connected(0);  
     }    my %cfg;
     if (@_ == 1) {
     if ($cfg{'decode'}) {      $cfg{'String'} = shift;
       return $vals;    } elsif (@_ > 1) {
     } else {      %cfg = @_;
       return @lines;    }
     }  
   } else {    $cfg{'Timeout'} ||= $self->Timeout;
     #$! = "Error with command ($cfg{'string'}): $last";  
     return undef;    unless ($cfg{'String'}) {
   }      #$! = "No command passed";
 }      #warn "No command passed\n";
       return undef;
 #=item _decode_lines    }
   
 sub _decode_lines    unless ($self->is_connected) {
 {      #$! = "Not connected";
   my @lines = @_;      #warn "Not connected\n";
       return undef;
   my %conf;    }
   
   my $key = '';    unless ($self->logged_in) {
   my $val = '';      #$! = "Not logged in";
   my $in_key = 0;      #warn "Not logged in\n";
   my $in_val = 0;      return undef;
     }
   foreach my $line (@lines) {  
     next if $line =~ /$success$/;  
     my %cmd;
     my @chars = split //, $line;    foreach (@valid_net_telnet_opts) {
       if (exists $cfg{$_}) {
     my $last_key = '';        $cmd{$_} = $cfg{$_};
     foreach my $c (@chars) {      }
     }
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {    if ($cfg{'args'}) {
         if ($c eq '[') {      $cmd{'String'} .= ' ' . $cfg{'args'};
           $in_key = 1;    }
           $in_val = 0;    my @lines;
         } else {    unless ($cfg{'no_prompt'}) {
           $in_key = 0;      @lines = $self->SUPER::cmd(%cmd);
           $in_val = 0;    } else {
         }      $self->print($cmd{'String'});
       @lines = $self->lastline;
         if ($key) {    }
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;    $self->last_lines(\@lines);
   
           $val =~ s/^\s+//;    my $vals = 1;
           $val =~ s/\s+$//;    if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
       if ($cfg{'decode'} eq 'each') {
           if ($key eq 'Checksum' && $last_key) {        $vals = _decode_each_line(@lines);
             # Special case for these bastids.      } elsif ($cfg{'decode'} eq 'sulog') {
             my $new = $last_key;        $vals = _decode_sulog(@lines);
             $new =~ s/\s+\S+$//;      } elsif ($cfg{'decode'} eq 'maclist') {
             $key = $new . " " . $key;        $vals = _decode_maclist(@lines);
           }      } else {
         $vals = _decode_lines(@lines);
           $last_key = $key;      }
           $conf{$key} = $val;    }
           $key = '';  
           $val = '';    $self->last_vals($vals);
         }  
   
       } elsif ($c eq ']') {    my $last = $self->lastline;
         $in_val = 1;  
         $in_key = 0;    if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
         $c = shift @chars;      if ($cfg{'cmd_disconnects'}) {
         $self->logged_in(0);
       } elsif ($in_key) {        $self->close;
         $key .= $c;        $self->is_connected(0);
       }
       } elsif ($in_val) {  
         $val .= $c;      if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
       }        return $vals;
     }      } else {
   }        return @lines;
       }
   if (%conf) {    } else {
     return \%conf;      #$! = "Error with command ($cfg{'string'}): $last";
   } else {      return undef;
     return undef;    }
   }  }
 }  
   #=item _decode_lines
 #=item _decode_each_line  
   sub _decode_lines
 sub _decode_each_line  {
 {    my @lines = @_;
   my @lines = @_;  
   my @decoded;    my %conf;
   foreach my $line (@lines) {  
     my $decoded = _decode_lines($line);    my $key = '';
     push @decoded, $decoded if defined $decoded;    my $val = '';
   }    my $in_key = 0;
   return \@decoded;    my $in_val = 0;
 }  
     foreach my $line (@lines) {
 #=item _decode_sulog      next if $line =~ /$success$/;
   
 sub _decode_sulog      my @chars = split //, $line;
 {  
   my @lines = @_;      my $last_key = '';
   my @decoded;      foreach my $c (@chars) {
   my $last_tm;  
   foreach my $line (@lines) {        if ($c eq '[' || $c eq "\r" || $c eq "\n") {
     my $decoded = _decode_lines($line);          if ($c eq '[') {
             $in_key = 1;
     if (defined $decoded) {            $in_val = 0;
       if ($decoded->{'tm'}) {          } else {
         $last_tm = $decoded->{'tm'};            $in_key = 0;
         next;            $in_val = 0;
       } else {          }
         $decoded->{'tm'} = $last_tm;  
       }          if ($key) {
       next unless $last_tm;            $key =~ s/^\s+//;
             $key =~ s/\s+$//;
       push @decoded, $decoded if defined $decoded;  
     }            $val =~ s/^\s+//;
   }            $val =~ s/\s+$//;
   return \@decoded;  
 }            if ($key eq 'Checksum' && $last_key) {
               # Special case for these bastids.
 1;              my $new = $last_key;
 __END__              $new =~ s/\s+\S+$//;
               $key = $new . " " . $key;
 =back            }
   
 =head1 SEE ALSO            $last_key = $key;
             $conf{$key} = $val;
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm            $key = '';
             $val = '';
 L<Net::Telnet>          }
   
 =head1 TODO        } elsif ($c eq ']') {
           $in_val = 1;
 There are still a lot of commands that are not accessed directly.  If you call them (as cmd("command + args") or whatever) and it works, please send me examples that work and I will try to get it incorporated into the next version of the script.          $in_key = 0;
           $c = shift @chars;
 I also want to be able to parse the different types of output from commands like su, sudb all and anything else that would be better available as a perl datastructure.  
         } elsif ($in_key) {
 =head1 AUTHOR          $key .= $c;
   
 Andrew Fresh E<lt>andrew@rraz.netE<gt>        } elsif ($in_val) {
           $val .= $c;
 =head1 COPYRIGHT AND LICENSE        }
       }
 Copyright (C) 2005 by Andrew Fresh    }
   
 This library is free software; you can redistribute it and/or modify    if (%conf) {
 it under the same terms as Perl itself, either Perl version 5.8.7 or,      return \%conf;
 at your option, any later version of Perl 5 you may have available.    } else {
       return undef;
     }
 =cut  }
   
   #=item _decode_each_line
   
   sub _decode_each_line
   {
     my @lines = @_;
     my @decoded;
     foreach my $line (@lines) {
       my $decoded = _decode_lines($line);
       push @decoded, $decoded if defined $decoded;
     }
     return \@decoded;
   }
   
   #=item _decode_sulog
   
   sub _decode_sulog
   {
     my @lines = @_;
     my @decoded;
     my $last_tm;
     foreach my $line (@lines) {
       my $decoded = _decode_lines($line);
   
       if (defined $decoded) {
         if ($decoded->{'tm'}) {
           $last_tm = $decoded->{'tm'};
           next;
         } else {
           $decoded->{'tm'} = $last_tm;
         }
         next unless $last_tm;
   
         push @decoded, $decoded if defined $decoded;
       }
     }
     return \@decoded;
   }
   
   #=item _decode_maclist
   
   sub _decode_maclist
   {
           my @lines = @_;
           my @decoded;
           my $total_entries = 0;
           my $current_tm = 0;
           foreach my $line (@lines) {
                   $line =~ s/\r?\n$//;
                   my ($mac, $loc, $tm) = $line =~ /
                           ([0-9a-fA-F ]{17})\s+
                           (.*)\s+
                           tm\s+
                           (\d+)
                   /x;
   
                   if ($mac) {
                           $mac =~ s/\s+//g;
                           $loc =~ s/^\s+//;
                           $loc =~ s/\s+$//;
   
                           my $suid = undef;
                           if ($loc =~ /suid\s+=\s+(\d+)/) {
                                   $suid = $1;
                                   $loc = undef;
                           }
   
                           push @decoded, {
                                   mac  => $mac,
                                   loc  => $loc,
                                   tm   => $tm,
                                   suid => $suid,
                           };
                   } elsif ($line =~ /(\d+)\s+entries/) {
                           $total_entries = $1;
                   } elsif ($line =~ /current tm = (\d+)\s+sec/) {
                           $current_tm = $1
                   }
           }
   
           map { $_->{'cur_tm'} = $current_tm } @decoded;
   
           if (scalar @decoded == $total_entries) {
                   return \@decoded;
           } else {
                   # XXX we should have a way to set last error, not sure why we don't
                   return undef;
           }
   }
   
   1;
   __END__
   
   =back
   
   =head1 SEE ALSO
   
   Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm
   
   L<Net::Telnet>
   
   =head1 TODO
   
   There are still a lot of commands that are not accessed directly.  If you call them (as cmd("command + args") or whatever) and it works, please send me examples that work and I will try to get it incorporated into the next version of the script.
   
   I also want to be able to parse the different types of output from commands like su, sudb all and anything else that would be better available as a perl datastructure.
   
   =head1 AUTHOR
   
   Andrew Fresh E<lt>andrew@rraz.netE<gt>
   
   =head1 COPYRIGHT AND LICENSE
   
   Copyright (C) 2005 by Andrew Fresh
   
   This library is free software; you can redistribute it and/or modify
   it under the same terms as Perl itself, either Perl version 5.8.7 or,
   at your option, any later version of Perl 5 you may have available.
   
   
   =cut

Legend:
Removed from v.1.8  
changed lines
  Added in v.1.16

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>