[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.19

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

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