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

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

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

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