[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.6 and 1.31

version 1.6, 2006/06/28 23:00:15 version 1.31, 2007/02/02 18:54:22
Line 1 
Line 1 
 package Net::Telnet::Trango;  package Net::Telnet::Trango;
 # $RedRiver: Trango.pm,v 1.5 2006/01/03 00:22:19 andrew Exp $  # $RedRiver: Trango.pm,v 1.30 2007/02/02 17:51:52 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;  
   my $t = new Net::Telnet::Trango ( Timeout => 5 );    use Net::Telnet::Trango;
       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->close;    $t->exit;
     $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
 =head2 EXPORT  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
 None  things like sysinfo very easy to do.
   
 =cut  =head2 EXPORT
   
 our $VERSION = '0.01';  None
   
 my %PRIVATE = (  =head1 METHODS
   is_connected => 0,  
   logged_in => 0,  =over
 );  
   =cut
 =pod  
   our $VERSION = '0.01';
 =item new  
   my %PRIVATE = (
 Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'    is_connected => 0,
     logged_in => 0,
 =cut  );
   
 sub new  =pod
 {  
   my $class = shift;  =item B<new>
   
   my %args;  - Creates a new Net::Telnet::Trango object.
   if (@_ == 1) {  
     $args{'Host'} = shift;          new([Options from Net::Telnet,]
   } else {              [Decode => 0,]);
     %args = @_;  
   }  Same as new from L<Net::Telnet> but sets the default Trango Prompt:
   '/#> *$/'
   $args{'Prompt'}  ||= '/#> *$/';  
   It also takes an optional parameter 'Decode'.  If not defined it
   foreach my $key (keys %args) {  defaults to 1, if it is set to 0, it will not decode the output and
     $PRIVATE{$key} = $args{$key};  instead return a reference to an array of the lines that were returned
   }  from the command.
   
   my $self = $class->SUPER::new(%args);  =cut
   bless $self if ref $self;  
   sub new
   return $self;  {
 }    my $class = shift;
   
 #  _password <new password> <new password>    my %args;
 #  ? [command]    if (@_ == 1) {
 #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...      $args{'Host'} = shift;
 #  arp -bcast <on|off>    } else {
 #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...      %args = @_;
 #  bye    }
 #  cf2cf ap [default|<size>]  
 #  date    $args{'Prompt'}  ||= '/#> *$/';
 #  date <month> <day> <year>  
 #  freq scantable    foreach my $key (keys %args) {
 #  freq channeltable      $PRIVATE{$key} = $args{$key};
 #  freq writescan [<ch#> <h|v>]    }
 #  freq writechannel [<ch#> <freq>] ...    $PRIVATE{'Decode'} = 1 unless defined $PRIVATE{'Decode'};
 #  freq <ch #> <h|v>    delete $args{'Decode'};
 #  help [command]  
 #  heater [<on temp> <off temp>]    my $self = $class->SUPER::new(%args);
 #  ipconfig [<new ip> <new subnet mask> <new gateway>]    bless $self if ref $self;
 #  log [<# of entries, 1..179>]  
 #  log <sum> <# of entries, 1..179>    return $self;
 #  logout  }
 #  opmode [ap [y]]  
 #  password  #  _password <new password> <new password>
 #  ping <ip addr>  #  ? [command]
 #  polar <h|v>  #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...
 #  power <setism|setunii> <max|min|<dBm>>  #  arp -bcast <on|off>
 #  reboot  #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...
 #  restart  #  bye
 #  remarks [<str>]  #  cf2cf ap [default|<size>]
 #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  date
 #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  date <month> <day> <year>
 #  sysinfo  #  freq scantable
 #  set suid <id>  #  freq channeltable
 #  set apid <id>  #  freq writescan [<ch#> <h|v>]
 #  set baseid <id>  #  freq writechannel [<ch#> <freq>] ...
 #  set defaultopmode [<ap|su> <min,0..10>]  #  freq <ch #> <h|v>
 #  set defaultopmode off  #  help [command]
 #  set snmpcomm [<read | write | trap (id or setall)> <str>]  #  heater [<on temp> <off temp>]
 #  set mir [on|off]  #  ipconfig [<new ip> <new subnet mask> <new gateway>]
 #  set mir threshold <kbps>  #  log [<# of entries, 1..179>]
 #  set rssitarget [<ism|unii> <dBm>]  #  log <sum> <# of entries, 1..179>
 #  set serviceradius [<ism | unii> <miles>]  #  logout
 #  ssrssi <ch #> <h|v>  #  opmode [ap [y]]
 #  su [<suid>|all]  #  password
 #  su changechannel <all|suid> <ch#> <h|v>  #  ping <ip addr>
 #  su ipconfig <suid> <new ip> <new subnet> <new gateway>  #  polar <h|v>
 #  su [live|poweroff|priority]  #  power <setism|setunii> <max|min|<dBm>>
 #  su <ping|info|status> <suid>  #  reboot
 #  su powerleveling <all|suid>  #  restart
 #  su reboot <all|suid>  #  remarks [<str>]
 #  su restart <all|suid>  #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  su testrflink <all|suid> [r]  #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  su testrflink <setlen> [64..1600]  #  sysinfo
 #  su testrflink <aptx> [20..100]  #  set suid <id>
 #  su sw <suid|all> <sw #> <on|off>  #  set apid <id>
 #  sudb [dload | view]  #  set baseid <id>
 #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>  #  set defaultopmode [<ap|su> <min,0..10>]
 #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>  #  set defaultopmode off
 #  sudb delete <all|<suid>>  #  set snmpcomm [<read | write | trap (id or setall)> <str>]
 #  sudb modify <suid> <cir|mir> <kbps>  #  set mir [on|off]
 #  sudb modify <suid> <su2su> <group id,hex>  #  set mir threshold <kbps>
 #  sudb view  #  set rssitarget [<ism|unii> <dBm>]
 #  sulog [lastmins | sampleperiod <1..60>]  #  set serviceradius [<ism | unii> <miles>]
 #  sulog [<# of entry,1..18>]  #  ssrssi <ch #> <h|v>
 #  survey <ism|unii> <time, sec> <h|v>  #  su [<suid>|all]
 #  sw [<sw #> <on|off>]  #  su changechannel <all|suid> <ch#> <h|v>
 #  temp  #  su ipconfig <suid> <new ip> <new subnet> <new gateway>
 #  tftpd [on|off]  #  su [live|poweroff|priority]
 #  time  #  su <ping|info|status> <suid>
 #  time <hour> <min> <sec>  #  su powerleveling <all|suid>
 #  save <mainimage|fpgaimage> <current chksum> <new chksum>  #  su reboot <all|suid>
 #  save <systemsetting|sudb>  #  su restart <all|suid>
 #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>  #  su testrflink <all|suid> [r]
 #  updateflash <systemsetting|sudb>  #  su testrflink <setlen> [64..1600]
   #  su testrflink <aptx> [20..100]
 =pod  #  su sw <suid|all> <sw #> <on|off>
   #  sudb [dload | view]
 =head1 METHODS  #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>
   #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>
 =head2 ACCESSORS  #  sudb delete <all|<suid>>
   #  sudb modify <suid> <cir|mir> <kbps>
 =over  #  sudb modify <suid> <su2su> <group id,hex>
   #  sudb view
 =item Host  #  sulog [lastmins | sampleperiod <1..60>]
   #  sulog [<# of entry,1..18>]
 returns the name of the host that you are accessing  #  survey <ism|unii> <time, sec> <h|v>
   #  sw [<sw #> <on|off>]
 =item firmware_version  #  temp
   #  tftpd [on|off]
 returns the firmware version on the trango if available otherwise undef.    #  time
 Available after a successful open()  #  time <hour> <min> <sec>
 This is usually only set internally  #  save <mainimage|fpgaimage> <current chksum> <new chksum>
   #  save <systemsetting|sudb>
 =item host_type  #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>
   #  updateflash <systemsetting|sudb>
 returns the type of host from the login banner for example M5830S or M5300S.    
 Available after a successful open()  =pod
 This is usually only set internally  
   =back
 =item is_connected  
   =head2 ACCESSORS
 returns 1 after a successful open() otherwise undef  
 This is usually only set internally  These are usually only set internally.
   
 =item logged_in  =over
   
 returns 1 after a successful login() 0 if it failed and undef if  =item B<firmware_version>
 login() was never called  
 This is usually only set internally  - returns the firmware version
   
 =item login_banner  Returns the firmware version if available, otherwise undef.
   
 returns the banner that is displayed when first connected at login.  Only set after a successful open()  It should be available after a successful open().
   
 This is usually only set internally  =item B<host_type>
   
 =item last_lines  - return the type of host you are connected to.
   
 returns the output from the last cmd() that was run as an array ref  returns the type of host from the login banner for example M5830S or M5300S.
 This is usually only set internally  
   Should be available after a successful open().
 =back  
   =item B<is_connected>
 =head2 ALIASES  
   - Status of the connection to host.
 =over  
   returns 1 when connected, undef otherwise.
 =item bye  
   =item B<logged_in>
 alias of exit()  
   - Status of being logged in to the host.
 =item restart  
   returns 1 after a successful login(), 0 if it failed and undef if
 alias of reboot()  login() was never called.
   
 =back  =item B<login_banner>
   
 =head2 COMMANDS  - The banner when first connecting to the host.
   
 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:  returns the banner that is displayed when first connected at login.
 C<tftpd(args =E<gt> 'on')> would enable tftpd  Only set after a successful open().
   
 =over  =item B<last_lines>
   
 =item tftpd  - The last lines of output from the last cmd().
   
 Returns a hash ref of the decoded output from the command.  returns, as an array ref, the output from the last cmd() that was run.
   
 Also see enable_tftpd() and disable_tftpd() as those check for correct output  =item B<last_error>
   
 =item ver  - A text output of the last error that was encountered.
   
 Returns a hash ref of the decoded output from the command.  returns the last error reported.  Probably contains the last entry in
   last_lines.
 =item sysinfo  
   =back
 Returns a hash ref of the decoded output from the command.  
   =head2 ALIASES
 =item exit  
   =over
 exits the command session with the trango and closes the connection  
   =item B<bye>
 =item reboot  
   - alias of L<exit()>.
 reboots the trango and closes the connection  
   =item B<restart>
 =item sulog  
   - alias of L<reboot()>.
 returns an array ref of hashes containing each log line.  
   =back
 =cut  
   =head2 COMMANDS
   
 my $success = 'Success.';  Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
 my %COMMANDS = (  as such they accept the same options as C<cmd()>.
   tftpd       => { decode => 'all',   expect => $success },  Specifically they take a named paramater "args", for example:
   ver         => { decode => 'all' },  C<tftpd(args =E<gt> 'on')> would enable tftpd
   sysinfo     => { decode => 'all',   expect => $success },  
   updateflash => { decode => 'all',   expect => $success },  =over
   sulog       => { decode => 'sulog', expect => $success },  
   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },  =item B<tftpd>
   reboot      => { no_prompt => 1, cmd_disconnects => 1 },  
   sudb_save   => { String => "sudb save", expect => $success },  - Returns a hash ref of the decoded output from the
   #su password???  command.
   #_bootloader  
   #temp  Also see enable_tftpd() and disable_tftpd() as those check that it was
   #heater  successfully changed.
 );  
   =item B<ver>
 my %ALIASES = (  
   bye     => 'exit',  - Returns a hash ref of the decoded output from the
   restart => 'reboot',  command.
 );  
   =item B<sysinfo>
 my %ACCESS = map { $_ => 1 } qw(  
   firmware_version  - Returns a hash ref of the decoded output from the
   host_type  command.
   Host  
   is_connected  =item B<exit>
   logged_in  
   login_banner  - exits the command session with the Trango and closes
   Timeout  the connection
   last_lines  
   last_vals  =item B<reboot>
 );  
   - reboots the Trango and closes the connection
 sub AUTOLOAD  
 {  =item B<remarks>
   my $self = shift;  
   - Set or retrieve the remarks.
   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/  
     or die "Weird: $AUTOLOAD";  Takes an optional argument, which sets the remarks.
   If there is no argument, returns the current remarks.
   if (exists $ALIASES{$method}) {  
     $method = $ALIASES{$method};    my $old_remarks = $t->remarks();
     return $self->$method(@_);    $t->remarks($new_remarks);
   }  
   =item B<sulog>
   if (exists $COMMANDS{$method}) {  
     $method = shift if (@_ == 1);  - returns an array ref of hashes containing each log
     $COMMANDS{$method}{'String'} ||= $method;  line.
     return $self->cmd(%{ $COMMANDS{$method} }, @_);  
   }  =item B<save_sudb>
   
   if (exists $ACCESS{$method}) {  - returns true on success, undef on failure
     my $prev = $PRIVATE{$method};  
     ($PRIVATE{$method}) = @_ if @_;  =item B<syslog>
     return $prev;  
   }  - returns the output from the syslog command
   
   $method = "SUPER::$method";  =item B<pipe>
   return $self->$method(@_);  
 }  - returns the output from the pipe command
   
 =pod  =item B<maclist>
   
 =item open  -  returns the output from the maclist command
   
 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()  =item B<maclist_reset>
   
 =cut  - resets the maclist.
   
 sub open  No useful output.
 {  
   my $self = shift;  =item B<eth_list>
   
   unless ( $self->SUPER::open(@_) ) {  - returns the output from the eth list command
     #$! = "Couldn't connect to " . $self->Host . ":  $!";  
     return undef;  =item B<su_info>
   }  
   - returns information about the SU.
   ## Get to login prompt  
   unless ($self->waitfor(  You need to pass in the $suid and it will return the info for that suid.
       -match => '/password: ?$/i',  
       -errmode => "return",    $t->su_info($suid);
     ) ) {  
     #$! = "problem connecting to host (" . $self->Host . "): " .  =item B<su_testrflink>
     #    $self->lastline;  
     return undef;  - tests the RF Link to an su
   }  
     $t->su_testrflink($suid|'all');
   $self->parse_login_banner($self->lastline);  
   =item B<save_ss>
   $self->is_connected(1);  
   - saves the config.
   return $self->is_connected;  
 }  Returns 1 on success, undef on failure.
   
 =pod  =cut
   
 =item login  
   my $success = 'Success\\.';
 Calls open() if not already connected, then sends the password and sets logged_in() if successful  my %COMMANDS = (
     tftpd       => { decode => 'all',   expect => $success },
 =cut    ver         => { decode => 'all' },
     sysinfo     => { decode => 'all',   expect => $success },
 sub login    updateflash => { decode => 'all',   expect => $success },
 {    sulog       => { decode => 'sulog', expect => $success },
   my $self = shift;    'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
     reboot      => { no_prompt => 1, cmd_disconnects => 1 },
   unless ($self->is_connected) {    remarks     => { decode => 'all', expect => $success },
     $self->open or return undef;    save_sudb   => { String => 'save sudb', expect => $success },
   }    syslog      => { expect => $success },
     'pipe'      => { }, # XXX needs a special decode
   my $password = shift;    maclist     => { decode => 'maclist' },
     maclist_reset => { String => 'maclist reset', expect => 'done' },
   $self->print($password);    eth_link    => { String => 'eth link', expect => $success },
   unless ($self->waitfor(    su_info     => { String => 'su info', decode => 'all', expect => $success },
     -match => $self->prompt,    su_testrflink => { String => 'su testrflink', decode => 'all', expect => $success },
     -errmode => "return",    save_ss     => { String => 'save ss', expect => $success },
   ) ) {    opmode      => { decode => 'all',   expect => $success },
     #$! = "login ($self->Host) failed: " . $self->lastline;    # eth r, w and reset???
     return undef;    #su password???
   }    #_bootloader
     #temp
   $self->logged_in(1);    #heater
   );
   return $self->logged_in;  
 }  my %ALIASES = (
     bye     => 'exit',
 =pod    restart => 'reboot',
     Host    => 'host',
 =item parse_login_banner  );
   
 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 %ACCESS = map { $_ => 1 } qw(
     firmware_version
 =cut    host_type
     is_connected
 sub parse_login_banner    logged_in
 {    login_banner
   my $self = shift;    Timeout
     last_lines
   if (@_) {    last_vals
     $self->login_banner(@_);    last_error
   }    Decode
   );
   my $banner = $self->login_banner;  
   sub AUTOLOAD
   my ($type, $ver) = $banner =~  {
     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;    my $self = shift;
   
   $self->login_banner($banner);    my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
   $self->host_type($type);      or die "Weird: $AUTOLOAD";
   $self->firmware_version($ver);  
     if (exists $ALIASES{$method}) {
   return 1;      $method = $ALIASES{$method};
 }      return $self->$method(@_);
     }
 =pod  
     if (exists $COMMANDS{$method}) {
 =item su_password      my %cmd;
       foreach my $k (keys %{ $COMMANDS{$method} }) {
 C<su_password('all'|suid, 'new_password')>        $cmd{$k} = $COMMANDS{$method}{$k};
       }
 =cut      $cmd{'String'} ||= $method;
       $cmd{'args'}   .= ' ' . shift if (@_ == 1);
 sub su_password      return $self->cmd(%cmd, @_);
 {    }
   my $self     = shift;  
   my $su       = shift || '!';    if (exists $ACCESS{$method}) {
   my $new_pass = shift || '';      my $prev = $PRIVATE{$method};
       ($PRIVATE{$method}) = @_ if @_;
   unless (defined $su) {      return $prev;
     warn "No su passed!"    }
     #return undef;  
   }    $method = "SUPER::$method";
     return $self->$method(@_);
   unless (defined $new_pass) {  }
     warn "No new password!"    
     #return undef;  =pod
   }  
   =item B<open>
   return $self->cmd(String => 'su password ' .  
                      $su . ' ' .  - Open a connection to a Trango AP.
                      $new_pass . ' ' .  
                      $new_pass,  Calls Net::Telnet::open() then makes sure you get a password prompt so
                      expect => $success,  you are ready to login() and parses the login banner so you can get
                     );  host_type() and firmware_version()
 }  
   =cut
   
 =pod  sub open
   {
 =item sudb_view    my $self = shift;
   
 returns a reference to an array of hashes each containing:    unless ( $self->SUPER::open(@_) ) {
       $self->last_error("Couldn't connect to " . $self->host . ":  $!");
   suid      return;
   type    }
   cir  
   mir    ## Get to login prompt
   mac    unless ($self->waitfor(
         -match => '/password: ?$/i',
 =cut        -errmode => "return",
       ) ) {
 sub sudb_view      $self->last_error("problem connecting to host (" . $self->host . "): " .
 {          $self->lastline);
   my $self = shift;      return;
     }
   my @lines = $self->cmd( String => 'sudb view', expect => $success );  
     $self->parse_login_banner($self->lastline);
   return undef unless @lines;  
     $self->is_connected(1);
   my @sus;  
   foreach (@lines) {    return $self->is_connected;
     if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {  }
       my %s = (  
         suid => $1,  =pod
         type => $2,  
         cir  => $3,  =item B<login>
         mir  => $4,  
         mac  => $5,  - Login to the AP.
       );  
   Calls open() if not already connected, then sends the password and sets
           $s{'mac'} =~ s/\s//g;  logged_in() if successful
           $s{'mac'} = uc($s{'mac'});  
   =cut
       push @sus, \%s;  
     }  sub login
   }  {
     my $self = shift;
   return \@sus;  
 }    unless ($self->is_connected) {
       $self->open or return;
 =pod    }
   
 =item sudb_add    my $password = shift;
   
 Takes the following paramaters    $self->print($password);
     unless ($self->waitfor(
         suid : numeric,      -match => $self->prompt,
         type : (reg|pr)      -errmode => "return",
         cir  : numeric,    ) ) {
         mir  : numeric,      $self->last_error("login ($self->host) failed: " . $self->lastline);
         mac  : Almost any format, it will be reformatted,      return;
     }
 and returns true on success or undef otherwise.  
     $self->logged_in(1);
 You should sudb_save() after calling this, or your changes  will be lost  
 when the AP is rebooted.    return $self->logged_in;
   }
 =cut  
   =pod
 sub sudb_add  
 {  =item B<parse_login_banner>
         my $self = shift;  
         my $suid = shift;  - Converts the login_banner to some useful
         my $type = shift;  variables.
         my $cir  = shift;  
         my $mir  = shift;  Takes a login banner (what you get when you first connect to the Trango)
         my $mac  = shift;  or reads what is already in login_banner() then parses it and sets
   host_type() and firmware_version() as well as login_banner()
         if ($suid =~ /\D/) {  
                 return undef;  =cut
         }  
   sub parse_login_banner
         unless (lc($type) eq 'reg' || lc($type) eq 'pr') {  {
                 warn "Invalid type '$type'!";    my $self = shift;
                 return undef;  
         }    if (@_) {
       $self->login_banner(@_);
         if ($cir =~ /\D/) {    }
                 warn "Invalid CIR '$cir'!";  
                 return undef;    my $banner = $self->login_banner;
         }  
     my ($type, $sep1, $subtype, $sep2, $ver) = $banner =~
         if ($mir =~ /\D/) {      /Welcome to Trango Broadband Wireless (\S+)([\s-]+)(\S+)([\s-]+)(.+)$/i;
                 warn "Invalid MIR '$mir'!";  
                 return undef;    $type .= $sep1 . $subtype;
         }    $ver = $subtype . $sep2 . $ver;
   
         my $new_mac = $mac;    $self->login_banner($banner);
         $new_mac =~ s/[^0-9A-Fa-f]//;    $self->host_type($type);
         unless (length $new_mac == 12) {    $self->firmware_version($ver);
                 warn "Invalid MAC '$mac'!";  
                 return undef;    return 1;
         }  }
         $new_mac = join ' ', $new_mac =~ /../g;  
   =pod
         my $string = 'sudb add ' .  
                 $suid . ' ' .  =item B<su_password>
                 $type . ' ' .  
                 $cir  . ' ' .  - Set the password on SUs connected to the AP.
                 $mir  . ' ' .  
                 $new_mac;  su_password('new_password'[, 'suid']) If no suid is specified,
   the default is "all".
   
         return $self->cmd( String => $string, expect => $success );    $t->su_password('good_pass', 5);
 }  
   =cut
 =pod  
   sub su_password
 =item sudb_delete  {
     my $self     = shift;
 Takes either 'all' or the  suid of the su to delete    my $new_pass = shift || '';
 and returns true on success or undef otherwise.    my $su       = shift || 'all';
   
 You should sudb_save() after calling this, or your changes  will be lost    unless (defined $new_pass) {
 when the AP is rebooted.      $self->last_error("No new password");
       #return;
 =cut    }
   
 sub sudb_delete    return $self->cmd(String => 'su password ' .
 {                       $su . ' ' .
         my $self = shift;                       $new_pass . ' ' .
         my $suid = shift;                       $new_pass,
                        expect => $success,
         if ($suid =~ /\D/) {                      );
                 return undef;  }
         }  
   =pod
         return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );  
 }  =item B<su_ipconfig>
   
   - Change IP configuration on SUs connected to
 =pod  the AP.
   
 =item sudb_modify  su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )
   
 Takes either the  suid of the su to delete    $t->su_ipconfig( 5, '10.0.1.5', '255.255.255.0', '10.0.1.1' );
 as well as what you are changing, either "cir, mir or su2su"  
 and returns true on success or undef otherwise.  =cut
   
 cir and mir also take a value to set the cir/mir to.  sub su_ipconfig
   {
 su2su takes a group id parameter that is in hex.          my $self        = shift;
   
 You should sudb_save() after calling this, or your changes  will be lost          my $suid        = shift;
 when the AP is rebooted.          my $new_ip      = shift;
           my $new_subnet  = shift;
 =cut          my $new_gateway = shift;
   
 sub sudb_modify          if ($suid =~ /\D/) {
 {                  $self->last_error("Invalid suid '$suid'");
         my $self  = shift;                  return;
         my $suid  = shift;          }
         my $opt   = shift;          unless ($new_ip) {
         my $value = shift;                  $self->last_error("no new_ip passed");
                   return;
         if ($suid =~ /\D/) {          }
                 return undef;          unless ($new_subnet) {
         }                  $self->last_error("no new_subnet passed");
                   return;
         if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {          }
                 if ($value =~ /\D/) {          unless ($new_gateway) {
                         return undef;                  $self->last_error("no new_gateway passed");
                 }                  return;
         } elsif (lc($opt) eq 'su2su') {          }
                 if ($value =~ /[^0-9A-Za-f]/) {  
                         return undef;          # su ipconfig <suid> <new ip> <new subnet> <new gateway>
                 }          return $self->cmd(String => 'su ipconfig ' .
         } else {                       $suid       . ' ' .
                 return undef;                       $new_ip     . ' ' .
         }                       $new_subnet . ' ' .
                        $new_gateway,
         my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;                       expect => $success,
                       );
         return $self->cmd( String => $string, expect => $success );  }
 }  
   =pod
 =pod  
   =item B<sudb_view>
 =item enable_tftpd  
   - Returns the output from the sudb view command
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing  
   returns a reference to an array of hashes each containing these keys
 =cut  'suid', 'type', 'cir', 'mir' and 'mac'
   
 sub enable_tftpd  =cut
 {  
   my $self = shift;  sub sudb_view
   {
   my $vals = $self->tftpd( args => 'on' );    my $self = shift;
   
   if ($vals->{'Tftpd'} eq 'listen') {    my $lines = $self->cmd( String => 'sudb view', expect => $success ) || [];
     return $vals;  
   } else {    return unless @{ $lines };
     return undef;  
   }    unless ($PRIVATE{'Decode'}) {
 }      return $lines;
     }
 =pod  
     my @sus;
 =item disable_tftpd    foreach (@{ $lines }) {
       next unless $_;
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'      if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
         my %s = (
 =cut          suid => $1,
           type => $2,
 sub disable_tftpd          cir  => $3,
 {          mir  => $4,
   my $self = shift;          mac  => $5,
         );
   my $vals = $self->tftpd( args => 'off' );  
             $s{'mac'} =~ s/\s//g;
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {            $s{'mac'} = uc($s{'mac'});
     return $vals;  
   } else {        push @sus, \%s;
     return undef;      }
   }    }
 }  
     return \@sus;
 =pod  }
   
 =item cmd  =pod
   
 This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.  =item B<sudb_add>
   
 Normally returns the last lines from from the command  Takes the following paramaters
   
 Also accepts these options:          suid : numeric,
           type : (reg|pr)
 I<decode>          cir  : numeric,
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output          mir  : numeric,
           mac  : Almost any format, it will be reformatted,
 I<cmd_disconnects>  
 - if this is true, it then sets logged_in() to false, then it will close() the connection and then sets is_connected() to false  and returns true on success or undef otherwise.
   
 I<expect>    $t->sudb_add($suid, 'reg', $cir, $mir, $mac);
 - 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  
   You should save_sudb() after calling this, or your changes  will be lost
 I<args>  when the AP is rebooted.
 - a string containing the command line options that are passed to the command  
   =cut
 =cut  
   sub sudb_add
 sub cmd  {
 {          my $self = shift;
   my $self = shift;          my $suid = shift;
           my $type = shift;
   my @valid_net_telnet_opts = qw(          my $cir  = shift;
     String          my $mir  = shift;
     Output          my $mac  = shift;
     Cmd_remove_mode  
     Errmode          if ($suid =~ /\D/) {
     Input_record_separator                  $self->last_error("Invalid suid '$suid'");
     Ors                  return;
     Output_record_separator          }
     Prompt  
     Rs          unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
     Timeout                  $self->last_error("Invalid type '$type'");
   );                  return;
           }
   my %cfg;  
   if (@_ == 2) {          if ($cir =~ /\D/) {
     $cfg{'String'} = shift;                  $self->last_error("Invalid CIR '$cir'");
   } elsif (@_ > 2) {                  return;
     %cfg = @_;          }
   }  
           if ($mir =~ /\D/) {
   $cfg{'Timeout'} ||= $self->Timeout;                  $self->last_error("Invalid MIR '$mir'");
                   return;
   unless ($cfg{'String'}) {          }
     #$! = "No command passed";  
     #warn "No command passed\n";          my $new_mac = $mac;
     return undef;          $new_mac =~ s/[^0-9A-Fa-f]//;
   }          unless (length $new_mac == 12) {
                   $self->last_error("Invalid MAC '$mac'");
   unless ($self->is_connected) {                  return;
     #$! = "Not connected";          }
     #warn "Not connected\n";          $new_mac = join ' ', $new_mac =~ /../g;
     return undef;  
   }          my $string = 'sudb add ' .
                   $suid . ' ' .
   unless ($self->logged_in) {                  $type . ' ' .
     #$! = "Not logged in";                  $cir  . ' ' .
     #warn "Not logged in\n";                  $mir  . ' ' .
     return undef;                  $new_mac;
   }  
   
           return $self->cmd( String => $string, expect => $success );
   my %cmd;  }
   foreach (@valid_net_telnet_opts) {  
     if (exists $cfg{$_}) {  =pod
       $cmd{$_} = $cfg{$_};  
     }  =item B<sudb_delete>
   }  
   if ($cfg{'args'}) {  Takes either 'all' or the  suid of the su to delete
     $cmd{'String'} .= ' ' . $cfg{'args'};  and returns true on success or undef otherwise.
   }  
   my @lines;    $t->sudb_delete($suid);
   unless ($cfg{'no_prompt'}) {  
     @lines = $self->SUPER::cmd(%cmd);  You should save_sudb() after calling this, or your changes  will be lost
   } else {  when the AP is rebooted.
     $self->print($cmd{'String'});  
     @lines = $self->lastline;  =cut
   }  
   sub sudb_delete
   $self->last_lines(\@lines);  {
           my $self = shift;
   my $vals = 1;          my $suid = shift;
   if ($cfg{'decode'}) {  
     if ($cfg{'decode'} eq 'each') {          #if (lc($suid) ne 'all' || $suid =~ /\D/) {
       $vals = _decode_each_line(@lines);          if ($suid =~ /\D/) {
     } elsif ($cfg{'decode'} eq 'sulog') {                  $self->last_error("Invalid suid '$suid'");
       $vals = _decode_sulog(@lines);                  return;
     } else {          }
       $vals = _decode_lines(@lines);  
     }          return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
   }  }
   
   $self->last_vals($vals);  =pod
   
   =item B<sudb_modify>
   my $last = $self->lastline;  
   Takes either the  suid of the su to change
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {  as well as what you are changing, either "cir, mir or su2su"
     if ($cfg{'cmd_disconnects'}) {  and returns true on success or undef otherwise.
       $self->logged_in(0);  
       $self->close;  cir and mir also take a value to set the cir/mir to.
       $self->is_connected(0);  
     }  su2su takes a group id parameter that is in hex.
   
     if ($cfg{'decode'}) {    $t->sudb_modify($suid, 'cir', 512);
       return $vals;  
     } else {  You should save_sudb() after calling this, or your changes  will be lost
       return @lines;  when the AP is rebooted.
     }  
   } else {  =cut
     #$! = "Error with command ($cfg{'string'}): $last";  
     return undef;  sub sudb_modify
   }  {
 }          my $self  = shift;
           my $suid  = shift;
 #=item _decode_lines          my $opt   = shift;
           my $value = shift;
 sub _decode_lines  
 {          if ($suid =~ /\D/) {
   my @lines = @_;                  $self->last_error("Invalid suid '$suid'");
                   return;
   my %conf;          }
   
   my $key = '';          if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
   my $val = '';                  if ($value =~ /\D/) {
   my $in_key = 0;                          $self->last_error("Invalid $opt '$value'");
   my $in_val = 0;                          return;
                   }
   foreach my $line (@lines) {          } elsif (lc($opt) eq 'su2su') {
     next if $line =~ /$success$/;                  if ($value =~ /[^0-9A-Za-f]/) {
                           $self->last_error("Invalid MAC '$value'");
     my @chars = split //, $line;                          return;
                   }
     my $last_key = '';          } else {
     foreach my $c (@chars) {                  $self->last_error("Invalid option '$opt'");
                   return;
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {          }
         if ($c eq '[') {  
           $in_key = 1;          my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
           $in_val = 0;  
         } else {          return $self->cmd( String => $string, expect => $success );
           $in_key = 0;  }
           $in_val = 0;  
         }  =pod
   
         if ($key) {  =item B<enable_tftpd>
           $key =~ s/^\s+//;  
           $key =~ s/\s+$//;  - enable the TFTP server
   
           $val =~ s/^\s+//;  runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
           $val =~ s/\s+$//;  
   =cut
           if ($key eq 'Checksum' && $last_key) {  
             # Special case for these bastids.  sub enable_tftpd
             my $new = $last_key;  {
             $new =~ s/\s+\S+$//;    my $self = shift;
             $key = $new . " " . $key;  
           }    my $vals = $self->tftpd( args => 'on' );
   
           $last_key = $key;    if ($vals->{'Tftpd'} eq 'listen') {
           $conf{$key} = $val;      return $vals;
           $key = '';    } else {
           $val = '';      return;
         }    }
   }
       } elsif ($c eq ']') {  
         $in_val = 1;  =pod
         $in_key = 0;  
         $c = shift @chars;  =item B<disable_tftpd>
   
       } elsif ($in_key) {  - disable the TFTP server
         $key .= $c;  
   runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
       } elsif ($in_val) {  
         $val .= $c;  =cut
       }  
     }  sub disable_tftpd
   }  {
     my $self = shift;
   if (%conf) {  
     return \%conf;    my $vals = $self->tftpd( args => 'off' );
   } else {  
     return undef;    if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
   }      return $vals;
 }    } else {
       return;
 #=item _decode_each_line    }
   }
 sub _decode_each_line  
 {  =pod
   my @lines = @_;  
   my @decoded;  =item B<cmd>
   foreach my $line (@lines) {  
     my $decoded = _decode_lines($line);  - runs a command on the AP.
     push @decoded, $decoded if defined $decoded;  
   }  This does most of the work.  At the heart, it calls Net::Telnet::cmd()
   return \@decoded;  but it also does some special stuff for Trango.
 }  
   Normally returns the last lines from from the command
 #=item _decode_sulog  
   If you are using this, rather than one of the "easy" methods above,
 sub _decode_sulog  you probably want to read through the source of this module to see how
 {  some of the other commands are called.
   my @lines = @_;  
   my @decoded;  In addition to the Net::Telnet::cmd() options, it also accepts these:
   my $last_tm;  
   foreach my $line (@lines) {  I<decode>
     my $decoded = _decode_lines($line);  
   - if this is true, then it will send the output lines to _decode_lines()
     if (defined $decoded) {  and then returns the decoded output
       if ($decoded->{'tm'}) {  
         $last_tm = $decoded->{'tm'};  I<no_prompt>
         next;  
       } else {  - if this is true, it does not wait for a prompt, so you are not stuck
         $decoded->{'tm'} = $last_tm;  waiting for something that will never happen.
       }  
       next unless $last_tm;  I<cmd_disconnects>
   
       push @decoded, $decoded if defined $decoded;  - if this is true, it then sets logged_in() to false, then it will
     }  close() the connection and set is_connected() to false
   }  
   return \@decoded;  I<expect>
 }  
   - if this is set (usually to 'Success.') it will check for that in the
 1;  last line of output and if it does not, will return undef because the
 __END__  command probably failed
   
 =back  I<args>
   
 =head1 SEE ALSO  - a string containing the command line options that are passed to the
   command
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm  
           $t->cmd( String => 'exit', no_prompt => 1, cmd_disconnects => 1 );
 L<Net::Telnet>  
   =cut
 =head1 TODO  
   sub cmd
 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.  {
     my $self = shift;
 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.  
     my @valid_net_telnet_opts = qw(
 =head1 AUTHOR      String
       Output
 Andrew Fresh E<lt>andrew@rraz.netE<gt>      Cmd_remove_mode
       Errmode
 =head1 COPYRIGHT AND LICENSE      Input_record_separator
       Ors
 Copyright (C) 2005 by Andrew Fresh      Output_record_separator
       Prompt
 This library is free software; you can redistribute it and/or modify      Rs
 it under the same terms as Perl itself, either Perl version 5.8.7 or,      Timeout
 at your option, any later version of Perl 5 you may have available.    );
   
     my %cfg;
 =cut    if (@_ == 1) {
       $cfg{'String'} = shift;
     } elsif (@_ > 1) {
       %cfg = @_;
     }
   
     $cfg{'Timeout'} ||= $self->Timeout;
   
     unless ($cfg{'String'}) {
       $self->last_error("No command passed");
       return;
     }
   
     unless ($self->is_connected) {
       $self->last_error("Not connected");
       return;
     }
   
     unless ($self->logged_in) {
       $self->last_error("Not logged in");
       return;
     }
   
   
     my %cmd;
     foreach (@valid_net_telnet_opts) {
       if (exists $cfg{$_}) {
         $cmd{$_} = $cfg{$_};
       }
     }
     if ($cfg{'args'}) {
       $cmd{'String'} .= ' ' . $cfg{'args'};
     }
   
     my @lines;
     if ($cfg{'no_prompt'}) {
       $self->print($cmd{'String'});
       @lines = $self->lastline;
     } else {
       @lines = $self->SUPER::cmd(%cmd);
     }
   
     $self->last_lines(\@lines);
   
     my $vals = 1;
     if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
       if ($cfg{'decode'} eq 'each') {
         $vals = _decode_each_line(@lines);
       } elsif ($cfg{'decode'} eq 'sulog') {
         $vals = _decode_sulog(@lines);
       } elsif ($cfg{'decode'} eq 'maclist') {
         $vals = _decode_maclist(@lines);
       } else {
         $vals = _decode_lines(@lines);
       }
     }
   
     $self->last_vals($vals);
   
   
     my $last = $self->lastline;
   
     if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
       if ($cfg{'cmd_disconnects'}) {
         $self->logged_in(0);
         $self->close;
         $self->is_connected(0);
       }
   
       if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
         return $vals;
       } else {
         return \@lines;
       }
     } else {
       $self->last_error("Error with command ($cfg{'String'}): $last");
       return;
     }
   }
   
   #=item _decode_lines
   
   sub _decode_lines
   {
     my @lines = @_;
   
     my %conf;
   
     my $key = '';
     my $val = undef;
     my @vals;
     my $in_key = 0;
     my $in_val = 1;
   
     foreach my $line (@lines) {
       next if $line =~ /$success$/;
   
       my @chars = split //, $line;
   
       my $last_key = '';
       foreach my $c (@chars) {
   
         if ($c eq '[' || $c eq "\r" || $c eq "\n") {
           if ($c eq '[') {
             $in_key = 1;
             $in_val = 0;
           } else {
             $in_key = 0;
             $in_val = 1;
           }
   
           if ($key) {
             $key =~ s/^\s+//;
             $key =~ s/\s+$//;
   
             if ($val) {
               $val =~ s/^\s+//;
               $val =~ s/\s+$//;
             }
   
             if ($key eq 'Checksum' && $last_key) {
               # Special case for these bastids.
               my $new = $last_key;
               $new =~ s/\s+\S+$//;
               $key = $new . " " . $key;
             }
   
             $conf{$key} = $val;
             $last_key = $key;
             $key = '';
           } elsif ($val) {
                           push @vals, $val;
                   }
           $val = '';
   
         } elsif ($c eq ']') {
           $in_val = 1;
           $in_key = 0;
           $c = shift @chars;
   
         } elsif ($in_key) {
           $key .= $c;
   
         } elsif ($in_val) {
           $val .= $c;
         }
       }
     }
   
     unless ($key) {
       push @vals, $val;
     }
   
     if (@vals == 1) {
       $val = $vals[0];
     } elsif (@vals) {
       $val= \@vals;
     } else {
       $val = undef;
     }
   
     if (%conf) {
       $conf{_pre} = $val if $val;
       return \%conf;
     } else {
       return $val;
     }
   }
   
   #=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;
           }
   }
   
   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,2006,2007 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.6  
changed lines
  Added in v.1.31

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