[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.3 and 1.17

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

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