[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.2 and 1.23

version 1.2, 2005/12/30 01:02:41 version 1.23, 2007/01/08 22:16:29
Line 1 
Line 1 
 package Net::Telnet::Trango;  package Net::Telnet::Trango;
 # $RedRiver$  # $RedRiver: Trango.pm,v 1.21 2006/10/05 17:10:39 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 ({    my $t = new Net::Telnet::Trango ( Timeout => 5 );
     Host    => $fox,  
     Timeout => 5,    $t->open( Host => $fox ) or die "Error connecting: $!";
   });  
       $t->login('password') or die "Couldn't log in: $!";
   my ($type, $version) = $t->open;  
       # Do whatever
   unless (defined $type && defined $version) {  
     die "Error connecting: $!";    $t->exit;
   }    $t->close;
   
   $t->login('password') or die "Couldn't log in: $!";  =head1 DESCRIPTION
     
   # Do whatever  Perl access to the telnet interface on Trango Foxes, SUs and APs.
     
   $t->exit;  Another handy feature is that it will parse the output from certain
   $t->close;  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
 =head1 DESCRIPTION  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 reboot  #  ping <ip addr>
   #  polar <h|v>
 alias of restart()  #  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.
 #  su sw <suid|all> <sw #> <on|off>  Only set after a successful open()
 #  sudb [dload | view]  
 #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>  This is usually only set internally
 #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>  
 #  sudb delete <all|<suid>>  =item last_lines
 #  sudb modify <suid> <cir|mir> <kbps>  
 #  sudb modify <suid> <su2su> <group id,hex>  returns the output from the last cmd() that was run as an array ref
 #  sudb view  This is usually only set internally
 #  sulog [lastmins | sampleperiod <1..60>]  
 #  sulog [<# of entry,1..18>]  =back
 #  survey <ism|unii> <time, sec> <h|v>  
 #  sw [<sw #> <on|off>]  =head2 ALIASES
 #  temp  
 #  tftpd [on|off]  =over
 #  time  
 #  time <hour> <min> <sec>  =item bye
 #  save <mainimage|fpgaimage> <current chksum> <new chksum>  
 #  save <systemsetting|sudb>  alias of exit()
 #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>  
 #  updateflash <systemsetting|sudb>  =item restart
   
   alias of reboot()
 my $success = 'Success.';  
 my %COMMANDS = (  =back
   tftpd       => { decode => 1, expect => $success },  
   ver         => { decode => 1 },  =head2 COMMANDS
   sysinfo     => { decode => 1, expect => $success },  
   updateflash => { decode => 1, expect => $success },  Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
   'exit'      => { Prompt => '//', cmd_disconnects => 1 },  as such they accept the same options as C<cmd()>.
   reboot      => { Prompt => '//', cmd_disconnects => 1 },  Specifically they take a named paramater "args", for example:
 );  C<tftpd(args =E<gt> 'on')> would enable tftpd
   
 my %ALIASES = (  =over
   bye     => 'exit',  
   restart => 'reboot',  =item tftpd
 );  
   Returns a hash ref of the decoded output from the command.
 my %ACCESS = map { $_ => 1 } qw(  
   firmware_version  Also see enable_tftpd() and disable_tftpd() as those check for correct output
   host_type  
   Host  =item ver
   is_connected  
   logged_in  Returns a hash ref of the decoded output from the command.
   login_banner  
   Timeout  =item sysinfo
   last_lines  
 );  Returns a hash ref of the decoded output from the command.
   
 sub AUTOLOAD  =item exit
 {  
   my $self = shift;  exits the command session with the trango and closes the connection
   
   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/  =item reboot
     or die "Weird: $AUTOLOAD";  
   reboots the trango and closes the connection
   if (exists $ALIASES{$method}) {  
     $method = $ALIASES{$method};  =item remarks
     return $self->$method(@_);  
   }  Takes an optional argument, which sets the remarks.
   If there is no argument, returns the current remarks.
   if (exists $COMMANDS{$method}) {  
     $COMMANDS{$method}{'String'} ||= $method;  =item sulog
     return $self->cmd(%{ $COMMANDS{$method} }, @_);  
   }  returns an array ref of hashes containing each log line.
   
   if (exists $ACCESS{$method}) {  =item save_sudb
     my $prev = $PRIVATE{$method};  
     ($PRIVATE{$method}) = @_ if @_;  returns true on success, undef on failure
     return $prev;  
   }  =item syslog
   
   $method = "SUPER::$method";  returns the output from the syslog command
   return $self->$method(@_);  
 }  =item pipe
   
 =pod  returns the output from the pipe command
   
 =item new  =item maclist
   
 =cut  returns the output from the maclist command
   
 sub new  =item maclist_reset
 {  
   my $class = shift;  resets the maclist.  No useful output.
   my $args = shift || {};  
   =item eth_list
   $args->{'Timeout'} ||= 5;  
   $args->{'Prompt'}  ||= '/#> *$/';  returns the output from the eth list command
   
   foreach my $key (keys %{ $args }) {  =item su_info
     $PRIVATE{$key} = $args->{$key};  
   }  You need to pass in args => <suid> and it will return the info for that suid.
   
   my $self = $class->SUPER::new(%{ $args });  =item save_ss
   bless $self if ref $self;  
   saves the config.  Returns 1 on success, undef on failure.
   return $self;  
 }  =cut
   
 =pod  
   my $success = 'Success.';
 =item open  my %COMMANDS = (
     tftpd       => { decode => 'all',   expect => $success },
 =cut    ver         => { decode => 'all' },
     sysinfo     => { decode => 'all',   expect => $success },
 sub open    updateflash => { decode => 'all',   expect => $success },
 {    sulog       => { decode => 'sulog', expect => $success },
   my $self = shift;    'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
     reboot      => { no_prompt => 1, cmd_disconnects => 1 },
   unless ( $self->SUPER::open(    remarks     => { decode => 'all', expect => $success },
       #Host => $self->Host,    save_sudb   => { String => 'save sudb', expect => $success },
       #Errmode => 'return',    syslog      => { expect => $success },
   ) ) {    'pipe'      => { }, # XXX needs a special decode
     #$! = "Couldn't connect to " . $self->Host . ":  $!";    maclist     => { decode => 'maclist' },
     return undef;    maclist_reset => { String => 'maclist reset', expect => 'done' },
   }    eth_link    => { String => 'eth link', expect => $success },
   #$self->dump_log('dump.log');    su_info     => { String => 'su info', decode => 'all', expect => $success },
     save_ss     => { String => 'save ss', expect => $success },
   ## Login to remote host.    opmode      => { decode => 'all',   expect => $success },
   unless ($self->waitfor(    # eth r, w and reset???
     -match => '/password: ?$/i',    #su password???
     -errmode => "return",    #_bootloader
   ) ) {    #temp
   #$! = "problem connecting to host (" . $self->Host . "): " .    #heater
   #    $self->lastline;  );
     return undef;  
   }  my %ALIASES = (
     bye     => 'exit',
   $self->parse_login_banner($self->lastline);    restart => 'reboot',
   );
   $self->is_connected(1);  
   my %ACCESS = map { $_ => 1 } qw(
   return $self->is_connected;    firmware_version
 }    host_type
     Host
 =pod    is_connected
     logged_in
 =item login    login_banner
     Timeout
 =cut    last_lines
     last_vals
 sub login  );
 {  
   my $self = shift;  sub AUTOLOAD
   {
   unless ($self->is_connected) {    my $self = shift;
     $self->open or return undef;  
   }    my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
       or die "Weird: $AUTOLOAD";
   my $password = shift;  
     if (exists $ALIASES{$method}) {
   $self->print($password);      $method = $ALIASES{$method};
   unless ($self->waitfor(      return $self->$method(@_);
     -match => $self->prompt,    }
     -errmode => "return",  
   ) ) {    if (exists $COMMANDS{$method}) {
     #$! = "login ($self->Host) failed: " . $self->lastline;      $COMMANDS{$method}{'String'} ||= $method;
     return undef;      $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
   }      return $self->cmd(%{ $COMMANDS{$method} }, @_);
     }
   $self->logged_in(1);  
     if (exists $ACCESS{$method}) {
   return $self->logged_in;      my $prev = $PRIVATE{$method};
 }      ($PRIVATE{$method}) = @_ if @_;
       return $prev;
 =pod    }
   
 =item parse_login_banner    $method = "SUPER::$method";
     return $self->$method(@_);
 =cut  }
   
 sub parse_login_banner  =pod
 {  
   my $self = shift;  =item open
   
   if (@_) {  Calls Net::Telnet::open() then makes sure you get a password prompt so
     $self->login_banner(@_);  you are ready to login() and parses the login banner so you can get
   }  host_type() and firmware_version()
   
   my $banner = $self->login_banner;  =cut
   
   my ($type, $ver) = $banner =~  sub open
     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;  {
     my $self = shift;
   $self->login_banner($banner);  
   $self->host_type($type);    unless ( $self->SUPER::open(@_) ) {
   $self->firmware_version($ver);      #$! = "Couldn't connect to " . $self->Host . ":  $!";
       return undef;
   return 1;    }
 }  
     ## Get to login prompt
 =pod    unless ($self->waitfor(
         -match => '/password: ?$/i',
 =item enable_tftpd        -errmode => "return",
       ) ) {
 =cut      #$! = "problem connecting to host (" . $self->Host . "): " .
       #    $self->lastline;
 sub enable_tftpd      return undef;
 {    }
   my $self = shift;  
     $self->parse_login_banner($self->lastline);
   my $vals = $self->tftpd( args => 'on' );  
     $self->is_connected(1);
   if ($vals->{'Tftpd'} eq 'listen') {  
     return $vals;    return $self->is_connected;
   } else {  }
     return undef;  
   }  =pod
 }  
   =item login
 =pod  
   Calls open() if not already connected, then sends the password and sets
 =item disable_tftpd  logged_in() if successful
   
 =cut  =cut
   
 sub disable_tftpd  sub login
 {  {
   my $self = shift;    my $self = shift;
   
   my $vals = $self->tftpd( args => 'off' );    unless ($self->is_connected) {
       $self->open or return undef;
   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {    }
     return $vals;  
   } else {    my $password = shift;
     return undef;  
   }    $self->print($password);
 }    unless ($self->waitfor(
       -match => $self->prompt,
 =pod      -errmode => "return",
     ) ) {
 =item cmd      #$! = "login ($self->Host) failed: " . $self->lastline;
       return undef;
 =cut    }
   
 sub cmd    $self->logged_in(1);
 {  
   my $self = shift;    return $self->logged_in;
   }
   my @valid_net_telnet_opts = qw(  
     String  =pod
     Output  
     Cmd_remove_mode  =item parse_login_banner
     Errmode  
     Input_record_separator  Takes a login banner (what you get when you first connect to the Trango)
     Ors  or reads what is already in login_banner() then parses it and sets
     Output_record_separator  host_type() and firmware_version() as well as login_banner()
     Prompt  
     Rs  =cut
     Timeout  
   );  sub parse_login_banner
   {
   my %cfg;    my $self = shift;
   if (@_ == 2) {  
     $cfg{'String'} = shift;    if (@_) {
   } elsif (@_ > 2) {      $self->login_banner(@_);
     %cfg = @_;    }
   }  
     my $banner = $self->login_banner;
   $cfg{'Timeout'} ||= $self->Timeout;  
     my ($type, $ver) = $banner =~
   unless ($cfg{'String'}) {      /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
     #$! = "No command passed";  
     warn "No command passed\n";    $self->login_banner($banner);
     return undef;    $self->host_type($type);
   }    $self->firmware_version($ver);
   
   unless ($self->is_connected) {    return 1;
     #$! = "Not connected";  }
     warn "Not connected\n";  
     return undef;  =pod
   }  
   =item su_password
   unless ($self->logged_in) {  
     #$! = "Not logged in";  C<su_password('new_password'[, 'suid'])> If no suid is specified,
     warn "Not logged in\n";  the default is "all".
     return undef;  
   }  =cut
   
   sub su_password
   my %cmd;  {
   foreach (@valid_net_telnet_opts) {    my $self     = shift;
     if (exists $cfg{$_}) {    my $new_pass = shift || '';
       $cmd{$_} = $cfg{$_};    my $su       = shift || 'all';
     }  
   }    unless (defined $new_pass) {
   if ($cfg{'args'}) {      warn "No new password!"
     $cmd{'String'} .= ' ' . $cfg{'args'};      #return undef;
   }    }
   my @lines = $self->SUPER::cmd(%cmd);  
     return $self->cmd(String => 'su password ' .
   $self->last_lines(\@lines);                       $su . ' ' .
                        $new_pass . ' ' .
   my $vals = 1;                       $new_pass,
   if ($cfg{'decode'}) {                       expect => $success,
     $vals = _decode_lines(@lines);                      );
   }  }
   
   =pod
   my $last = $self->lastline;  
   =item su_ipconfig
   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {  
     if ($cfg{'cmd_disconnects'}) {  C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
       $self->logged_in(0);  
       $self->close;  =cut
       $self->is_connected(0);  
     }  sub su_ipconfig
   {
     if ($cfg{'decode'}) {          my $self        = shift;
       return $vals;  
     } else {          my $suid        = shift;
       return @lines;          my $new_ip      = shift;
     }          my $new_subnet  = shift;
   } else {          my $new_gateway = shift;
     #$! = "Error with command ($cfg{'string'}): $last";  
     return undef;          return undef unless $suid =~ /^\d+$/;
   }          return undef unless $new_ip;
 }          return undef unless $new_subnet;
           return undef unless $new_gateway;
 #=item _decode_lines  
           # su ipconfig <suid> <new ip> <new subnet> <new gateway>
 sub _decode_lines          return $self->cmd(String => 'su ipconfig ' .
 {                       $suid       . ' ' .
   my @lines = @_;                       $new_ip     . ' ' .
                        $new_subnet . ' ' .
   my %conf;                       $new_gateway,
                        expect => $success,
   my $key = '';                      );
   my $val = '';  }
   my $in_key = 0;  
   my $in_val = 0;  =pod
   
   foreach my $line (@lines) {  =item sudb_view
     my @chars = split //, $line;  
   returns a reference to an array of hashes each containing:
     my $last_key = '';  
     foreach my $c (@chars) {    suid
     type
       if ($c eq '[' || $c eq "\r" || $c eq "\n") {    cir
         if ($c eq '[') {    mir
           $in_key = 1;    mac
           $in_val = 0;  
         } else {  =cut
           $in_key = 0;  
           $in_val = 0;  sub sudb_view
         }  {
     my $self = shift;
         if ($key) {  
           $key =~ s/^\s+//;    my @lines = $self->cmd( String => 'sudb view', expect => $success );
           $key =~ s/\s+$//;  
     return undef unless @lines;
           $val =~ s/^\s+//;  
           $val =~ s/\s+$//;    unless ($PRIVATE{'Decode'}) {
       return @lines;
           if ($key eq 'Checksum' && $last_key) {    }
             # Special case for these bastids.  
             my $new = $last_key;    my @sus;
             $new =~ s/\s+\S+$//;    foreach (@lines) {
             $key = $new . " " . $key;      next unless $_;
           }      if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
         my %s = (
           $last_key = $key;          suid => $1,
           $conf{$key} = $val;          type => $2,
           $key = '';          cir  => $3,
           $val = '';          mir  => $4,
         }          mac  => $5,
         );
       } elsif ($c eq ']') {  
         $in_val = 1;            $s{'mac'} =~ s/\s//g;
         $in_key = 0;            $s{'mac'} = uc($s{'mac'});
         $c = shift @chars;  
         push @sus, \%s;
       } elsif ($in_key) {      }
         $key .= $c;    }
   
       } elsif ($in_val) {    return \@sus;
         $val .= $c;  }
       }  
     }  =pod
   }  
   =item sudb_add
   if (%conf) {  
     return \%conf;  Takes the following paramaters
   } else {  
     return \@lines;          suid : numeric,
   }          type : (reg|pr)
 }          cir  : numeric,
           mir  : numeric,
 1;          mac  : Almost any format, it will be reformatted,
 __END__  
   and returns true on success or undef otherwise.
 =back  
   You should save_sudb() after calling this, or your changes  will be lost
 =head1 SEE ALSO  when the AP is rebooted.
   
 If you have a web site set up for your module, mention it here.  =cut
   
 Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm  sub sudb_add
   {
 L<Net::Telnet>          my $self = shift;
           my $suid = shift;
 =head1 TODO          my $type = shift;
           my $cir  = shift;
 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.          my $mir  = shift;
           my $mac  = shift;
 I also want to be able to parse the different types of output from commands like su, sudb all and anything else that would be better available as a perl datastructure.  
           if ($suid =~ /\D/) {
 =head1 AUTHOR                  return undef;
           }
 Andrew Fresh E<lt>andrew@rraz.netE<gt>  
           unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
 =head1 COPYRIGHT AND LICENSE                  warn "Invalid type '$type'!";
                   return undef;
 Copyright (C) 2005 by Andrew Fresh          }
   
 This library is free software; you can redistribute it and/or modify          if ($cir =~ /\D/) {
 it under the same terms as Perl itself, either Perl version 5.8.7 or,                  warn "Invalid CIR '$cir'!";
 at your option, any later version of Perl 5 you may have available.                  return undef;
           }
   
 =cut          if ($mir =~ /\D/) {
                   warn "Invalid MIR '$mir'!";
                   return undef;
           }
   
           my $new_mac = $mac;
           $new_mac =~ s/[^0-9A-Fa-f]//;
           unless (length $new_mac == 12) {
                   warn "Invalid MAC '$mac'!";
                   return undef;
           }
           $new_mac = join ' ', $new_mac =~ /../g;
   
           my $string = 'sudb add ' .
                   $suid . ' ' .
                   $type . ' ' .
                   $cir  . ' ' .
                   $mir  . ' ' .
                   $new_mac;
   
   
           return $self->cmd( String => $string, expect => $success );
   }
   
   =pod
   
   =item sudb_delete
   
   Takes either 'all' or the  suid of the su to delete
   and returns true on success or undef otherwise.
   
   You should save_sudb() after calling this, or your changes  will be lost
   when the AP is rebooted.
   
   =cut
   
   sub sudb_delete
   {
           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.2  
changed lines
  Added in v.1.23

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