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

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

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