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

version 1.12, 2006/08/31 22:29:53 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.11 2006/08/23 00:37:20 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     => { decode => 'maclist' },  =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 su_ipconfig  =item su_password
   
 C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>  C<su_password('new_password'[, 'suid'])> If no suid is specified,
   the default is "all".
 =cut  
   =cut
 sub su_ipconfig  
 {  sub su_password
         my $self        = shift;  {
     my $self     = shift;
         my $suid        = shift;    my $new_pass = shift || '';
         my $new_ip      = shift;    my $su       = shift || 'all';
         my $new_subnet  = shift;  
         my $new_gateway = shift;    unless (defined $new_pass) {
       warn "No new password!"
         return undef unless $suid =~ /^\d+$/;      #return undef;
         return undef unless $new_ip;    }
         return undef unless $new_subnet;  
         return undef unless $new_gateway;    return $self->cmd(String => 'su password ' .
                                        $su . ' ' .
         # su ipconfig <suid> <new ip> <new subnet> <new gateway>                       $new_pass . ' ' .
         return $self->cmd(String => 'su ipconfig ' .                       $new_pass,
                      $suid       . ' ' .                       expect => $success,
                      $new_ip     . ' ' .                      );
                      $new_subnet . ' ' .  }
                      $new_gateway,  
                      expect => $success,  =pod
                     );  
 }  =item su_ipconfig
   
 =pod  C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
   
 =item sudb_view  =cut
   
 returns a reference to an array of hashes each containing:  sub su_ipconfig
   {
   suid          my $self        = shift;
   type  
   cir          my $suid        = shift;
   mir          my $new_ip      = shift;
   mac          my $new_subnet  = shift;
           my $new_gateway = shift;
 =cut  
           return undef unless $suid =~ /^\d+$/;
 sub sudb_view          return undef unless $new_ip;
 {          return undef unless $new_subnet;
   my $self = shift;          return undef unless $new_gateway;
   
   my @lines = $self->cmd( String => 'sudb view', expect => $success );          # su ipconfig <suid> <new ip> <new subnet> <new gateway>
           return $self->cmd(String => 'su ipconfig ' .
   return undef unless @lines;                       $suid       . ' ' .
                        $new_ip     . ' ' .
   unless ($PRIVATE{'Decode'}) {                       $new_subnet . ' ' .
     return @lines;                       $new_gateway,
   }                       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 sudb_view
         suid => $1,  
         type => $2,  returns a reference to an array of hashes each containing:
         cir  => $3,  
         mir  => $4,    suid
         mac  => $5,    type
       );    cir
     mir
           $s{'mac'} =~ s/\s//g;    mac
           $s{'mac'} = uc($s{'mac'});  
   =cut
       push @sus, \%s;  
     }  sub sudb_view
   }  {
     my $self = shift;
   return \@sus;  
 }    my @lines = $self->cmd( String => 'sudb view', expect => $success );
   
 =pod    return undef unless @lines;
   
 =item sudb_add    unless ($PRIVATE{'Decode'}) {
       return @lines;
 Takes the following paramaters    }
   
         suid : numeric,    my @sus;
         type : (reg|pr)    foreach (@lines) {
         cir  : numeric,      next unless $_;
         mir  : numeric,      if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
         mac  : Almost any format, it will be reformatted,        my %s = (
           suid => $1,
 and returns true on success or undef otherwise.          type => $2,
           cir  => $3,
 You should save_sudb() after calling this, or your changes  will be lost          mir  => $4,
 when the AP is rebooted.          mac  => $5,
         );
 =cut  
             $s{'mac'} =~ s/\s//g;
 sub sudb_add            $s{'mac'} = uc($s{'mac'});
 {  
         my $self = shift;        push @sus, \%s;
         my $suid = shift;      }
         my $type = shift;    }
         my $cir  = shift;  
         my $mir  = shift;    return \@sus;
         my $mac  = shift;  }
   
         if ($suid =~ /\D/) {  =pod
                 return undef;  
         }  =item sudb_add
   
         unless (lc($type) eq 'reg' || lc($type) eq 'pr') {  Takes the following paramaters
                 warn "Invalid type '$type'!";  
                 return undef;          suid : numeric,
         }          type : (reg|pr)
           cir  : numeric,
         if ($cir =~ /\D/) {          mir  : numeric,
                 warn "Invalid CIR '$cir'!";          mac  : Almost any format, it will be reformatted,
                 return undef;  
         }  and returns true on success or undef otherwise.
   
         if ($mir =~ /\D/) {  You should save_sudb() after calling this, or your changes  will be lost
                 warn "Invalid MIR '$mir'!";  when the AP is rebooted.
                 return undef;  
         }  =cut
   
         my $new_mac = $mac;  sub sudb_add
         $new_mac =~ s/[^0-9A-Fa-f]//;  {
         unless (length $new_mac == 12) {          my $self = shift;
                 warn "Invalid MAC '$mac'!";          my $suid = shift;
                 return undef;          my $type = shift;
         }          my $cir  = shift;
         $new_mac = join ' ', $new_mac =~ /../g;          my $mir  = shift;
           my $mac  = shift;
         my $string = 'sudb add ' .  
                 $suid . ' ' .          if ($suid =~ /\D/) {
                 $type . ' ' .                  return undef;
                 $cir  . ' ' .          }
                 $mir  . ' ' .  
                 $new_mac;          unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
                   warn "Invalid type '$type'!";
                   return undef;
         return $self->cmd( String => $string, expect => $success );          }
 }  
           if ($cir =~ /\D/) {
 =pod                  warn "Invalid CIR '$cir'!";
                   return undef;
 =item sudb_delete          }
   
 Takes either 'all' or the  suid of the su to delete          if ($mir =~ /\D/) {
 and returns true on success or undef otherwise.                  warn "Invalid MIR '$mir'!";
                   return undef;
 You should save_sudb() after calling this, or your changes  will be lost          }
 when the AP is rebooted.  
           my $new_mac = $mac;
 =cut          $new_mac =~ s/[^0-9A-Fa-f]//;
           unless (length $new_mac == 12) {
 sub sudb_delete                  warn "Invalid MAC '$mac'!";
 {                  return undef;
         my $self = shift;          }
         my $suid = shift;          $new_mac = join ' ', $new_mac =~ /../g;
   
         if (lc($suid) ne 'all' || $suid =~ /\D/) {          my $string = 'sudb add ' .
                 return undef;                  $suid . ' ' .
         }                  $type . ' ' .
                   $cir  . ' ' .
         return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );                  $mir  . ' ' .
 }                  $new_mac;
   
 =pod  
           return $self->cmd( String => $string, expect => $success );
 =item sudb_modify  }
   
 Takes either the  suid of the su to delete  =pod
 as well as what you are changing, either "cir, mir or su2su"  
 and returns true on success or undef otherwise.  =item sudb_delete
   
 cir and mir also take a value to set the cir/mir to.  Takes either 'all' or the  suid of the su to delete
   and returns true on success or undef otherwise.
 su2su takes a group id parameter that is in hex.  
   You should save_sudb() after calling this, or your changes  will be lost
 You should save_sudb() after calling this, or your changes  will be lost  when the AP is rebooted.
 when the AP is rebooted.  
   =cut
 =cut  
   sub sudb_delete
 sub sudb_modify  {
 {          my $self = shift;
         my $self  = shift;          my $suid = shift;
         my $suid  = shift;  
         my $opt   = shift;          if (lc($suid) ne 'all' || $suid =~ /\D/) {
         my $value = shift;                  return undef;
           }
         if ($suid =~ /\D/) {  
                 return undef;          return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
         }  }
   
         if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {  =pod
                 if ($value =~ /\D/) {  
                         return undef;  =item sudb_modify
                 }  
         } elsif (lc($opt) eq 'su2su') {  Takes either the  suid of the su to delete
                 if ($value =~ /[^0-9A-Za-f]/) {  as well as what you are changing, either "cir, mir or su2su"
                         return undef;  and returns true on success or undef otherwise.
                 }  
         } else {  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.
   
         my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;  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
   
 =pod  sub sudb_modify
   {
 =item enable_tftpd          my $self  = shift;
           my $suid  = shift;
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing          my $opt   = shift;
           my $value = shift;
 =cut  
           if ($suid =~ /\D/) {
 sub enable_tftpd                  return undef;
 {          }
   my $self = shift;  
           if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
   my $vals = $self->tftpd( args => 'on' );                  if ($value =~ /\D/) {
                           return undef;
   if ($vals->{'Tftpd'} eq 'listen') {                  }
     return $vals;          } elsif (lc($opt) eq 'su2su') {
   } else {                  if ($value =~ /[^0-9A-Za-f]/) {
     return undef;                          return undef;
   }                  }
 }          } else {
                   return undef;
 =pod          }
   
 =item disable_tftpd          my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
   
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'          return $self->cmd( String => $string, expect => $success );
   }
 =cut  
   =pod
 sub disable_tftpd  
 {  =item enable_tftpd
   my $self = shift;  
   runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
   my $vals = $self->tftpd( args => 'off' );  
   =cut
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {  
     return $vals;  sub enable_tftpd
   } else {  {
     return undef;    my $self = shift;
   }  
 }    my $vals = $self->tftpd( args => 'on' );
   
 =pod    if ($vals->{'Tftpd'} eq 'listen') {
       return $vals;
 =item cmd    } else {
       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  
   =pod
 Also accepts these options:  
   =item disable_tftpd
 I<decode>  
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output  runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
   
 I<cmd_disconnects>  =cut
 - if this is true, it then sets logged_in() to false, then it will close() the connection and then sets is_connected() to false  
   sub disable_tftpd
 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    my $self = shift;
   
 I<args>    my $vals = $self->tftpd( args => 'off' );
 - a string containing the command line options that are passed to the command  
     if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
 =cut      return $vals;
     } else {
 sub cmd      return undef;
 {    }
   my $self = shift;  }
   
   my @valid_net_telnet_opts = qw(  =pod
     String  
     Output  =item cmd
     Cmd_remove_mode  
     Errmode  This does most of the work.  At the heart, it calls Net::Telnet::cmd()
     Input_record_separator  but it also does some special stuff for Trango.
     Ors  
     Output_record_separator  Normally returns the last lines from from the command
     Prompt  
     Rs  Also accepts these options:
     Timeout  
   );  I<decode>
   - if this is true, then it will send the output lines to _decode_lines()
   my %cfg;  and then returns the decoded output
   if (@_ == 1) {  
     $cfg{'String'} = shift;  I<cmd_disconnects>
   } elsif (@_ > 1) {  - if this is true, it then sets logged_in() to false, then it will
     %cfg = @_;  close() the connection and then sets is_connected() to false
   }  
   I<expect>
   $cfg{'Timeout'} ||= $self->Timeout;  - 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
   unless ($cfg{'String'}) {  command probably failed
     #$! = "No command passed";  
     #warn "No command passed\n";  I<args>
     return undef;  - a string containing the command line options that are passed to the
   }  command
   
   unless ($self->is_connected) {  =cut
     #$! = "Not connected";  
     #warn "Not connected\n";  sub cmd
     return undef;  {
   }    my $self = shift;
   
   unless ($self->logged_in) {    my @valid_net_telnet_opts = qw(
     #$! = "Not logged in";      String
     #warn "Not logged in\n";      Output
     return undef;      Cmd_remove_mode
   }      Errmode
       Input_record_separator
       Ors
   my %cmd;      Output_record_separator
   foreach (@valid_net_telnet_opts) {      Prompt
     if (exists $cfg{$_}) {      Rs
       $cmd{$_} = $cfg{$_};      Timeout
     }    );
   }  
   if ($cfg{'args'}) {    my %cfg;
     $cmd{'String'} .= ' ' . $cfg{'args'};    if (@_ == 1) {
   }      $cfg{'String'} = shift;
   my @lines;    } elsif (@_ > 1) {
   unless ($cfg{'no_prompt'}) {      %cfg = @_;
     @lines = $self->SUPER::cmd(%cmd);    }
   } else {  
     $self->print($cmd{'String'});    $cfg{'Timeout'} ||= $self->Timeout;
     @lines = $self->lastline;  
   }    unless ($cfg{'String'}) {
       #$! = "No command passed";
   $self->last_lines(\@lines);      #warn "No command passed\n";
       return undef;
   my $vals = 1;    }
   if ($PRIVATE{'Decode'} && $cfg{'decode'}) {  
     if ($cfg{'decode'} eq 'each') {    unless ($self->is_connected) {
       $vals = _decode_each_line(@lines);      #$! = "Not connected";
     } elsif ($cfg{'decode'} eq 'sulog') {      #warn "Not connected\n";
       $vals = _decode_sulog(@lines);      return undef;
     } elsif ($cfg{'decode'} eq 'maclist') {    }
       $vals = _decode_maclist(@lines);  
     } else {    unless ($self->logged_in) {
       $vals = _decode_lines(@lines);      #$! = "Not logged in";
     }      #warn "Not logged in\n";
   }      return undef;
     }
   $self->last_vals($vals);  
   
     my %cmd;
   my $last = $self->lastline;    foreach (@valid_net_telnet_opts) {
       if (exists $cfg{$_}) {
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {        $cmd{$_} = $cfg{$_};
     if ($cfg{'cmd_disconnects'}) {      }
       $self->logged_in(0);    }
       $self->close;    if ($cfg{'args'}) {
       $self->is_connected(0);      $cmd{'String'} .= ' ' . $cfg{'args'};
     }    }
     my @lines;
     if ($PRIVATE{'Decode'} && $cfg{'decode'}) {    unless ($cfg{'no_prompt'}) {
       return $vals;      @lines = $self->SUPER::cmd(%cmd);
     } else {    } else {
       return @lines;      $self->print($cmd{'String'});
     }      @lines = $self->lastline;
   } else {    }
     #$! = "Error with command ($cfg{'string'}): $last";  
     return undef;    $self->last_lines(\@lines);
   }  
 }    my $vals = 1;
     if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
 #=item _decode_lines      if ($cfg{'decode'} eq 'each') {
         $vals = _decode_each_line(@lines);
 sub _decode_lines      } elsif ($cfg{'decode'} eq 'sulog') {
 {        $vals = _decode_sulog(@lines);
   my @lines = @_;      } elsif ($cfg{'decode'} eq 'maclist') {
         $vals = _decode_maclist(@lines);
   my %conf;      } else {
         $vals = _decode_lines(@lines);
   my $key = '';      }
   my $val = '';    }
   my $in_key = 0;  
   my $in_val = 0;    $self->last_vals($vals);
   
   foreach my $line (@lines) {  
     next if $line =~ /$success$/;    my $last = $self->lastline;
   
     my @chars = split //, $line;    if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
       if ($cfg{'cmd_disconnects'}) {
     my $last_key = '';        $self->logged_in(0);
     foreach my $c (@chars) {        $self->close;
         $self->is_connected(0);
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {      }
         if ($c eq '[') {  
           $in_key = 1;      if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
           $in_val = 0;        return $vals;
         } else {      } else {
           $in_key = 0;        return @lines;
           $in_val = 0;      }
         }    } else {
       #$! = "Error with command ($cfg{'string'}): $last";
         if ($key) {      return undef;
           $key =~ s/^\s+//;    }
           $key =~ s/\s+$//;  }
   
           $val =~ s/^\s+//;  #=item _decode_lines
           $val =~ s/\s+$//;  
   sub _decode_lines
           if ($key eq 'Checksum' && $last_key) {  {
             # Special case for these bastids.    my @lines = @_;
             my $new = $last_key;  
             $new =~ s/\s+\S+$//;    my %conf;
             $key = $new . " " . $key;  
           }    my $key = '';
     my $val = undef;
           $last_key = $key;    my $in_key = 0;
           $conf{$key} = $val;    my $in_val = 1;
           $key = '';  
           $val = '';    foreach my $line (@lines) {
         }      next if $line =~ /$success$/;
   
       } elsif ($c eq ']') {      my @chars = split //, $line;
         $in_val = 1;  
         $in_key = 0;      my $last_key = '';
         $c = shift @chars;      foreach my $c (@chars) {
   
       } elsif ($in_key) {        if ($c eq '[' || $c eq "\r" || $c eq "\n") {
         $key .= $c;          if ($c eq '[') {
             $in_key = 1;
       } elsif ($in_val) {            $in_val = 0;
         $val .= $c;          } else {
       }            $in_key = 0;
     }            $in_val = 1;
   }          }
   
   if (%conf) {          if ($key) {
     return \%conf;            $key =~ s/^\s+//;
   } else {            $key =~ s/\s+$//;
     return undef;  
   }            if (defined $val) {
 }              $val =~ s/^\s+//;
               $val =~ s/\s+$//;
 #=item _decode_each_line            }
   
 sub _decode_each_line            if ($key eq 'Checksum' && $last_key) {
 {              # Special case for these bastids.
   my @lines = @_;              my $new = $last_key;
   my @decoded;              $new =~ s/\s+\S+$//;
   foreach my $line (@lines) {              $key = $new . " " . $key;
     my $decoded = _decode_lines($line);            }
     push @decoded, $decoded if defined $decoded;  
   }            $last_key = $key;
   return \@decoded;            $conf{$key} = $val;
 }            $key = '';
             $val = '';
 #=item _decode_sulog          }
   
 sub _decode_sulog        } elsif ($c eq ']') {
 {          $in_val = 1;
   my @lines = @_;          $in_key = 0;
   my @decoded;          $c = shift @chars;
   my $last_tm;  
   foreach my $line (@lines) {        } elsif ($in_key) {
     my $decoded = _decode_lines($line);          $key .= $c;
   
     if (defined $decoded) {        } elsif ($in_val) {
       if ($decoded->{'tm'}) {          $val .= $c;
         $last_tm = $decoded->{'tm'};        }
         next;      }
       } else {    }
         $decoded->{'tm'} = $last_tm;  
       }    if (%conf) {
       next unless $last_tm;      return \%conf;
     } else {
       push @decoded, $decoded if defined $decoded;      return $val;
     }    }
   }  }
   return \@decoded;  
 }  #=item _decode_each_line
   
 #=item _decode_maclist  sub _decode_each_line
   {
 sub _decode_maclist    my @lines = @_;
 {    my @decoded;
         my @lines = @_;    foreach my $line (@lines) {
         my @decoded;      my $decoded = _decode_lines($line);
         my $total_entries = 0;      push @decoded, $decoded if defined $decoded;
         foreach my $line (@lines) {    }
                 $line =~ s/\r?\n$//;    return \@decoded;
                 my ($mac, $loc, $tm) = $line =~ /  }
                         ([0-9a-fA-F ]{17})\s+  
                         (.*)\s+  #=item _decode_sulog
                         tm\s+  
                         (\d+)  sub _decode_sulog
                 /x;  {
     my @lines = @_;
                 if ($mac) {    my @decoded;
                         $mac =~ s/\s+//g;    my $last_tm;
                         $loc =~ s/^\s+//;    foreach my $line (@lines) {
                         $loc =~ s/\s+$//;      my $decoded = _decode_lines($line);
   
                         my $suid = undef;      if (defined $decoded) {
                         if ($loc =~ /suid\s+=\s+(\d+)/) {        if ($decoded->{'tm'}) {
                                 $suid = $1;          $last_tm = $decoded->{'tm'};
                                 $loc = undef;          next;
                         }        } else {
           $decoded->{'tm'} = $last_tm;
                         push @decoded, {        }
                                 mac  => $mac,        next unless $last_tm;
                                 loc  => $loc,  
                                 tm   => $tm,        push @decoded, $decoded if defined $decoded;
                                 suid => $suid,      }
                         };    }
                 } elsif ($line =~ /(\d+)\s+entries/) {    return \@decoded;
                         $total_entries = $1;  }
                 }  
         }  #=item _decode_maclist
         if (scalar @decoded == $total_entries) {  
                 return \@decoded;  sub _decode_maclist
         } else {  {
                 # XXX we should have a way to set last error, not sure why we don't          my @lines = @_;
                 return undef;          my @decoded;
         }          my $total_entries = 0;
 }          my $current_tm = 0;
           foreach my $line (@lines) {
 1;                  $line =~ s/\r?\n$//;
 __END__                  my ($mac, $loc, $tm) = $line =~ /
                           ([0-9a-fA-F ]{17})\s+
 =back                          (.*)\s+
                           tm\s+
 =head1 SEE ALSO                          (\d+)
                   /x;
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm  
                   if ($mac) {
 L<Net::Telnet>                          $mac =~ s/\s+//g;
                           $loc =~ s/^\s+//;
 =head1 TODO                          $loc =~ s/\s+$//;
   
 There are still a lot of commands that are not accessed directly.  If you call them (as cmd("command + args") or whatever) and it works, please send me examples that work and I will try to get it incorporated into the next version of the script.                          my $suid = undef;
                           if ($loc =~ /suid\s+=\s+(\d+)/) {
 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.                                  $suid = $1;
                                   $loc = undef;
 =head1 AUTHOR                          }
   
 Andrew Fresh E<lt>andrew@rraz.netE<gt>                          push @decoded, {
                                   mac  => $mac,
 =head1 COPYRIGHT AND LICENSE                                  loc  => $loc,
                                   tm   => $tm,
 Copyright (C) 2005 by Andrew Fresh                                  suid => $suid,
                           };
 This library is free software; you can redistribute it and/or modify                  } elsif ($line =~ /(\d+)\s+entries/) {
 it under the same terms as Perl itself, either Perl version 5.8.7 or,                          $total_entries = $1;
 at your option, any later version of Perl 5 you may have available.                  } elsif ($line =~ /current tm = (\d+)\s+sec/) {
                           $current_tm = $1
                   }
 =cut          }
   
           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.24

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