[BACK]Return to Trango.pm CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / lib / Net / Telnet

Diff for /trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm between version 1.10 and 1.25

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

Legend:
Removed from v.1.10  
changed lines
  Added in v.1.25

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