[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.12 and 1.25

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

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