[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.3 and 1.16

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

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