[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.7 and 1.26

version 1.7, 2006/06/28 23:33:18 version 1.26, 2007/01/17 23:15:13
Line 1 
Line 1 
 package Net::Telnet::Trango;  package Net::Telnet::Trango;
 # $RedRiver: Trango.pm,v 1.6 2006/06/28 22:00:15 andrew Exp $  # $RedRiver: Trango.pm,v 1.25 2007/01/17 20:48:46 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 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<Host>
 login() was never called  
 This is usually only set internally  - returns the name of the host that you are accessing
   
 =item login_banner  =item B<firmware_version>
   
 returns the banner that is displayed when first connected at login.  Only set after a successful open()  - returns the firmware version
   
 This is usually only set internally  Returns the firmware version if available, otherwise undef.
   
 =item last_lines  It should be available after a successful open().
   
 returns the output from the last cmd() that was run as an array ref  =item B<host_type>
 This is usually only set internally  
   - return the type of host you are connected to.
 =back  
   returns the type of host from the login banner for example M5830S or M5300S.
 =head2 ALIASES  
   Should be available after a successful open().
 =over  
   =item B<is_connected>
 =item bye  
   - Status of the connection to host.
 alias of exit()  
   returns 1 when connected, undef otherwise.
 =item restart  
   =item B<logged_in>
 alias of reboot()  
   - Status of being logged in to the host.
 =back  
   returns 1 after a successful login(), 0 if it failed and undef if
 =head2 COMMANDS  login() was never called.
   
 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:  =item B<login_banner>
 C<tftpd(args =E<gt> 'on')> would enable tftpd  
   - The banner when first connecting to the host.
 =over  
   returns the banner that is displayed when first connected at login.
 =item tftpd  Only set after a successful open().
   
 Returns a hash ref of the decoded output from the command.  =item B<last_lines>
   
 Also see enable_tftpd() and disable_tftpd() as those check for correct output  - The last lines of output from the last cmd().
   
 =item ver  returns, as an array ref, the output from the last cmd() that was run.
   
 Returns a hash ref of the decoded output from the command.  =item B<last_error>
   
 =item sysinfo  - 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 exit  
   =back
 exits the command session with the trango and closes the connection  
   =head2 ALIASES
 =item reboot  
   =over
 reboots the trango and closes the connection  
   =item B<bye>
 =item sulog  
   - alias of L<exit()>.
 returns an array ref of hashes containing each log line.  
   =item B<restart>
 =cut  
   - alias of L<reboot()>.
   
 my $success = 'Success.';  =back
 my %COMMANDS = (  
   tftpd       => { decode => 'all',   expect => $success },  =head2 COMMANDS
   ver         => { decode => 'all' },  
   sysinfo     => { decode => 'all',   expect => $success },  Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
   updateflash => { decode => 'all',   expect => $success },  as such they accept the same options as C<cmd()>.
   sulog       => { decode => 'sulog', expect => $success },  Specifically they take a named paramater "args", for example:
   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },  C<tftpd(args =E<gt> 'on')> would enable tftpd
   reboot      => { no_prompt => 1, cmd_disconnects => 1 },  
   sudb_save   => { String => "sudb save", expect => $success },  =over
   #su password???  
   #_bootloader  =item B<tftpd>
   #temp  
   #heater  - Returns a hash ref of the decoded output from the
 );  command.
   
 my %ALIASES = (  Also see enable_tftpd() and disable_tftpd() as those check that it was
   bye     => 'exit',  successfully changed.
   restart => 'reboot',  
 );  =item B<ver>
   
 my %ACCESS = map { $_ => 1 } qw(  - Returns a hash ref of the decoded output from the
   firmware_version  command.
   host_type  
   Host  =item B<sysinfo>
   is_connected  
   logged_in  - Returns a hash ref of the decoded output from the
   login_banner  command.
   Timeout  
   last_lines  =item B<exit>
   last_vals  
 );  - exits the command session with the Trango and closes
   the connection
 sub AUTOLOAD  
 {  =item B<reboot>
   my $self = shift;  
   - reboots the Trango and closes the connection
   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/  
     or die "Weird: $AUTOLOAD";  =item B<remarks>
   
   if (exists $ALIASES{$method}) {  - Set or retrieve the remarks.
     $method = $ALIASES{$method};  
     return $self->$method(@_);  Takes an optional argument, which sets the remarks.
   }  If there is no argument, returns the current remarks.
   
   if (exists $COMMANDS{$method}) {    my $old_remarks = $t->remarks();
     $method = shift if (@_ == 1);    $t->remarks($new_remarks);
     $COMMANDS{$method}{'String'} ||= $method;  
     return $self->cmd(%{ $COMMANDS{$method} }, @_);  =item B<sulog>
   }  
   - returns an array ref of hashes containing each log
   if (exists $ACCESS{$method}) {  line.
     my $prev = $PRIVATE{$method};  
     ($PRIVATE{$method}) = @_ if @_;  =item B<save_sudb>
     return $prev;  
   }  - returns true on success, undef on failure
   
   $method = "SUPER::$method";  =item B<syslog>
   return $self->$method(@_);  
 }  - returns the output from the syslog command
   
 =pod  =item B<pipe>
   
 =item open  - returns the output from the pipe 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>
   
 =cut  -  returns the output from the maclist command
   
 sub open  =item B<maclist_reset>
 {  
   my $self = shift;  - resets the maclist.
   
   unless ( $self->SUPER::open(@_) ) {  No useful output.
     #$! = "Couldn't connect to " . $self->Host . ":  $!";  
     return undef;  =item B<eth_list>
   }  
   - returns the output from the eth list command
   ## Get to login prompt  
   unless ($self->waitfor(  =item B<su_info>
       -match => '/password: ?$/i',  
       -errmode => "return",  - returns information about the SU.
     ) ) {  
     #$! = "problem connecting to host (" . $self->Host . "): " .  You need to pass in the $suid and it will return the info for that suid.
     #    $self->lastline;  
     return undef;    $t->su_info($suid);
   }  
   =item B<save_ss>
   $self->parse_login_banner($self->lastline);  
   - saves the config.
   $self->is_connected(1);  
   Returns 1 on success, undef on failure.
   return $self->is_connected;  
 }  =cut
   
 =pod  
   my $success = 'Success.';
 =item login  my %COMMANDS = (
     tftpd       => { decode => 'all',   expect => $success },
 Calls open() if not already connected, then sends the password and sets logged_in() if successful    ver         => { decode => 'all' },
     sysinfo     => { decode => 'all',   expect => $success },
 =cut    updateflash => { decode => 'all',   expect => $success },
     sulog       => { decode => 'sulog', expect => $success },
 sub login    'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
 {    reboot      => { no_prompt => 1, cmd_disconnects => 1 },
   my $self = shift;    remarks     => { decode => 'all', expect => $success },
     save_sudb   => { String => 'save sudb', expect => $success },
   unless ($self->is_connected) {    syslog      => { expect => $success },
     $self->open or return undef;    'pipe'      => { }, # XXX needs a special decode
   }    maclist     => { decode => 'maclist' },
     maclist_reset => { String => 'maclist reset', expect => 'done' },
   my $password = shift;    eth_link    => { String => 'eth link', expect => $success },
     su_info     => { String => 'su info', decode => 'all', expect => $success },
   $self->print($password);    save_ss     => { String => 'save ss', expect => $success },
   unless ($self->waitfor(    opmode      => { decode => 'all',   expect => $success },
     -match => $self->prompt,    # eth r, w and reset???
     -errmode => "return",    #su password???
   ) ) {    #_bootloader
     #$! = "login ($self->Host) failed: " . $self->lastline;    #temp
     return undef;    #heater
   }  );
   
   $self->logged_in(1);  my %ALIASES = (
     bye     => 'exit',
   return $self->logged_in;    restart => 'reboot',
 }  );
   
 =pod  my %ACCESS = map { $_ => 1 } qw(
     firmware_version
 =item parse_login_banner    host_type
     Host
 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()    is_connected
     logged_in
 =cut    login_banner
     Timeout
 sub parse_login_banner    last_lines
 {    last_vals
   my $self = shift;    last_error
   );
   if (@_) {  
     $self->login_banner(@_);  sub AUTOLOAD
   }  {
     my $self = shift;
   my $banner = $self->login_banner;  
     my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
   my ($type, $ver) = $banner =~      or die "Weird: $AUTOLOAD";
     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;  
     if (exists $ALIASES{$method}) {
   $self->login_banner($banner);      $method = $ALIASES{$method};
   $self->host_type($type);      return $self->$method(@_);
   $self->firmware_version($ver);    }
   
   return 1;    if (exists $COMMANDS{$method}) {
 }      $COMMANDS{$method}{'String'} ||= $method;
       $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
 =pod      return $self->cmd(%{ $COMMANDS{$method} }, @_);
     }
 =item su_password  
     if (exists $ACCESS{$method}) {
 C<su_password('all'|suid, 'new_password')>      my $prev = $PRIVATE{$method};
       ($PRIVATE{$method}) = @_ if @_;
 =cut      return $prev;
     }
 sub su_password  
 {    $method = "SUPER::$method";
   my $self     = shift;    return $self->$method(@_);
   my $su       = shift || '!';  }
   my $new_pass = shift || '';  
   =pod
   unless (defined $su) {  
     warn "No su passed!"  =item B<open>
     #return undef;  
   }  - Open a connection to a Trango AP.
   
   unless (defined $new_pass) {  Calls Net::Telnet::open() then makes sure you get a password prompt so
     warn "No new password!"    you are ready to login() and parses the login banner so you can get
     #return undef;  host_type() and firmware_version()
   }  
   =cut
   return $self->cmd(String => 'su password ' .  
                      $su . ' ' .  sub open
                      $new_pass . ' ' .  {
                      $new_pass,    my $self = shift;
                      expect => $success,  
                     );    unless ( $self->SUPER::open(@_) ) {
 }      $self->last_error("Couldn't connect to " . $self->Host . ":  $!");
       return undef;
     }
 =pod  
     ## Get to login prompt
 =item sudb_view    unless ($self->waitfor(
         -match => '/password: ?$/i',
 returns a reference to an array of hashes each containing:        -errmode => "return",
       ) ) {
   suid      $self->last_error("problem connecting to host (" . $self->Host . "): " .
   type          $self->lastline);
   cir      return undef;
   mir    }
   mac  
     $self->parse_login_banner($self->lastline);
 =cut  
     $self->is_connected(1);
 sub sudb_view  
 {    return $self->is_connected;
   my $self = shift;  }
   
   my @lines = $self->cmd( String => 'sudb view', expect => $success );  =pod
   
   return undef unless @lines;  =item B<login>
   
   my @sus;  - Login to the AP.
   foreach (@lines) {  
     if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {  Calls open() if not already connected, then sends the password and sets
       my %s = (  logged_in() if successful
         suid => $1,  
         type => $2,  =cut
         cir  => $3,  
         mir  => $4,  sub login
         mac  => $5,  {
       );    my $self = shift;
   
           $s{'mac'} =~ s/\s//g;    unless ($self->is_connected) {
           $s{'mac'} = uc($s{'mac'});      $self->open or return undef;
     }
       push @sus, \%s;  
     }    my $password = shift;
   }  
     $self->print($password);
   return \@sus;    unless ($self->waitfor(
 }      -match => $self->prompt,
       -errmode => "return",
 =pod    ) ) {
       $self->last_error("login ($self->Host) failed: " . $self->lastline);
 =item sudb_add      return undef;
     }
 Takes the following paramaters  
     $self->logged_in(1);
         suid : numeric,  
         type : (reg|pr)    return $self->logged_in;
         cir  : numeric,  }
         mir  : numeric,  
         mac  : Almost any format, it will be reformatted,  =pod
   
 and returns true on success or undef otherwise.  =item B<parse_login_banner>
   
 You should sudb_save() after calling this, or your changes  will be lost  - Converts the login_banner to some useful
 when the AP is rebooted.  variables.
   
 =cut  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
 sub sudb_add  host_type() and firmware_version() as well as login_banner()
 {  
         my $self = shift;  =cut
         my $suid = shift;  
         my $type = shift;  sub parse_login_banner
         my $cir  = shift;  {
         my $mir  = shift;    my $self = shift;
         my $mac  = shift;  
     if (@_) {
         if ($suid =~ /\D/) {      $self->login_banner(@_);
                 return undef;    }
         }  
     my $banner = $self->login_banner;
         unless (lc($type) eq 'reg' || lc($type) eq 'pr') {  
                 warn "Invalid type '$type'!";    my ($type, $ver) = $banner =~
                 return undef;      /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
         }  
     $self->login_banner($banner);
         if ($cir =~ /\D/) {    $self->host_type($type);
                 warn "Invalid CIR '$cir'!";    $self->firmware_version($ver);
                 return undef;  
         }    return 1;
   }
         if ($mir =~ /\D/) {  
                 warn "Invalid MIR '$mir'!";  =pod
                 return undef;  
         }  =item B<su_password>
   
         my $new_mac = $mac;  - Set the password on SUs connected to the AP.
         $new_mac =~ s/[^0-9A-Fa-f]//;  
         unless (length $new_mac == 12) {  su_password('new_password'[, 'suid']) If no suid is specified,
                 warn "Invalid MAC '$mac'!";  the default is "all".
                 return undef;  
         }    $t->su_password('good_pass', 5);
         $new_mac = join ' ', $new_mac =~ /../g;  
   =cut
         my $string = 'sudb add ' .  
                 $suid . ' ' .  sub su_password
                 $type . ' ' .  {
                 $cir  . ' ' .    my $self     = shift;
                 $mir  . ' ' .    my $new_pass = shift || '';
                 $new_mac;    my $su       = shift || 'all';
   
     unless (defined $new_pass) {
         return $self->cmd( String => $string, expect => $success );      $self->last_error("No new password");
 }      #return undef;
     }
 =pod  
     return $self->cmd(String => 'su password ' .
 =item sudb_delete                       $su . ' ' .
                        $new_pass . ' ' .
 Takes either 'all' or the  suid of the su to delete                       $new_pass,
 and returns true on success or undef otherwise.                       expect => $success,
                       );
 You should sudb_save() after calling this, or your changes  will be lost  }
 when the AP is rebooted.  
   =pod
 =cut  
   =item B<su_ipconfig>
 sub sudb_delete  
 {  - Change IP configuration on SUs connected to
         my $self = shift;  the AP.
         my $suid = shift;  
   su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )
         if (lc($suid) ne 'all' || $suid =~ /\D/) {  
                 return undef;    $t->su_ipconfig( 5, '10.0.1.5', '255.255.255.0', '10.0.1.1' );
         }  
   =cut
         return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );  
 }  sub su_ipconfig
   {
           my $self        = shift;
 =pod  
           my $suid        = shift;
 =item sudb_modify          my $new_ip      = shift;
           my $new_subnet  = shift;
 Takes either the  suid of the su to delete          my $new_gateway = shift;
 as well as what you are changing, either "cir, mir or su2su"  
 and returns true on success or undef otherwise.          if ($suid =~ /\D/) {
                   $self->last_error("Invalid suid '$suid'");
 cir and mir also take a value to set the cir/mir to.                  return undef;
           }
 su2su takes a group id parameter that is in hex.          unless ($new_ip) {
                   $self->last_error("no new_ip passed");
 You should sudb_save() after calling this, or your changes  will be lost                  return undef;
 when the AP is rebooted.          }
           unless ($new_subnet) {
 =cut                  $self->last_error("no new_subnet passed");
                   return undef;
 sub sudb_modify          }
 {          unless ($new_gateway) {
         my $self  = shift;                  $self->last_error("no new_gateway passed");
         my $suid  = shift;                  return undef;
         my $opt   = shift;          }
         my $value = shift;  
           # su ipconfig <suid> <new ip> <new subnet> <new gateway>
         if ($suid =~ /\D/) {          return $self->cmd(String => 'su ipconfig ' .
                 return undef;                       $suid       . ' ' .
         }                       $new_ip     . ' ' .
                        $new_subnet . ' ' .
         if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {                       $new_gateway,
                 if ($value =~ /\D/) {                       expect => $success,
                         return undef;                      );
                 }  }
         } elsif (lc($opt) eq 'su2su') {  
                 if ($value =~ /[^0-9A-Za-f]/) {  =pod
                         return undef;  
                 }  =item B<sudb_view>
         } else {  
                 return undef;  - Returns the output from the sudb view command
         }  
   returns a reference to an array of hashes each containing these keys
         my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;  'suid', 'type', 'cir', 'mir' and 'mac'
   
         return $self->cmd( String => $string, expect => $success );  =cut
 }  
   sub sudb_view
 =pod  {
     my $self = shift;
 =item enable_tftpd  
     my @lines = $self->cmd( String => 'sudb view', expect => $success );
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing  
     return undef unless @lines;
 =cut  
     unless ($PRIVATE{'Decode'}) {
 sub enable_tftpd      return @lines;
 {    }
   my $self = shift;  
     my @sus;
   my $vals = $self->tftpd( args => 'on' );    foreach (@lines) {
       next unless $_;
   if ($vals->{'Tftpd'} eq 'listen') {      if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
     return $vals;        my %s = (
   } else {          suid => $1,
     return undef;          type => $2,
   }          cir  => $3,
 }          mir  => $4,
           mac  => $5,
 =pod        );
   
 =item disable_tftpd            $s{'mac'} =~ s/\s//g;
             $s{'mac'} = uc($s{'mac'});
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'  
         push @sus, \%s;
 =cut      }
     }
 sub disable_tftpd  
 {    return \@sus;
   my $self = shift;  }
   
   my $vals = $self->tftpd( args => 'off' );  =pod
   
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {  =item B<sudb_add>
     return $vals;  
   } else {  Takes the following paramaters
     return undef;  
   }          suid : numeric,
 }          type : (reg|pr)
           cir  : numeric,
 =pod          mir  : numeric,
           mac  : Almost any format, it will be reformatted,
 =item cmd  
   and returns true on success or undef otherwise.
 This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.  
     $t->sudb_add($suid, 'reg', $cir, $mir, $mac);
 Normally returns the last lines from from the command  
   You should save_sudb() after calling this, or your changes  will be lost
 Also accepts these options:  when the AP is rebooted.
   
 I<decode>  =cut
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output  
   sub sudb_add
 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          my $self = shift;
           my $suid = shift;
 I<expect>          my $type = shift;
 - 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          my $cir  = shift;
           my $mir  = shift;
 I<args>          my $mac  = shift;
 - a string containing the command line options that are passed to the command  
           if ($suid =~ /\D/) {
 =cut                  $self->last_error("Invalid suid '$suid'");
                   return undef;
 sub cmd          }
 {  
   my $self = shift;          unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
                   $self->last_error("Invalid type '$type'");
   my @valid_net_telnet_opts = qw(                  return undef;
     String          }
     Output  
     Cmd_remove_mode          if ($cir =~ /\D/) {
     Errmode                  $self->last_error("Invalid CIR '$cir'");
     Input_record_separator                  return undef;
     Ors          }
     Output_record_separator  
     Prompt          if ($mir =~ /\D/) {
     Rs                  $self->last_error("Invalid MIR '$mir'");
     Timeout                  return undef;
   );          }
   
   my %cfg;          my $new_mac = $mac;
   if (@_ == 2) {          $new_mac =~ s/[^0-9A-Fa-f]//;
     $cfg{'String'} = shift;          unless (length $new_mac == 12) {
   } elsif (@_ > 2) {                  $self->last_error("Invalid MAC '$mac'");
     %cfg = @_;                  return undef;
   }          }
           $new_mac = join ' ', $new_mac =~ /../g;
   $cfg{'Timeout'} ||= $self->Timeout;  
           my $string = 'sudb add ' .
   unless ($cfg{'String'}) {                  $suid . ' ' .
     #$! = "No command passed";                  $type . ' ' .
     #warn "No command passed\n";                  $cir  . ' ' .
     return undef;                  $mir  . ' ' .
   }                  $new_mac;
   
   unless ($self->is_connected) {  
     #$! = "Not connected";          return $self->cmd( String => $string, expect => $success );
     #warn "Not connected\n";  }
     return undef;  
   }  =pod
   
   unless ($self->logged_in) {  =item B<sudb_delete>
     #$! = "Not logged in";  
     #warn "Not logged in\n";  Takes either 'all' or the  suid of the su to delete
     return undef;  and returns true on success or undef otherwise.
   }  
     $t->sudb_delete($suid);
   
   my %cmd;  You should save_sudb() after calling this, or your changes  will be lost
   foreach (@valid_net_telnet_opts) {  when the AP is rebooted.
     if (exists $cfg{$_}) {  
       $cmd{$_} = $cfg{$_};  =cut
     }  
   }  sub sudb_delete
   if ($cfg{'args'}) {  {
     $cmd{'String'} .= ' ' . $cfg{'args'};          my $self = shift;
   }          my $suid = shift;
   my @lines;  
   unless ($cfg{'no_prompt'}) {          #if (lc($suid) ne 'all' || $suid =~ /\D/) {
     @lines = $self->SUPER::cmd(%cmd);          if ($suid =~ /\D/) {
   } else {                  $self->last_error("Invalid suid '$suid'");
     $self->print($cmd{'String'});                  return undef;
     @lines = $self->lastline;          }
   }  
           return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
   $self->last_lines(\@lines);  }
   
   my $vals = 1;  =pod
   if ($cfg{'decode'}) {  
     if ($cfg{'decode'} eq 'each') {  =item B<sudb_modify>
       $vals = _decode_each_line(@lines);  
     } elsif ($cfg{'decode'} eq 'sulog') {  Takes either the  suid of the su to change
       $vals = _decode_sulog(@lines);  as well as what you are changing, either "cir, mir or su2su"
     } else {  and returns true on success or undef otherwise.
       $vals = _decode_lines(@lines);  
     }  cir and mir also take a value to set the cir/mir to.
   }  
   su2su takes a group id parameter that is in hex.
   $self->last_vals($vals);  
     $t->sudb_modify($suid, 'cir', 512);
   
   my $last = $self->lastline;  You should save_sudb() after calling this, or your changes  will be lost
   when the AP is rebooted.
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {  
     if ($cfg{'cmd_disconnects'}) {  =cut
       $self->logged_in(0);  
       $self->close;  sub sudb_modify
       $self->is_connected(0);  {
     }          my $self  = shift;
           my $suid  = shift;
     if ($cfg{'decode'}) {          my $opt   = shift;
       return $vals;          my $value = shift;
     } else {  
       return @lines;          if ($suid =~ /\D/) {
     }                  $self->last_error("Invalid suid '$suid'");
   } else {                  return undef;
     #$! = "Error with command ($cfg{'string'}): $last";          }
     return undef;  
   }          if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
 }                  if ($value =~ /\D/) {
                           $self->last_error("Invalid $opt '$value'");
 #=item _decode_lines                          return undef;
                   }
 sub _decode_lines          } elsif (lc($opt) eq 'su2su') {
 {                  if ($value =~ /[^0-9A-Za-f]/) {
   my @lines = @_;                          $self->last_error("Invalid MAC '$value'");
                           return undef;
   my %conf;                  }
           } else {
   my $key = '';                  $self->last_error("Invalid option '$opt'");
   my $val = '';                  return undef;
   my $in_key = 0;          }
   my $in_val = 0;  
           my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
   foreach my $line (@lines) {  
     next if $line =~ /$success$/;          return $self->cmd( String => $string, expect => $success );
   }
     my @chars = split //, $line;  
   =pod
     my $last_key = '';  
     foreach my $c (@chars) {  =item B<enable_tftpd>
   
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  - enable the TFTP server
         if ($c eq '[') {  
           $in_key = 1;  runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
           $in_val = 0;  
         } else {  =cut
           $in_key = 0;  
           $in_val = 0;  sub enable_tftpd
         }  {
     my $self = shift;
         if ($key) {  
           $key =~ s/^\s+//;    my $vals = $self->tftpd( args => 'on' );
           $key =~ s/\s+$//;  
     if ($vals->{'Tftpd'} eq 'listen') {
           $val =~ s/^\s+//;      return $vals;
           $val =~ s/\s+$//;    } else {
       return undef;
           if ($key eq 'Checksum' && $last_key) {    }
             # Special case for these bastids.  }
             my $new = $last_key;  
             $new =~ s/\s+\S+$//;  =pod
             $key = $new . " " . $key;  
           }  =item B<disable_tftpd>
   
           $last_key = $key;  - disable the TFTP server
           $conf{$key} = $val;  
           $key = '';  runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
           $val = '';  
         }  =cut
   
       } elsif ($c eq ']') {  sub disable_tftpd
         $in_val = 1;  {
         $in_key = 0;    my $self = shift;
         $c = shift @chars;  
     my $vals = $self->tftpd( args => 'off' );
       } elsif ($in_key) {  
         $key .= $c;    if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
       return $vals;
       } elsif ($in_val) {    } else {
         $val .= $c;      return undef;
       }    }
     }  }
   }  
   =pod
   if (%conf) {  
     return \%conf;  =item B<cmd>
   } else {  
     return undef;  - runs a command on the AP.
   }  
 }  This does most of the work.  At the heart, it calls Net::Telnet::cmd()
   but it also does some special stuff for Trango.
 #=item _decode_each_line  
   Normally returns the last lines from from the command
 sub _decode_each_line  
 {  If you are using this, rather than one of the "easy" methods above,
   my @lines = @_;  you probably want to read through the source of this module to see how
   my @decoded;  some of the other commands are called.
   foreach my $line (@lines) {  
     my $decoded = _decode_lines($line);  In addition to the Net::Telnet::cmd() options, it also accepts these:
     push @decoded, $decoded if defined $decoded;  
   }  I<decode>
   return \@decoded;  
 }  - if this is true, then it will send the output lines to _decode_lines()
   and then returns the decoded output
 #=item _decode_sulog  
   I<no_prompt>
 sub _decode_sulog  
 {  - if this is true, it does not wait for a prompt, so you are not stuck
   my @lines = @_;  waiting for something that will never happen.
   my @decoded;  
   my $last_tm;  I<cmd_disconnects>
   foreach my $line (@lines) {  
     my $decoded = _decode_lines($line);  - if this is true, it then sets logged_in() to false, then it will
   close() the connection and set is_connected() to false
     if (defined $decoded) {  
       if ($decoded->{'tm'}) {  I<expect>
         $last_tm = $decoded->{'tm'};  
         next;  - if this is set (usually to 'Success.') it will check for that in the
       } else {  last line of output and if it does not, will return undef because the
         $decoded->{'tm'} = $last_tm;  command probably failed
       }  
       next unless $last_tm;  I<args>
   
       push @decoded, $decoded if defined $decoded;  - a string containing the command line options that are passed to the
     }  command
   }  
   return \@decoded;          $t->cmd( String => 'exit', no_prompt => 1, cmd_disconnects => 1 );
 }  
   =cut
 1;  
 __END__  sub cmd
   {
 =back    my $self = shift;
   
 =head1 SEE ALSO    my @valid_net_telnet_opts = qw(
       String
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm      Output
       Cmd_remove_mode
 L<Net::Telnet>      Errmode
       Input_record_separator
 =head1 TODO      Ors
       Output_record_separator
 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.      Prompt
       Rs
 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.      Timeout
     );
 =head1 AUTHOR  
     my %cfg;
 Andrew Fresh E<lt>andrew@rraz.netE<gt>    if (@_ == 1) {
       $cfg{'String'} = shift;
 =head1 COPYRIGHT AND LICENSE    } elsif (@_ > 1) {
       %cfg = @_;
 Copyright (C) 2005 by Andrew Fresh    }
   
 This library is free software; you can redistribute it and/or modify    $cfg{'Timeout'} ||= $self->Timeout;
 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.    unless ($cfg{'String'}) {
       $self->last_error("No command passed");
       return undef;
 =cut    }
   
     unless ($self->is_connected) {
       $self->last_error("Not connected");
       return undef;
     }
   
     unless ($self->logged_in) {
       $self->last_error("Not logged in");
       return undef;
     }
   
   
     my %cmd;
     foreach (@valid_net_telnet_opts) {
       if (exists $cfg{$_}) {
         $cmd{$_} = $cfg{$_};
       }
     }
     if ($cfg{'args'}) {
       $cmd{'String'} .= ' ' . $cfg{'args'};
     }
     my @lines;
     unless ($cfg{'no_prompt'}) {
       @lines = $self->SUPER::cmd(%cmd);
     } else {
       $self->print($cmd{'String'});
       @lines = $self->lastline;
     }
   
     $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 undef;
     }
   }
   
   #=item _decode_lines
   
   sub _decode_lines
   {
     my @lines = @_;
   
     my %conf;
   
     my $key = '';
     my $val = undef;
     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 (defined $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;
             }
   
             $last_key = $key;
             $conf{$key} = $val;
             $key = '';
             $val = '';
           }
   
         } elsif ($c eq ']') {
           $in_val = 1;
           $in_key = 0;
           $c = shift @chars;
   
         } elsif ($in_key) {
           $key .= $c;
   
         } elsif ($in_val) {
           $val .= $c;
         }
       }
     }
   
     if (%conf) {
       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 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,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.7  
changed lines
  Added in v.1.26

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