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

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

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