[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.10 and 1.24

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

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