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

version 1.9, 2006/07/14 02:17:29 version 1.24, 2007/01/17 19:00:51
Line 1 
Line 1 
 package Net::Telnet::Trango;  package Net::Telnet::Trango;
 # $RedRiver: Trango.pm,v 1.8 2006/06/29 00:39:52 andrew Exp $  # $RedRiver: Trango.pm,v 1.23 2007/01/08 22:16:29 mike Exp $
 use strict;  use strict;
 use warnings;  use warnings;
 use base 'Net::Telnet';  use base 'Net::Telnet';
   
 =pod  =pod
   
 =head1 NAME  =head1 NAME
   
 Net::Telnet::Trango - Perl extension for accessing the Trango telnet interface  Net::Telnet::Trango - Perl extension for accessing the Trango telnet interface
   
 =head1 SYNOPSIS  =head1 SYNOPSIS
   
   use Net::Telnet::Trango;    use Net::Telnet::Trango;
   my $t = new Net::Telnet::Trango ( Timeout => 5 );    my $t = new Net::Telnet::Trango ( Timeout => 5 );
     
   $t->open( Host => $fox ) or die "Error connecting: $!";    $t->open( Host => $fox ) or die "Error connecting: $!";
   
   $t->login('password') or die "Couldn't log in: $!";    $t->login('password') or die "Couldn't log in: $!";
     
   # Do whatever    # Do whatever
     
   $t->exit;    $t->exit;
   $t->close;    $t->close;
   
 =head1 DESCRIPTION  =head1 DESCRIPTION
   
 Perl access to the telnet interface on Trango Foxes, SUs and APs.  Perl access to the telnet interface on Trango Foxes, SUs and APs.
   
 Another handy feature is that it will parse the output from certain commands that is in the format "[key1] value1 [key2] value2" and put those in a hashref that is returned.  This makes using the output from things like sysinfo very easy to do.  Another handy feature is that it will parse the output from certain
   commands that is in the format "[key1] value1 [key2] value2" and put
 =head2 EXPORT  those in a hashref that is returned.  This makes using the output from
   things like sysinfo very easy to do.
 None  
   =head2 EXPORT
 =cut  
   None
 our $VERSION = '0.01';  
   =head1 METHODS
 my %PRIVATE = (  
   is_connected => 0,  =over
   logged_in => 0,  
 );  =cut
   
 =pod  our $VERSION = '0.01';
   
 =item new  my %PRIVATE = (
     is_connected => 0,
 Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'    logged_in => 0,
   );
 It also takes an optional parameter 'Decode'.  If not defined it  
 defaults to 1, if it is set to 0, it will not decode the output and  =pod
 instead return an array of the lines that were returned from the  
 command.  =item new
   
 =cut  Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'
   
 sub new  It also takes an optional parameter 'Decode'.  If not defined it
 {  defaults to 1, if it is set to 0, it will not decode the output and
   my $class = shift;  instead return an array of the lines that were returned from the
   command.
   my %args;  
   if (@_ == 1) {  =cut
     $args{'Host'} = shift;  
   } else {  sub new
     %args = @_;  {
   }    my $class = shift;
   
   $args{'Prompt'}  ||= '/#> *$/';    my %args;
     if (@_ == 1) {
   foreach my $key (keys %args) {      $args{'Host'} = shift;
     $PRIVATE{$key} = $args{$key};    } else {
   }      %args = @_;
   $PRIVATE{'Decode'} = 1 unless defined $PRIVATE{'Decode'};    }
   delete $args{'Decode'};  
     $args{'Prompt'}  ||= '/#> *$/';
   my $self = $class->SUPER::new(%args);  
   bless $self if ref $self;    foreach my $key (keys %args) {
       $PRIVATE{$key} = $args{$key};
   return $self;    }
 }    $PRIVATE{'Decode'} = 1 unless defined $PRIVATE{'Decode'};
     delete $args{'Decode'};
 #  _password <new password> <new password>  
 #  ? [command]    my $self = $class->SUPER::new(%args);
 #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...    bless $self if ref $self;
 #  arp -bcast <on|off>  
 #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...    return $self;
 #  bye  }
 #  cf2cf ap [default|<size>]  
 #  date  #  _password <new password> <new password>
 #  date <month> <day> <year>  #  ? [command]
 #  freq scantable  #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...
 #  freq channeltable  #  arp -bcast <on|off>
 #  freq writescan [<ch#> <h|v>]  #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...
 #  freq writechannel [<ch#> <freq>] ...  #  bye
 #  freq <ch #> <h|v>  #  cf2cf ap [default|<size>]
 #  help [command]  #  date
 #  heater [<on temp> <off temp>]  #  date <month> <day> <year>
 #  ipconfig [<new ip> <new subnet mask> <new gateway>]  #  freq scantable
 #  log [<# of entries, 1..179>]  #  freq channeltable
 #  log <sum> <# of entries, 1..179>  #  freq writescan [<ch#> <h|v>]
 #  logout  #  freq writechannel [<ch#> <freq>] ...
 #  opmode [ap [y]]  #  freq <ch #> <h|v>
 #  password  #  help [command]
 #  ping <ip addr>  #  heater [<on temp> <off temp>]
 #  polar <h|v>  #  ipconfig [<new ip> <new subnet mask> <new gateway>]
 #  power <setism|setunii> <max|min|<dBm>>  #  log [<# of entries, 1..179>]
 #  reboot  #  log <sum> <# of entries, 1..179>
 #  restart  #  logout
 #  remarks [<str>]  #  opmode [ap [y]]
 #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  password
 #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]  #  ping <ip addr>
 #  sysinfo  #  polar <h|v>
 #  set suid <id>  #  power <setism|setunii> <max|min|<dBm>>
 #  set apid <id>  #  reboot
 #  set baseid <id>  #  restart
 #  set defaultopmode [<ap|su> <min,0..10>]  #  remarks [<str>]
 #  set defaultopmode off  #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  set snmpcomm [<read | write | trap (id or setall)> <str>]  #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]
 #  set mir [on|off]  #  sysinfo
 #  set mir threshold <kbps>  #  set suid <id>
 #  set rssitarget [<ism|unii> <dBm>]  #  set apid <id>
 #  set serviceradius [<ism | unii> <miles>]  #  set baseid <id>
 #  ssrssi <ch #> <h|v>  #  set defaultopmode [<ap|su> <min,0..10>]
 #  su [<suid>|all]  #  set defaultopmode off
 #  su changechannel <all|suid> <ch#> <h|v>  #  set snmpcomm [<read | write | trap (id or setall)> <str>]
 #  su ipconfig <suid> <new ip> <new subnet> <new gateway>  #  set mir [on|off]
 #  su [live|poweroff|priority]  #  set mir threshold <kbps>
 #  su <ping|info|status> <suid>  #  set rssitarget [<ism|unii> <dBm>]
 #  su powerleveling <all|suid>  #  set serviceradius [<ism | unii> <miles>]
 #  su reboot <all|suid>  #  ssrssi <ch #> <h|v>
 #  su restart <all|suid>  #  su [<suid>|all]
 #  su testrflink <all|suid> [r]  #  su changechannel <all|suid> <ch#> <h|v>
 #  su testrflink <setlen> [64..1600]  #  su ipconfig <suid> <new ip> <new subnet> <new gateway>
 #  su testrflink <aptx> [20..100]  #  su [live|poweroff|priority]
 #  su sw <suid|all> <sw #> <on|off>  #  su <ping|info|status> <suid>
 #  sudb [dload | view]  #  su powerleveling <all|suid>
 #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>  #  su reboot <all|suid>
 #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>  #  su restart <all|suid>
 #  sudb delete <all|<suid>>  #  su testrflink <all|suid> [r]
 #  sudb modify <suid> <cir|mir> <kbps>  #  su testrflink <setlen> [64..1600]
 #  sudb modify <suid> <su2su> <group id,hex>  #  su testrflink <aptx> [20..100]
 #  sudb view  #  su sw <suid|all> <sw #> <on|off>
 #  sulog [lastmins | sampleperiod <1..60>]  #  sudb [dload | view]
 #  sulog [<# of entry,1..18>]  #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>
 #  survey <ism|unii> <time, sec> <h|v>  #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>
 #  sw [<sw #> <on|off>]  #  sudb delete <all|<suid>>
 #  temp  #  sudb modify <suid> <cir|mir> <kbps>
 #  tftpd [on|off]  #  sudb modify <suid> <su2su> <group id,hex>
 #  time  #  sudb view
 #  time <hour> <min> <sec>  #  sulog [lastmins | sampleperiod <1..60>]
 #  save <mainimage|fpgaimage> <current chksum> <new chksum>  #  sulog [<# of entry,1..18>]
 #  save <systemsetting|sudb>  #  survey <ism|unii> <time, sec> <h|v>
 #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>  #  sw [<sw #> <on|off>]
 #  updateflash <systemsetting|sudb>  #  temp
   #  tftpd [on|off]
 =pod  #  time
   #  time <hour> <min> <sec>
 =head1 METHODS  #  save <mainimage|fpgaimage> <current chksum> <new chksum>
   #  save <systemsetting|sudb>
 =head2 ACCESSORS  #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>
   #  updateflash <systemsetting|sudb>
 =over  
   =pod
 =item Host  
   =back
 returns the name of the host that you are accessing  
   =head2 ACCESSORS
 =item firmware_version  
   =over
 returns the firmware version on the trango if available otherwise undef.    
 Available after a successful open()  =item Host
 This is usually only set internally  
   returns the name of the host that you are accessing
 =item host_type  
   =item firmware_version
 returns the type of host from the login banner for example M5830S or M5300S.    
 Available after a successful open()  returns the firmware version on the trango if available otherwise undef.
 This is usually only set internally  Available after a successful open()
   This is usually only set internally
 =item is_connected  
   =item host_type
 returns 1 after a successful open() otherwise undef  
 This is usually only set internally  returns the type of host from the login banner for example M5830S or M5300S.
   Available after a successful open()
 =item logged_in  This is usually only set internally
   
 returns 1 after a successful login() 0 if it failed and undef if  =item is_connected
 login() was never called  
 This is usually only set internally  returns 1 after a successful open() otherwise undef
   This is usually only set internally
 =item login_banner  
   =item logged_in
 returns the banner that is displayed when first connected at login.  Only set after a successful open()  
   returns 1 after a successful login() 0 if it failed and undef if
 This is usually only set internally  login() was never called
   This is usually only set internally
 =item last_lines  
   =item login_banner
 returns the output from the last cmd() that was run as an array ref  
 This is usually only set internally  returns the banner that is displayed when first connected at login.
   Only set after a successful open()
 =back  
   This is usually only set internally
 =head2 ALIASES  
   =item last_lines
 =over  
   returns the output from the last cmd() that was run as an array ref
 =item bye  This is usually only set internally
   
 alias of exit()  =back
   
 =item restart  =head2 ALIASES
   
 alias of reboot()  =over
   
 =back  =item bye
   
 =head2 COMMANDS  alias of exit()
   
 Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>, as such they accept the same options as C<cmd()>.  Specifically they take a named paramater "args", for example:  =item restart
 C<tftpd(args =E<gt> 'on')> would enable tftpd  
   alias of reboot()
 =over  
   =back
 =item tftpd  
   =head2 COMMANDS
 Returns a hash ref of the decoded output from the command.  
   Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
 Also see enable_tftpd() and disable_tftpd() as those check for correct output  as such they accept the same options as C<cmd()>.
   Specifically they take a named paramater "args", for example:
 =item ver  C<tftpd(args =E<gt> 'on')> would enable tftpd
   
 Returns a hash ref of the decoded output from the command.  =over
   
 =item sysinfo  =item tftpd
   
 Returns a hash ref of the decoded output from the command.  Returns a hash ref of the decoded output from the command.
   
 =item exit  Also see enable_tftpd() and disable_tftpd() as those check for correct output
   
 exits the command session with the trango and closes the connection  =item ver
   
 =item reboot  Returns a hash ref of the decoded output from the command.
   
 reboots the trango and closes the connection  =item sysinfo
   
 =item sulog  Returns a hash ref of the decoded output from the command.
   
 returns an array ref of hashes containing each log line.  =item exit
   
 =item save_sudb  exits the command session with the trango and closes the connection
   
 returns true on success, undef on failure  =item reboot
   
 =item syslog  reboots the trango and closes the connection
   
 returns the output from the syslog command  =item remarks
   
 =item pipe  Takes an optional argument, which sets the remarks.
   If there is no argument, returns the current remarks.
 returns the output from the pipe command  
   =item sulog
 =item maclist  
   returns an array ref of hashes containing each log line.
 returns the output from the maclist command  
   =item save_sudb
 =item eth_list  
   returns true on success, undef on failure
 returns the output from the eth list command  
   =item syslog
 =cut  
   returns the output from the syslog command
   
 my $success = 'Success.';  =item pipe
 my %COMMANDS = (  
   tftpd       => { decode => 'all',   expect => $success },  returns the output from the pipe command
   ver         => { decode => 'all' },  
   sysinfo     => { decode => 'all',   expect => $success },  =item maclist
   updateflash => { decode => 'all',   expect => $success },  
   sulog       => { decode => 'sulog', expect => $success },  returns the output from the maclist command
   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },  
   reboot      => { no_prompt => 1, cmd_disconnects => 1 },  =item maclist_reset
   save_sudb   => { String => "save sudb", expect => $success },  
   syslog      => { expect => $success },  resets the maclist.  No useful output.
   'pipe'      => { }, # XXX needs a special decode  
   maclist     => { }, # XXX needs a special decode and a special expect  =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  );
   
 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+)$/
 {      or die "Weird: $AUTOLOAD";
   my $self = shift;  
     if (exists $ALIASES{$method}) {
   unless ( $self->SUPER::open(@_) ) {      $method = $ALIASES{$method};
     #$! = "Couldn't connect to " . $self->Host . ":  $!";      return $self->$method(@_);
     return undef;    }
   }  
     if (exists $COMMANDS{$method}) {
   ## Get to login prompt      $COMMANDS{$method}{'String'} ||= $method;
   unless ($self->waitfor(      $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
       -match => '/password: ?$/i',      return $self->cmd(%{ $COMMANDS{$method} }, @_);
       -errmode => "return",    }
     ) ) {  
     #$! = "problem connecting to host (" . $self->Host . "): " .    if (exists $ACCESS{$method}) {
     #    $self->lastline;      my $prev = $PRIVATE{$method};
     return undef;      ($PRIVATE{$method}) = @_ if @_;
   }      return $prev;
     }
   $self->parse_login_banner($self->lastline);  
     $method = "SUPER::$method";
   $self->is_connected(1);    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
   you are ready to login() and parses the login banner so you can get
 Calls open() if not already connected, then sends the password and sets logged_in() if successful  host_type() and firmware_version()
   
 =cut  =cut
   
 sub login  sub open
 {  {
   my $self = shift;    my $self = shift;
   
   unless ($self->is_connected) {    unless ( $self->SUPER::open(@_) ) {
     $self->open or return undef;      #$! = "Couldn't connect to " . $self->Host . ":  $!";
   }      return undef;
     }
   my $password = shift;  
     ## Get to login prompt
   $self->print($password);    unless ($self->waitfor(
   unless ($self->waitfor(        -match => '/password: ?$/i',
     -match => $self->prompt,        -errmode => "return",
     -errmode => "return",      ) ) {
   ) ) {      #$! = "problem connecting to host (" . $self->Host . "): " .
     #$! = "login ($self->Host) failed: " . $self->lastline;      #    $self->lastline;
     return undef;      return undef;
   }    }
   
   $self->logged_in(1);    $self->parse_login_banner($self->lastline);
   
   return $self->logged_in;    $self->is_connected(1);
 }  
     return $self->is_connected;
 =pod  }
   
 =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
   logged_in() if successful
 sub parse_login_banner  
 {  =cut
   my $self = shift;  
   sub login
   if (@_) {  {
     $self->login_banner(@_);    my $self = shift;
   }  
     unless ($self->is_connected) {
   my $banner = $self->login_banner;      $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->print($password);
   $self->host_type($type);    unless ($self->waitfor(
   $self->firmware_version($ver);      -match => $self->prompt,
       -errmode => "return",
   return 1;    ) ) {
 }      #$! = "login ($self->Host) failed: " . $self->lastline;
       return undef;
 =pod    }
   
 =item su_password    $self->logged_in(1);
   
 C<su_password('all'|suid, 'new_password')>    return $self->logged_in;
   }
 =cut  
   =pod
 sub su_password  
 {  =item parse_login_banner
   my $self     = shift;  
   my $su       = shift || '!';  Takes a login banner (what you get when you first connect to the Trango)
   my $new_pass = shift || '';  or reads what is already in login_banner() then parses it and sets
   host_type() and firmware_version() as well as login_banner()
   unless (defined $su) {  
     warn "No su passed!"  =cut
     #return undef;  
   }  sub parse_login_banner
   {
   unless (defined $new_pass) {    my $self = shift;
     warn "No new password!"    
     #return undef;    if (@_) {
   }      $self->login_banner(@_);
     }
   return $self->cmd(String => 'su password ' .  
                      $su . ' ' .    my $banner = $self->login_banner;
                      $new_pass . ' ' .  
                      $new_pass,    my ($type, $ver) = $banner =~
                      expect => $success,      /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
                     );  
 }    $self->login_banner($banner);
     $self->host_type($type);
 =pod    $self->firmware_version($ver);
   
 =item sudb_view    return 1;
   }
 returns a reference to an array of hashes each containing:  
   =pod
   suid  
   type  =item su_password
   cir  
   mir  C<su_password('new_password'[, 'suid'])> If no suid is specified,
   mac  the default is "all".
   
 =cut  =cut
   
 sub sudb_view  sub su_password
 {  {
   my $self = shift;    my $self     = shift;
     my $new_pass = shift || '';
   my @lines = $self->cmd( String => 'sudb view', expect => $success );    my $su       = shift || 'all';
   
   return undef unless @lines;    unless (defined $new_pass) {
       warn "No new password!"
   unless ($PRIVATE{'Decode'}) {      #return undef;
     return @lines;    }
   }  
     return $self->cmd(String => 'su password ' .
   my @sus;                       $su . ' ' .
   foreach (@lines) {                       $new_pass . ' ' .
     next unless $_;                       $new_pass,
     if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {                       expect => $success,
       my %s = (                      );
         suid => $1,  }
         type => $2,  
         cir  => $3,  =pod
         mir  => $4,  
         mac  => $5,  =item su_ipconfig
       );  
   C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
           $s{'mac'} =~ s/\s//g;  
           $s{'mac'} = uc($s{'mac'});  =cut
   
       push @sus, \%s;  sub su_ipconfig
     }  {
   }          my $self        = shift;
   
   return \@sus;          my $suid        = shift;
 }          my $new_ip      = shift;
           my $new_subnet  = shift;
 =pod          my $new_gateway = shift;
   
 =item sudb_add          return undef unless $suid =~ /^\d+$/;
           return undef unless $new_ip;
 Takes the following paramaters          return undef unless $new_subnet;
           return undef unless $new_gateway;
         suid : numeric,  
         type : (reg|pr)          # su ipconfig <suid> <new ip> <new subnet> <new gateway>
         cir  : numeric,          return $self->cmd(String => 'su ipconfig ' .
         mir  : numeric,                       $suid       . ' ' .
         mac  : Almost any format, it will be reformatted,                       $new_ip     . ' ' .
                        $new_subnet . ' ' .
 and returns true on success or undef otherwise.                       $new_gateway,
                        expect => $success,
 You should save_sudb() after calling this, or your changes  will be lost                      );
 when the AP is rebooted.  }
   
 =cut  =pod
   
 sub sudb_add  =item sudb_view
 {  
         my $self = shift;  returns a reference to an array of hashes each containing:
         my $suid = shift;  
         my $type = shift;    suid
         my $cir  = shift;    type
         my $mir  = shift;    cir
         my $mac  = shift;    mir
     mac
         if ($suid =~ /\D/) {  
                 return undef;  =cut
         }  
   sub sudb_view
         unless (lc($type) eq 'reg' || lc($type) eq 'pr') {  {
                 warn "Invalid type '$type'!";    my $self = shift;
                 return undef;  
         }    my @lines = $self->cmd( String => 'sudb view', expect => $success );
   
         if ($cir =~ /\D/) {    return undef unless @lines;
                 warn "Invalid CIR '$cir'!";  
                 return undef;    unless ($PRIVATE{'Decode'}) {
         }      return @lines;
     }
         if ($mir =~ /\D/) {  
                 warn "Invalid MIR '$mir'!";    my @sus;
                 return undef;    foreach (@lines) {
         }      next unless $_;
       if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
         my $new_mac = $mac;        my %s = (
         $new_mac =~ s/[^0-9A-Fa-f]//;          suid => $1,
         unless (length $new_mac == 12) {          type => $2,
                 warn "Invalid MAC '$mac'!";          cir  => $3,
                 return undef;          mir  => $4,
         }          mac  => $5,
         $new_mac = join ' ', $new_mac =~ /../g;        );
   
         my $string = 'sudb add ' .            $s{'mac'} =~ s/\s//g;
                 $suid . ' ' .            $s{'mac'} = uc($s{'mac'});
                 $type . ' ' .  
                 $cir  . ' ' .        push @sus, \%s;
                 $mir  . ' ' .      }
                 $new_mac;    }
   
     return \@sus;
         return $self->cmd( String => $string, expect => $success );  }
 }  
   =pod
 =pod  
   =item sudb_add
 =item sudb_delete  
   Takes the following paramaters
 Takes either 'all' or the  suid of the su to delete  
 and returns true on success or undef otherwise.          suid : numeric,
           type : (reg|pr)
 You should save_sudb() after calling this, or your changes  will be lost          cir  : numeric,
 when the AP is rebooted.          mir  : numeric,
           mac  : Almost any format, it will be reformatted,
 =cut  
   and returns true on success or undef otherwise.
 sub sudb_delete  
 {  You should save_sudb() after calling this, or your changes  will be lost
         my $self = shift;  when the AP is rebooted.
         my $suid = shift;  
   =cut
         if (lc($suid) ne 'all' || $suid =~ /\D/) {  
                 return undef;  sub sudb_add
         }  {
           my $self = shift;
         return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );          my $suid = shift;
 }          my $type = shift;
           my $cir  = shift;
 =pod          my $mir  = shift;
           my $mac  = shift;
 =item sudb_modify  
           if ($suid =~ /\D/) {
 Takes either the  suid of the su to delete                  return undef;
 as well as what you are changing, either "cir, mir or su2su"          }
 and returns true on success or undef otherwise.  
           unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
 cir and mir also take a value to set the cir/mir to.                  warn "Invalid type '$type'!";
                   return undef;
 su2su takes a group id parameter that is in hex.          }
   
 You should save_sudb() after calling this, or your changes  will be lost          if ($cir =~ /\D/) {
 when the AP is rebooted.                  warn "Invalid CIR '$cir'!";
                   return undef;
 =cut          }
   
 sub sudb_modify          if ($mir =~ /\D/) {
 {                  warn "Invalid MIR '$mir'!";
         my $self  = shift;                  return undef;
         my $suid  = shift;          }
         my $opt   = shift;  
         my $value = shift;          my $new_mac = $mac;
           $new_mac =~ s/[^0-9A-Fa-f]//;
         if ($suid =~ /\D/) {          unless (length $new_mac == 12) {
                 return undef;                  warn "Invalid MAC '$mac'!";
         }                  return undef;
           }
         if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {          $new_mac = join ' ', $new_mac =~ /../g;
                 if ($value =~ /\D/) {  
                         return undef;          my $string = 'sudb add ' .
                 }                  $suid . ' ' .
         } elsif (lc($opt) eq 'su2su') {                  $type . ' ' .
                 if ($value =~ /[^0-9A-Za-f]/) {                  $cir  . ' ' .
                         return undef;                  $mir  . ' ' .
                 }                  $new_mac;
         } else {  
                 return undef;  
         }          return $self->cmd( String => $string, expect => $success );
   }
         my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;  
   =pod
         return $self->cmd( String => $string, expect => $success );  
 }  =item sudb_delete
   
 =pod  Takes either 'all' or the  suid of the su to delete
   and returns true on success or undef otherwise.
 =item enable_tftpd  
   You should save_sudb() after calling this, or your changes  will be lost
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing  when the AP is rebooted.
   
 =cut  =cut
   
 sub enable_tftpd  sub sudb_delete
 {  {
   my $self = shift;          my $self = shift;
           my $suid = shift;
   my $vals = $self->tftpd( args => 'on' );  
           if (lc($suid) ne 'all' || $suid =~ /\D/) {
   if ($vals->{'Tftpd'} eq 'listen') {                  return undef;
     return $vals;          }
   } else {  
     return undef;          return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
   }  }
 }  
   =pod
 =pod  
   =item sudb_modify
 =item disable_tftpd  
   Takes either the  suid of the su to delete
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'  as well as what you are changing, either "cir, mir or su2su"
   and returns true on success or undef otherwise.
 =cut  
   cir and mir also take a value to set the cir/mir to.
 sub disable_tftpd  
 {  su2su takes a group id parameter that is in hex.
   my $self = shift;  
   You should save_sudb() after calling this, or your changes  will be lost
   my $vals = $self->tftpd( args => 'off' );  when the AP is rebooted.
   
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {  =cut
     return $vals;  
   } else {  sub sudb_modify
     return undef;  {
   }          my $self  = shift;
 }          my $suid  = shift;
           my $opt   = shift;
 =pod          my $value = shift;
   
 =item cmd          if ($suid =~ /\D/) {
                   return undef;
 This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.          }
   
 Normally returns the last lines from from the command          if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
                   if ($value =~ /\D/) {
 Also accepts these options:                          return undef;
                   }
 I<decode>          } elsif (lc($opt) eq 'su2su') {
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output                  if ($value =~ /[^0-9A-Za-f]/) {
                           return undef;
 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          } else {
                   return undef;
 I<expect>          }
 - if this is set (usually to 'Success.') it will check for that in the last line of output and if it does not, will return undef because the command probably failed  
           my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
 I<args>  
 - a string containing the command line options that are passed to the command          return $self->cmd( String => $string, expect => $success );
   }
 =cut  
   =pod
 sub cmd  
 {  =item enable_tftpd
   my $self = shift;  
   runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
   my @valid_net_telnet_opts = qw(  
     String  =cut
     Output  
     Cmd_remove_mode  sub enable_tftpd
     Errmode  {
     Input_record_separator    my $self = shift;
     Ors  
     Output_record_separator    my $vals = $self->tftpd( args => 'on' );
     Prompt  
     Rs    if ($vals->{'Tftpd'} eq 'listen') {
     Timeout      return $vals;
   );    } else {
       return undef;
   my %cfg;    }
   if (@_ == 1) {  }
     $cfg{'String'} = shift;  
   } elsif (@_ > 1) {  =pod
     %cfg = @_;  
   }  =item disable_tftpd
   
   $cfg{'Timeout'} ||= $self->Timeout;  runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
   
   unless ($cfg{'String'}) {  =cut
     #$! = "No command passed";  
     #warn "No command passed\n";  sub disable_tftpd
     return undef;  {
   }    my $self = shift;
   
   unless ($self->is_connected) {    my $vals = $self->tftpd( args => 'off' );
     #$! = "Not connected";  
     #warn "Not connected\n";    if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
     return undef;      return $vals;
   }    } else {
       return undef;
   unless ($self->logged_in) {    }
     #$! = "Not logged in";  }
     #warn "Not logged in\n";  
     return undef;  =pod
   }  
   =item cmd
   
   my %cmd;  This does most of the work.  At the heart, it calls Net::Telnet::cmd()
   foreach (@valid_net_telnet_opts) {  but it also does some special stuff for Trango.
     if (exists $cfg{$_}) {  
       $cmd{$_} = $cfg{$_};  Normally returns the last lines from from the command
     }  
   }  Also accepts these options:
   if ($cfg{'args'}) {  
     $cmd{'String'} .= ' ' . $cfg{'args'};  I<decode>
   }  - if this is true, then it will send the output lines to _decode_lines()
   my @lines;  and then returns the decoded output
   unless ($cfg{'no_prompt'}) {  
     @lines = $self->SUPER::cmd(%cmd);  I<cmd_disconnects>
   } else {  - if this is true, it then sets logged_in() to false, then it will
     $self->print($cmd{'String'});  close() the connection and then sets is_connected() to false
     @lines = $self->lastline;  
   }  I<expect>
   - if this is set (usually to 'Success.') it will check for that in the
   $self->last_lines(\@lines);  last line of output and if it does not, will return undef because the
   command probably failed
   my $vals = 1;  
   if ($PRIVATE{'Decode'} && $cfg{'decode'}) {  I<args>
     if ($cfg{'decode'} eq 'each') {  - a string containing the command line options that are passed to the
       $vals = _decode_each_line(@lines);  command
     } elsif ($cfg{'decode'} eq 'sulog') {  
       $vals = _decode_sulog(@lines);  =cut
     } else {  
       $vals = _decode_lines(@lines);  sub cmd
     }  {
   }    my $self = shift;
   
   $self->last_vals($vals);    my @valid_net_telnet_opts = qw(
       String
       Output
   my $last = $self->lastline;      Cmd_remove_mode
       Errmode
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {      Input_record_separator
     if ($cfg{'cmd_disconnects'}) {      Ors
       $self->logged_in(0);      Output_record_separator
       $self->close;      Prompt
       $self->is_connected(0);      Rs
     }      Timeout
     );
     if ($PRIVATE{'Decode'} && $cfg{'decode'}) {  
       return $vals;    my %cfg;
     } else {    if (@_ == 1) {
       return @lines;      $cfg{'String'} = shift;
     }    } elsif (@_ > 1) {
   } else {      %cfg = @_;
     #$! = "Error with command ($cfg{'string'}): $last";    }
     return undef;  
   }    $cfg{'Timeout'} ||= $self->Timeout;
 }  
     unless ($cfg{'String'}) {
 #=item _decode_lines      #$! = "No command passed";
       #warn "No command passed\n";
 sub _decode_lines      return undef;
 {    }
   my @lines = @_;  
     unless ($self->is_connected) {
   my %conf;      #$! = "Not connected";
       #warn "Not connected\n";
   my $key = '';      return undef;
   my $val = '';    }
   my $in_key = 0;  
   my $in_val = 0;    unless ($self->logged_in) {
       #$! = "Not logged in";
   foreach my $line (@lines) {      #warn "Not logged in\n";
     next if $line =~ /$success$/;      return undef;
     }
     my @chars = split //, $line;  
   
     my $last_key = '';    my %cmd;
     foreach my $c (@chars) {    foreach (@valid_net_telnet_opts) {
       if (exists $cfg{$_}) {
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {        $cmd{$_} = $cfg{$_};
         if ($c eq '[') {      }
           $in_key = 1;    }
           $in_val = 0;    if ($cfg{'args'}) {
         } else {      $cmd{'String'} .= ' ' . $cfg{'args'};
           $in_key = 0;    }
           $in_val = 0;    my @lines;
         }    unless ($cfg{'no_prompt'}) {
       @lines = $self->SUPER::cmd(%cmd);
         if ($key) {    } else {
           $key =~ s/^\s+//;      $self->print($cmd{'String'});
           $key =~ s/\s+$//;      @lines = $self->lastline;
     }
           $val =~ s/^\s+//;  
           $val =~ s/\s+$//;    $self->last_lines(\@lines);
   
           if ($key eq 'Checksum' && $last_key) {    my $vals = 1;
             # Special case for these bastids.    if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
             my $new = $last_key;      if ($cfg{'decode'} eq 'each') {
             $new =~ s/\s+\S+$//;        $vals = _decode_each_line(@lines);
             $key = $new . " " . $key;      } elsif ($cfg{'decode'} eq 'sulog') {
           }        $vals = _decode_sulog(@lines);
       } elsif ($cfg{'decode'} eq 'maclist') {
           $last_key = $key;        $vals = _decode_maclist(@lines);
           $conf{$key} = $val;      } else {
           $key = '';        $vals = _decode_lines(@lines);
           $val = '';      }
         }    }
   
       } elsif ($c eq ']') {    $self->last_vals($vals);
         $in_val = 1;  
         $in_key = 0;  
         $c = shift @chars;    my $last = $self->lastline;
   
       } elsif ($in_key) {    if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
         $key .= $c;      if ($cfg{'cmd_disconnects'}) {
         $self->logged_in(0);
       } elsif ($in_val) {        $self->close;
         $val .= $c;        $self->is_connected(0);
       }      }
     }  
   }      if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
         return $vals;
   if (%conf) {      } else {
     return \%conf;        return @lines;
   } else {      }
     return undef;    } else {
   }      #$! = "Error with command ($cfg{'string'}): $last";
 }      return undef;
     }
 #=item _decode_each_line  }
   
 sub _decode_each_line  #=item _decode_lines
 {  
   my @lines = @_;  sub _decode_lines
   my @decoded;  {
   foreach my $line (@lines) {    my @lines = @_;
     my $decoded = _decode_lines($line);  
     push @decoded, $decoded if defined $decoded;    my %conf;
   }  
   return \@decoded;    my $key = '';
 }    my $val = undef;
     my $in_key = 0;
 #=item _decode_sulog    my $in_val = 1;
   
 sub _decode_sulog    foreach my $line (@lines) {
 {      next if $line =~ /$success$/;
   my @lines = @_;  
   my @decoded;      my @chars = split //, $line;
   my $last_tm;  
   foreach my $line (@lines) {      my $last_key = '';
     my $decoded = _decode_lines($line);      foreach my $c (@chars) {
   
     if (defined $decoded) {        if ($c eq '[' || $c eq "\r" || $c eq "\n") {
       if ($decoded->{'tm'}) {          if ($c eq '[') {
         $last_tm = $decoded->{'tm'};            $in_key = 1;
         next;            $in_val = 0;
       } else {          } else {
         $decoded->{'tm'} = $last_tm;            $in_key = 0;
       }            $in_val = 1;
       next unless $last_tm;          }
   
       push @decoded, $decoded if defined $decoded;          if ($key) {
     }            $key =~ s/^\s+//;
   }            $key =~ s/\s+$//;
   return \@decoded;  
 }            if (defined $val) {
               $val =~ s/^\s+//;
 1;              $val =~ s/\s+$//;
 __END__            }
   
 =back            if ($key eq 'Checksum' && $last_key) {
               # Special case for these bastids.
 =head1 SEE ALSO              my $new = $last_key;
               $new =~ s/\s+\S+$//;
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm              $key = $new . " " . $key;
             }
 L<Net::Telnet>  
             $last_key = $key;
 =head1 TODO            $conf{$key} = $val;
             $key = '';
 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.            $val = '';
           }
 I also want to be able to parse the different types of output from commands like su, sudb all and anything else that would be better available as a perl datastructure.  
         } elsif ($c eq ']') {
 =head1 AUTHOR          $in_val = 1;
           $in_key = 0;
 Andrew Fresh E<lt>andrew@rraz.netE<gt>          $c = shift @chars;
   
 =head1 COPYRIGHT AND LICENSE        } elsif ($in_key) {
           $key .= $c;
 Copyright (C) 2005 by Andrew Fresh  
         } elsif ($in_val) {
 This library is free software; you can redistribute it and/or modify          $val .= $c;
 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    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.24

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