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

version 1.3, 2005/12/30 19:26:06 version 1.19, 2006/09/12 03:16:43
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.18 2006/09/12 02:13:08 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)>,
   updateflash => { decode => 1, expect => $success },  as such they accept the same options as C<cmd()>.
   'exit'      => { Prompt => '//', cmd_disconnects => 1 },  Specifically they take a named paramater "args", for example:
   reboot      => { Prompt => '//', cmd_disconnects => 1 },  C<tftpd(args =E<gt> 'on')> would enable tftpd
   #su password???  
   #_bootloader  =over
   #temp  
   #heater  =item tftpd
 );  
   Returns a hash ref of the decoded output from the command.
 my %ALIASES = (  
   bye     => 'exit',  Also see enable_tftpd() and disable_tftpd() as those check for correct output
   restart => 'reboot',  
 );  =item ver
   
 my %ACCESS = map { $_ => 1 } qw(  Returns a hash ref of the decoded output from the command.
   firmware_version  
   host_type  =item sysinfo
   Host  
   is_connected  Returns a hash ref of the decoded output from the command.
   logged_in  
   login_banner  =item exit
   Timeout  
   last_lines  exits the command session with the trango and closes the connection
 );  
   =item reboot
 sub AUTOLOAD  
 {  reboots the trango and closes the connection
   my $self = shift;  
   =item remarks
   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/  
     or die "Weird: $AUTOLOAD";  Takes an optional argument, which sets the remarks.
   If there is no argument, returns the current remarks.
   if (exists $ALIASES{$method}) {  
     $method = $ALIASES{$method};  =item sulog
     return $self->$method(@_);  
   }  returns an array ref of hashes containing each log line.
   
   if (exists $COMMANDS{$method}) {  =item save_sudb
     $method = shift if (@_ == 1);  
     $COMMANDS{$method}{'String'} ||= $method;  returns true on success, undef on failure
     return $self->cmd(%{ $COMMANDS{$method} }, @_);  
   }  =item syslog
   
   if (exists $ACCESS{$method}) {  returns the output from the syslog command
     my $prev = $PRIVATE{$method};  
     ($PRIVATE{$method}) = @_ if @_;  =item pipe
     return $prev;  
   }  returns the output from the pipe command
   
   $method = "SUPER::$method";  =item maclist
   return $self->$method(@_);  
 }  returns the output from the maclist command
   
 =pod  =item maclist_reset
   
 =item new  resets the maclist.  No useful output.
   
 Same as new from L<Net::Telnet> but has defaults for the trango 'Prompt'  =item eth_list
   
 =cut  returns the output from the eth list command
   
 sub new  =item su_info
 {  
   my $class = shift;  You need to pass in args => <suid> and it will return the info for that suid.
   
   my %args;  =item save_ss
   if (@_ == 1) {  
     $args{'Host'} = shift;  saves the config.  Returns 1 on success, undef on failure.
   } else {  
     %args = @_;  =cut
   }  
   
   $args{'Prompt'}  ||= '/#> *$/';  my $success = 'Success.';
   my %COMMANDS = (
   foreach my $key (keys %args) {    tftpd       => { decode => 'all',   expect => $success },
     $PRIVATE{$key} = $args{$key};    ver         => { decode => 'all' },
   }    sysinfo     => { decode => 'all',   expect => $success },
     updateflash => { decode => 'all',   expect => $success },
   my $self = $class->SUPER::new(%args);    sulog       => { decode => 'sulog', expect => $success },
   bless $self if ref $self;    'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
     reboot      => { no_prompt => 1, cmd_disconnects => 1 },
   return $self;    remarks     => { decode => 'all', expect => $success },
 }    save_sudb   => { String => 'save sudb', expect => $success },
     syslog      => { expect => $success },
 =pod    'pipe'      => { }, # XXX needs a special decode
     maclist     => { decode => 'maclist' },
 =item open    maclist_reset => { String => 'maclist reset', expect => 'done' },
     eth_link    => { String => 'eth link', 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()    su_info     => { String => 'su info', decode => 'all', expect => $success },
     save_ss     => { String => 'save ss', expect => $success },
 =cut    # eth r, w and reset???
     #su password???
 sub open    #_bootloader
 {    #temp
   my $self = shift;    #heater
   );
   unless ( $self->SUPER::open(@_) ) {  
     #$! = "Couldn't connect to " . $self->Host . ":  $!";  my %ALIASES = (
     return undef;    bye     => 'exit',
   }    restart => 'reboot',
   );
   ## Get to login prompt  
   unless ($self->waitfor(  my %ACCESS = map { $_ => 1 } qw(
       -match => '/password: ?$/i',    firmware_version
       -errmode => "return",    host_type
     ) ) {    Host
     #$! = "problem connecting to host (" . $self->Host . "): " .    is_connected
     #    $self->lastline;    logged_in
     return undef;    login_banner
   }    Timeout
     last_lines
   $self->parse_login_banner($self->lastline);    last_vals
   );
   $self->is_connected(1);  
   sub AUTOLOAD
   return $self->is_connected;  {
 }    my $self = shift;
   
 =pod    my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
       or die "Weird: $AUTOLOAD";
 =item login  
     if (exists $ALIASES{$method}) {
 Calls open() if not already connected, then sends the password and sets logged_in() if successful      $method = $ALIASES{$method};
       return $self->$method(@_);
 =cut    }
   
 sub login    if (exists $COMMANDS{$method}) {
 {      $COMMANDS{$method}{'String'} ||= $method;
   my $self = shift;      $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
       return $self->cmd(%{ $COMMANDS{$method} }, @_);
   unless ($self->is_connected) {    }
     $self->open or return undef;  
   }    if (exists $ACCESS{$method}) {
       my $prev = $PRIVATE{$method};
   my $password = shift;      ($PRIVATE{$method}) = @_ if @_;
       return $prev;
   $self->print($password);    }
   unless ($self->waitfor(  
     -match => $self->prompt,    $method = "SUPER::$method";
     -errmode => "return",    return $self->$method(@_);
   ) ) {  }
     #$! = "login ($self->Host) failed: " . $self->lastline;  
     return undef;  =pod
   }  
   =item open
   $self->logged_in(1);  
   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()
   return $self->logged_in;  
 }  =cut
   
 =pod  sub open
   {
 =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()    unless ( $self->SUPER::open(@_) ) {
       #$! = "Couldn't connect to " . $self->Host . ":  $!";
 =cut      return undef;
     }
 sub parse_login_banner  
 {    ## Get to login prompt
   my $self = shift;    unless ($self->waitfor(
         -match => '/password: ?$/i',
   if (@_) {        -errmode => "return",
     $self->login_banner(@_);      ) ) {
   }      #$! = "problem connecting to host (" . $self->Host . "): " .
       #    $self->lastline;
   my $banner = $self->login_banner;      return undef;
     }
   my ($type, $ver) = $banner =~  
     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;    $self->parse_login_banner($self->lastline);
   
   $self->login_banner($banner);    $self->is_connected(1);
   $self->host_type($type);  
   $self->firmware_version($ver);    return $self->is_connected;
   }
   return 1;  
 }  =pod
   
 =pod  =item login
   
 =item enable_tftpd  Calls open() if not already connected, then sends the password and sets logged_in() if successful
   
 runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing  =cut
   
 =cut  sub login
   {
 sub enable_tftpd    my $self = shift;
 {  
   my $self = shift;    unless ($self->is_connected) {
       $self->open or return undef;
   my $vals = $self->tftpd( args => 'on' );    }
   
   if ($vals->{'Tftpd'} eq 'listen') {    my $password = shift;
     return $vals;  
   } else {    $self->print($password);
     return undef;    unless ($self->waitfor(
   }      -match => $self->prompt,
 }      -errmode => "return",
     ) ) {
 =pod      #$! = "login ($self->Host) failed: " . $self->lastline;
       return undef;
 =item disable_tftpd    }
   
 runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'    $self->logged_in(1);
   
 =cut    return $self->logged_in;
   }
 sub disable_tftpd  
 {  =pod
   my $self = shift;  
   =item parse_login_banner
   my $vals = $self->tftpd( args => 'off' );  
   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()
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {  
     return $vals;  =cut
   } else {  
     return undef;  sub parse_login_banner
   }  {
 }    my $self = shift;
   
 =pod    if (@_) {
       $self->login_banner(@_);
 =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.    my $banner = $self->login_banner;
   
 Normally returns the last lines from from the command    my ($type, $ver) = $banner =~
       /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
 Also accepts these options:  
     $self->login_banner($banner);
 I<decode>    $self->host_type($type);
 - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output    $self->firmware_version($ver);
   
 I<cmd_disconnects>    return 1;
 - 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>  =pod
 - 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  
   =item su_password
 I<args>  
 - a string containing the command line options that are passed to the command  C<su_password('all'|suid, 'new_password')>
   
 =cut  =cut
   
 sub cmd  sub su_password
 {  {
   my $self = shift;    my $self     = shift;
     my $su       = shift || '!';
   my @valid_net_telnet_opts = qw(    my $new_pass = shift || '';
     String  
     Output    unless (defined $su) {
     Cmd_remove_mode      warn "No su passed!"
     Errmode      #return undef;
     Input_record_separator    }
     Ors  
     Output_record_separator    unless (defined $new_pass) {
     Prompt      warn "No new password!"
     Rs      #return undef;
     Timeout    }
   );  
     return $self->cmd(String => 'su password ' .
   my %cfg;                       $su . ' ' .
   if (@_ == 2) {                       $new_pass . ' ' .
     $cfg{'String'} = shift;                       $new_pass,
   } elsif (@_ > 2) {                       expect => $success,
     %cfg = @_;                      );
   }  }
   
   $cfg{'Timeout'} ||= $self->Timeout;  =pod
   
   unless ($cfg{'String'}) {  =item su_ipconfig
     #$! = "No command passed";  
     #warn "No command passed\n";  C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
     return undef;  
   }  =cut
   
   unless ($self->is_connected) {  sub su_ipconfig
     #$! = "Not connected";  {
     #warn "Not connected\n";          my $self        = shift;
     return undef;  
   }          my $suid        = shift;
           my $new_ip      = shift;
   unless ($self->logged_in) {          my $new_subnet  = shift;
     #$! = "Not logged in";          my $new_gateway = shift;
     #warn "Not logged in\n";  
     return undef;          return undef unless $suid =~ /^\d+$/;
   }          return undef unless $new_ip;
           return undef unless $new_subnet;
           return undef unless $new_gateway;
   my %cmd;  
   foreach (@valid_net_telnet_opts) {          # su ipconfig <suid> <new ip> <new subnet> <new gateway>
     if (exists $cfg{$_}) {          return $self->cmd(String => 'su ipconfig ' .
       $cmd{$_} = $cfg{$_};                       $suid       . ' ' .
     }                       $new_ip     . ' ' .
   }                       $new_subnet . ' ' .
   if ($cfg{'args'}) {                       $new_gateway,
     $cmd{'String'} .= ' ' . $cfg{'args'};                       expect => $success,
   }                      );
   my @lines = $self->SUPER::cmd(%cmd);  }
   
   $self->last_lines(\@lines);  =pod
   
   my $vals = 1;  =item sudb_view
   if ($cfg{'decode'}) {  
     $vals = _decode_lines(@lines);  returns a reference to an array of hashes each containing:
   }  
     suid
     type
   my $last = $self->lastline;    cir
     mir
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {    mac
     if ($cfg{'cmd_disconnects'}) {  
       $self->logged_in(0);  =cut
       $self->close;  
       $self->is_connected(0);  sub sudb_view
     }  {
     my $self = shift;
     if ($cfg{'decode'}) {  
       return $vals;    my @lines = $self->cmd( String => 'sudb view', expect => $success );
     } else {  
       return @lines;    return undef unless @lines;
     }  
   } else {    unless ($PRIVATE{'Decode'}) {
     #$! = "Error with command ($cfg{'string'}): $last";      return @lines;
     return undef;    }
   }  
 }    my @sus;
     foreach (@lines) {
 #=item _decode_lines      next unless $_;
       if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
 sub _decode_lines        my %s = (
 {          suid => $1,
   my @lines = @_;          type => $2,
           cir  => $3,
   my %conf;          mir  => $4,
           mac  => $5,
   my $key = '';        );
   my $val = '';  
   my $in_key = 0;            $s{'mac'} =~ s/\s//g;
   my $in_val = 0;            $s{'mac'} = uc($s{'mac'});
   
   foreach my $line (@lines) {        push @sus, \%s;
     my @chars = split //, $line;      }
     }
     my $last_key = '';  
     foreach my $c (@chars) {    return \@sus;
   }
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {  
         if ($c eq '[') {  =pod
           $in_key = 1;  
           $in_val = 0;  =item sudb_add
         } else {  
           $in_key = 0;  Takes the following paramaters
           $in_val = 0;  
         }          suid : numeric,
           type : (reg|pr)
         if ($key) {          cir  : numeric,
           $key =~ s/^\s+//;          mir  : numeric,
           $key =~ s/\s+$//;          mac  : Almost any format, it will be reformatted,
   
           $val =~ 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
           if ($key eq 'Checksum' && $last_key) {  when the AP is rebooted.
             # Special case for these bastids.  
             my $new = $last_key;  =cut
             $new =~ s/\s+\S+$//;  
             $key = $new . " " . $key;  sub sudb_add
           }  {
           my $self = shift;
           $last_key = $key;          my $suid = shift;
           $conf{$key} = $val;          my $type = shift;
           $key = '';          my $cir  = shift;
           $val = '';          my $mir  = shift;
         }          my $mac  = shift;
   
       } elsif ($c eq ']') {          if ($suid =~ /\D/) {
         $in_val = 1;                  return undef;
         $in_key = 0;          }
         $c = shift @chars;  
           unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
       } elsif ($in_key) {                  warn "Invalid type '$type'!";
         $key .= $c;                  return undef;
           }
       } elsif ($in_val) {  
         $val .= $c;          if ($cir =~ /\D/) {
       }                  warn "Invalid CIR '$cir'!";
     }                  return undef;
   }          }
   
   if (%conf) {          if ($mir =~ /\D/) {
     return \%conf;                  warn "Invalid MIR '$mir'!";
   } else {                  return undef;
     return \@lines;          }
   }  
 }          my $new_mac = $mac;
           $new_mac =~ s/[^0-9A-Fa-f]//;
 1;          unless (length $new_mac == 12) {
 __END__                  warn "Invalid MAC '$mac'!";
                   return undef;
 =back          }
           $new_mac = join ' ', $new_mac =~ /../g;
 =head1 SEE ALSO  
           my $string = 'sudb add ' .
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm                  $suid . ' ' .
                   $type . ' ' .
 L<Net::Telnet>                  $cir  . ' ' .
                   $mir  . ' ' .
 =head1 TODO                  $new_mac;
   
 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.  
           return $self->cmd( String => $string, expect => $success );
 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  =pod
   
 Andrew Fresh E<lt>andrew@rraz.netE<gt>  =item sudb_delete
   
 =head1 COPYRIGHT AND LICENSE  Takes either 'all' or the  suid of the su to delete
   and returns true on success or undef otherwise.
 Copyright (C) 2005 by Andrew Fresh  
   You should save_sudb() after calling this, or your changes  will be lost
 This library is free software; you can redistribute it and/or modify  when the AP is rebooted.
 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
   
   sub sudb_delete
 =cut  {
           my $self = shift;
           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;
           } 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.19

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