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

Diff for /trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm between version 1.6 and 1.17

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

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.17

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