[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.7 and 1.14

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

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