[BACK]Return to Trango.pm CVS log [TXT][DIR] Up to [local] / trango / Net-Telnet-Trango / lib / Net / Telnet

Annotation of trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm, Revision 1.43

1.40      andrew      1: package Net::Telnet::Trango;
                      2:
1.43    ! andrew      3: # $RedRiver: Trango.pm,v 1.42 2007/05/21 20:24:13 andrew Exp $
1.40      andrew      4: use strict;
                      5: use warnings;
                      6: use base 'Net::Telnet';
                      7:
                      8: =pod
                      9:
                     10: =head1 NAME
                     11:
                     12: Net::Telnet::Trango
                     13: - Perl extension for accessing the Trango telnet interface
                     14:
                     15: =head1 SYNOPSIS
                     16:
                     17:   use Net::Telnet::Trango;
                     18:   my $t = new Net::Telnet::Trango ( Timeout => 5 );
                     19:
1.41      andrew     20:   $t->open( Host => $ap ) or die "Error connecting: $!";
1.40      andrew     21:
                     22:   $t->login('password') or die "Couldn't log in: $!";
                     23:
                     24:   # Do whatever
                     25:
                     26:   $t->exit;
                     27:   $t->close;
                     28:
                     29: =head1 DESCRIPTION
                     30:
1.41      andrew     31: Perl access to the telnet interface on Trango APs and SUs.
1.40      andrew     32:
1.41      andrew     33: A handy feature is that it will parse the output from certain commands that is
                     34: in the format "[key1] value1 [key2] value2" and put those in a hashref that is
                     35: returned.  This makes using the output from things like sysinfo very easy to
                     36: do.
1.40      andrew     37:
                     38: =head2 EXPORT
                     39:
                     40: None
                     41:
                     42: =head1 METHODS
                     43:
                     44: =cut
                     45:
                     46: our $VERSION = '0.01';
                     47:
                     48: my $EMPTY = q{};
                     49: my $SPACE = q{ };
                     50:
                     51: my %PRIVATE = (
                     52:     is_connected => 0,
                     53:     logged_in    => 0,
                     54: );
                     55:
                     56: =pod
                     57:
                     58: =head2 B<new> - Creates a new Net::Telnet::Trango object.
                     59:
                     60:     new([Options from Net::Telnet,]
                     61:         [Decode => 0,]);
                     62:
                     63: Same as new from L<Net::Telnet> but sets the default Trango Prompt:
                     64: '/#> *$/'
                     65:
                     66: It also takes an optional parameter 'Decode'.  If not defined it
                     67: defaults to 1, if it is set to 0, it will not decode the output and
                     68: instead return a reference to an array of the lines that were returned
                     69: from the command.
                     70:
                     71: =cut
                     72:
                     73: sub new {
                     74:     my $class = shift;
                     75:
                     76:     my %args;
                     77:     if ( @_ == 1 ) {
                     78:         $args{'Host'} = shift;
                     79:     }
                     80:     else {
                     81:         %args = @_;
                     82:     }
                     83:
                     84:     $args{'Prompt'} ||= '/#> *$/';
                     85:
                     86:     foreach my $key ( keys %args ) {
                     87:         $PRIVATE{$key} = $args{$key};
                     88:     }
                     89:     $PRIVATE{'Decode'} = 1 unless defined $PRIVATE{'Decode'};
                     90:     delete $args{'Decode'};
                     91:
                     92:     my $self = $class->SUPER::new(%args);
                     93:     bless $self if ref $self;
                     94:
                     95:     return $self;
                     96: }
                     97:
                     98: #  _password <new password> <new password>
                     99: #  ? [command]
                    100: #  apsearch <secs> <ch#> <h|v> [<ch#> <h|v>]...
                    101: #  arp -bcast <on|off>
                    102: #  bcastscant <all|suid> <ch#> <h|v> [<ch#> <h|v> ...
                    103: #  bye
                    104: #  cf2cf ap [default|<size>]
                    105: #  date
                    106: #  date <month> <day> <year>
                    107: #  freq scantable
                    108: #  freq channeltable
                    109: #  freq writescan [<ch#> <h|v>]
                    110: #  freq writechannel [<ch#> <freq>] ...
                    111: #  freq <ch #> <h|v>
                    112: #  help [command]
                    113: #  heater [<on temp> <off temp>]
                    114: #  ipconfig [<new ip> <new subnet mask> <new gateway>]
                    115: #  log [<# of entries, 1..179>]
                    116: #  log <sum> <# of entries, 1..179>
                    117: #  logout
                    118: #  opmode [ap [y]]
                    119: #  password
                    120: #  ping <ip addr>
                    121: #  polar <h|v>
                    122: #  power <setism|setunii> <max|min|<dBm>>
                    123: #  reboot
                    124: #  restart
                    125: #  remarks [<str>]
                    126: #  rfrxthreshold [<ism|unii> <-90|-85|-80|-75|-70|-65>]
                    127: #  rfrxth [<ism|unii> <-90|-85|-80|-75|-70|-65>]
                    128: #  sysinfo
                    129: #  set suid <id>
                    130: #  set apid <id>
                    131: #  set baseid <id>
                    132: #  set defaultopmode [<ap|su> <min,0..10>]
                    133: #  set defaultopmode off
                    134: #  set snmpcomm [<read | write | trap (id or setall)> <str>]
                    135: #  set mir [on|off]
                    136: #  set mir threshold <kbps>
                    137: #  set rssitarget [<ism|unii> <dBm>]
                    138: #  set serviceradius [<ism | unii> <miles>]
                    139: #  ssrssi <ch #> <h|v>
                    140: #  su [<suid>|all]
                    141: #  su changechannel <all|suid> <ch#> <h|v>
                    142: #  su ipconfig <suid> <new ip> <new subnet> <new gateway>
                    143: #  su [live|poweroff|priority]
                    144: #  su <ping|info|status> <suid>
                    145: #  su powerleveling <all|suid>
                    146: #  su reboot <all|suid>
                    147: #  su restart <all|suid>
                    148: #  su testrflink <all|suid> [r]
                    149: #  su testrflink <setlen> [64..1600]
                    150: #  su testrflink <aptx> [20..100]
                    151: #  su sw <suid|all> <sw #> <on|off>
                    152: #  sudb [dload | view]
                    153: #  sudb add <suid> pr <cir,kbps> <mir,kbps> <device id,hex>
                    154: #  sudb add <suid> reg <cir,kbps> <mir,kbps> <device id,hex>
                    155: #  sudb delete <all|<suid>>
                    156: #  sudb modify <suid> <cir|mir> <kbps>
                    157: #  sudb modify <suid> <su2su> <group id,hex>
                    158: #  sudb view
                    159: #  sulog [lastmins | sampleperiod <1..60>]
                    160: #  sulog [<# of entry,1..18>]
                    161: #  survey <ism|unii> <time, sec> <h|v>
                    162: #  sw [<sw #> <on|off>]
                    163: #  temp
                    164: #  tftpd [on|off]
                    165: #  time
                    166: #  time <hour> <min> <sec>
                    167: #  save <mainimage|fpgaimage> <current chksum> <new chksum>
                    168: #  save <systemsetting|sudb>
                    169: #  updateflash <mainimage|fpgaimage> <current chksum> <new chksum>
                    170: #  updateflash <systemsetting|sudb>
                    171:
                    172: =pod
                    173:
                    174: =head1 ACCESSORS
                    175:
                    176: These are usually only set internally.
                    177:
                    178: =head2 B<firmware_version> - returns the firmware version
                    179:
                    180: Returns the firmware version if available, otherwise undef.
                    181:
                    182: It should be available after a successful open().
                    183:
                    184: =head2 B<host_type> - return the type of host you are connected to.
                    185:
                    186: returns the type of host from the login banner for example M5830S or M5300S.
                    187:
                    188: Should be available after a successful open().
                    189:
                    190: =head2 B<is_connected> - Status of the connection to host.
                    191:
                    192: returns 1 when connected, undef otherwise.
                    193:
                    194: =head2 B<logged_in> - Status of being logged in to the host.
                    195:
                    196: returns 1 after a successful login(), 0 if it failed and undef if
                    197: login() was never called.
                    198:
                    199: =head2 B<login_banner> - The banner when first connecting to the host.
                    200:
                    201: returns the banner that is displayed when first connected at login.
                    202: Only set after a successful open().
                    203:
                    204: =head2 B<last_lines> - The last lines of output from the last cmd().
                    205:
                    206: returns, as an array ref, the output from the last cmd() that was run.
                    207:
                    208: =head2 B<last_error> - A text output of the last error that was encountered.
                    209:
                    210: returns the last error reported.  Probably contains the last entry in
                    211: last_lines.
                    212:
                    213: =head1 ALIASES
                    214:
                    215: =head2 B<bye> - alias of exit()
                    216:
                    217: Does the same as exit()
                    218:
                    219: =head2 B<restart> - alias of reboot()
                    220:
                    221: Does the same as reboot()
                    222:
                    223: =head2 B<save_systemsetting> - alias of save_ss()
                    224:
                    225: Does the same as save_ss()
                    226:
                    227: =head1 COMMANDS
                    228:
                    229: Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
                    230: as such they accept the same options as C<cmd()>.
                    231: Specifically they take a named paramater "args", for example:
                    232: C<tftpd(args =E<gt> 'on')> would enable tftpd
                    233:
                    234: =head2 B<tftpd> - The output from the tftpd command
                    235:
                    236: Returns a hash ref of the decoded output from the
                    237: command.
                    238:
                    239: Also see enable_tftpd() and disable_tftpd() as those check that it was
                    240: successfully changed.
                    241:
                    242: =head2 B<ver> - The output from the ver command
                    243:
                    244: Returns a hash ref of the decoded output from the
                    245: command.
                    246:
                    247: =head2 B<sysinfo> - The output from the sysinfo command
                    248:
                    249: Returns a hash ref of the decoded output from the
                    250: command.
                    251:
                    252: =head2 B<exit> - Exits the connection
                    253:
                    254: exits the command session with the Trango and closes
                    255: the connection
                    256:
                    257: =head2 B<reboot> - Sends a reboot command
                    258:
                    259: reboots the Trango and closes the connection
                    260:
                    261: =head2 B<remarks> - Set or retrieve the remarks.
                    262:
                    263: Takes an optional argument, which sets the remarks.
                    264: If there is no argument, returns the current remarks.
                    265:
                    266:   my $old_remarks = $t->remarks();
                    267:   $t->remarks($new_remarks);
                    268:
                    269: =head2 B<sulog> - The output from the sulog command
                    270:
                    271: Returns an array ref of hashes containing each log
                    272: line.
                    273:
                    274: =head2 B<save_sudb> - saves the sudb
                    275:
                    276: Returns true on success, undef on failure
                    277:
                    278: =head2 B<syslog> - The output from the sulog command
                    279:
                    280: Returns a hashref of the output from the syslog command
                    281:
                    282: =head2 B<pipe> - the pipe command
                    283:
                    284: Returns the output from the pipe command
                    285:
                    286: =head2 B<maclist> - retrieves the maclist
                    287:
                    288: Returns the output from the maclist command
                    289:
                    290: =head2 B<maclist_reset> - resets the maclist.
                    291:
                    292: No useful output.
                    293:
                    294: =head2 B<eth_link> - eth link command
                    295:
                    296: Returns the output from the eth link command
                    297:
                    298: This command seems to cause some weird issues.  It often will cause the
                    299: command after it to appear to fail.  I am not sure why.
                    300:
                    301: =head2 B<su_info> - gets the su info
                    302:
                    303: Returns information about the SU.
                    304:
                    305: You need to pass in the $suid and it will return the info for that suid.
                    306:
                    307:   $t->su_info($suid);
                    308:
                    309: =head2 B<su_testrflink> - tests the RF Link to an su
                    310:
                    311:   $t->su_testrflink($suid|'all');
                    312:
                    313: =head2 B<save_ss> - saves the config.
                    314:
                    315: Returns 1 on success, undef on failure.
                    316:
                    317: =head2 B<opmode> - sets opmode ap y or returns the opmode
                    318:
                    319:     $t->opmode([ap y]);
                    320:
                    321: =cut
                    322:
                    323: my $success  = 'Success\\.';
                    324: my %COMMANDS = (
1.43    ! andrew    325:     _clear      => { decode    => 'all', String => '', args => '' },
1.40      andrew    326:     tftpd       => { decode    => 'all',       expect          => $success },
                    327:     ver         => { decode    => 'all' },
                    328:     sysinfo     => { decode    => 'all',       expect          => $success },
                    329:     updateflash => { decode    => 'all',       expect          => $success },
                    330:     sulog       => { decode    => 'sulog',     expect          => $success },
                    331:     'exit'      => { no_prompt => 1,           cmd_disconnects => 1 },
                    332:     reboot      => { no_prompt => 1,           cmd_disconnects => 1 },
                    333:     remarks     => { decode    => 'all',       expect          => $success },
                    334:     save_sudb   => { String    => 'save sudb', expect          => $success },
                    335:     syslog      => { expect    => $success },
                    336:     'pipe'      => {},                        # XXX needs a special decode
                    337:     maclist     => { decode => 'maclist' },
                    338:     maclist_reset => { String => 'maclist reset', expect       => 'done' },
                    339:     eth_link    => { String => 'eth link',     expect          => $success },
                    340:     su_info     =>
                    341:       { String => 'su info',  decode => 'all', expect          => $success },
                    342:     su_testrflink =>
                    343:       { String => 'su testrflink', decode => 'each', expect    => $success },
                    344:     save_ss     => { String => 'save ss',      expect          => $success },
                    345:     opmode      => { decode => 'all',          expect          => $success },
                    346: );
                    347:
                    348: my %ALIASES = (
                    349:     bye     => 'exit',
                    350:     restart => 'reboot',
                    351:     Host    => 'host',
                    352:     save_systemseting => 'save_ss',
                    353: );
                    354:
                    355: my %ACCESS = map { $_ => 1 } qw(
                    356:   firmware_version
                    357:   host_type
                    358:   is_connected
                    359:   logged_in
                    360:   login_banner
                    361:   Timeout
                    362:   last_lines
                    363:   last_vals
                    364:   last_error
                    365:   Decode
                    366: );
                    367:
                    368: sub AUTOLOAD {
                    369:     my $self = shift;
                    370:
                    371:     my ($method) = ( our $AUTOLOAD ) =~ /^.*::(\w+)$/
                    372:       or die "Weird: $AUTOLOAD";
                    373:
                    374:     if ( exists $ALIASES{$method} ) {
                    375:         $method = $ALIASES{$method};
                    376:         return $self->$method(@_);
                    377:     }
                    378:
                    379:     if ( exists $COMMANDS{$method} ) {
                    380:         my %cmd;
                    381:         foreach my $k ( keys %{ $COMMANDS{$method} } ) {
                    382:             $cmd{$k} = $COMMANDS{$method}{$k};
                    383:         }
                    384:         $cmd{'String'} ||= $method;
                    385:         $cmd{'args'} .= $SPACE . shift if ( @_ == 1 );
                    386:         return $self->cmd( %cmd, @_ );
                    387:     }
                    388:
                    389:     if ( exists $ACCESS{$method} ) {
                    390:         my $prev = $PRIVATE{$method};
                    391:         ( $PRIVATE{$method} ) = @_ if @_;
                    392:         return $prev;
                    393:     }
                    394:
                    395:     $method = "SUPER::$method";
                    396:     return $self->$method(@_);
                    397: }
                    398:
                    399: =pod
                    400:
                    401: =head2 B<open> - Open a connection to a Trango AP.
                    402:
                    403: Calls Net::Telnet::open() then makes sure you get a password prompt so
                    404: you are ready to login() and parses the login banner so you can get
                    405: host_type() and firmware_version()
                    406:
                    407: =cut
                    408:
                    409: sub open {
                    410:     my $self = shift;
                    411:
                    412:     unless ( $self->SUPER::open(@_) ) {
                    413:         $self->last_error( "Couldn't connect to " . $self->host . ":  $!" );
                    414:         return;
                    415:     }
                    416:
                    417:     ## Get to login prompt
                    418:     unless (
                    419:         $self->waitfor(
                    420:             -match   => '/password: ?$/i',
                    421:             -errmode => "return",
                    422:         )
                    423:       )
                    424:     {
                    425:         $self->last_error( "problem connecting to host ("
                    426:               . $self->host . "): "
                    427:               . $self->lastline );
                    428:         return;
                    429:     }
                    430:
                    431:     $self->parse_login_banner( $self->lastline );
                    432:
                    433:     $self->is_connected(1);
                    434:
                    435:     return $self->is_connected;
                    436: }
                    437:
                    438: =pod
                    439:
                    440: =head2 B<login> - Login to the AP.
                    441:
                    442: Calls open() if not already connected, then sends the password and sets
                    443: logged_in() if successful
                    444:
                    445: =cut
                    446:
                    447: sub login {
                    448:     my $self = shift;
                    449:
                    450:     unless ( $self->is_connected ) {
                    451:         $self->open or return;
                    452:     }
                    453:
                    454:     my $password = shift;
                    455:
                    456:     $self->print($password);
                    457:     unless (
                    458:         $self->waitfor(
                    459:             -match   => $self->prompt,
                    460:             -errmode => "return",
                    461:         )
                    462:       )
                    463:     {
                    464:         $self->last_error( "login ($self->host) failed: " . $self->lastline );
                    465:         return;
                    466:     }
                    467:
                    468:     $self->logged_in(1);
                    469:
                    470:     return $self->logged_in;
                    471: }
                    472:
                    473: =pod
                    474:
                    475: =head2 B<parse_login_banner> - Converts the login_banner to something useful.
                    476:
                    477: Takes a login banner (what you get when you first connect to the Trango)
                    478: or reads what is already in login_banner() then parses it and sets
                    479: host_type() and firmware_version() as well as login_banner()
                    480:
                    481: =cut
                    482:
                    483: sub parse_login_banner {
                    484:     my $self = shift;
                    485:
                    486:     if (@_) {
                    487:         $self->login_banner(@_);
                    488:     }
                    489:
                    490:     my $banner = $self->login_banner;
                    491:
                    492:     my ( $type, $sep1, $subtype, $sep2, $ver ) =
                    493:       $banner =~
                    494:       /Welcome to Trango Broadband Wireless (\S+)([\s-]+)(\S+)([\s-]+)(.+)$/i;
                    495:
                    496:     $type .= $sep1 . $subtype;
                    497:     $ver = $subtype . $sep2 . $ver;
                    498:
                    499:     $self->login_banner($banner);
                    500:     $self->host_type($type);
                    501:     $self->firmware_version($ver);
                    502:
                    503:     return 1;
                    504: }
                    505:
                    506: =pod
                    507:
                    508: =head2 B<su_password> - Set the password on SUs connected to the AP.
                    509:
                    510: su_password('new_password'[, 'suid']) If no suid is specified,
                    511: the default is "all".
                    512:
                    513:   $t->su_password('good_pass', 5);
                    514:
                    515: =cut
                    516:
                    517: sub su_password {
                    518:     my $self     = shift;
                    519:     my $new_pass = shift || $EMPTY;
                    520:     my $su       = shift || 'all';
                    521:
                    522:     unless ( defined $new_pass ) {
                    523:         $self->last_error("No new password");
                    524:
                    525:         #return;
                    526:     }
                    527:
                    528:     return $self->cmd(
                    529:         String => 'su password ' . $su . $SPACE . $new_pass . $SPACE . $new_pass,
                    530:         expect => $success,
                    531:     );
                    532: }
                    533:
                    534: =pod
                    535:
                    536: =head2 B<su_ipconfig> - Change IP configuration on SUs connected to the AP.
                    537:
                    538: su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )
                    539:
                    540:   $t->su_ipconfig( 5, '10.0.1.5', '255.255.255.0', '10.0.1.1' );
                    541:
                    542: =cut
                    543:
                    544: sub su_ipconfig {
                    545:     my $self = shift;
                    546:
                    547:     my $suid        = shift;
                    548:     my $new_ip      = shift;
                    549:     my $new_subnet  = shift;
                    550:     my $new_gateway = shift;
                    551:
                    552:     if ( $suid =~ /\D/ ) {
                    553:         $self->last_error("Invalid suid '$suid'");
                    554:         return;
                    555:     }
                    556:     unless ($new_ip) {
                    557:         $self->last_error("no new_ip passed");
                    558:         return;
                    559:     }
                    560:     unless ($new_subnet) {
                    561:         $self->last_error("no new_subnet passed");
                    562:         return;
                    563:     }
                    564:     unless ($new_gateway) {
                    565:         $self->last_error("no new_gateway passed");
                    566:         return;
                    567:     }
                    568:
                    569:     # su ipconfig <suid> <new ip> <new subnet> <new gateway>
                    570:     return $self->cmd(
                    571:         String => 'su ipconfig ' . $suid . $SPACE . $new_ip . $SPACE
                    572:           . $new_subnet . $SPACE
                    573:           . $new_gateway,
                    574:         expect => $success,
                    575:     );
                    576: }
                    577:
                    578: =pod
                    579:
                    580: =head2 B<sudb_view> - Returns the output from the sudb view command
                    581:
                    582: returns a reference to an array of hashes each containing these keys
                    583: 'suid', 'type', 'cir', 'mir' and 'mac'
                    584:
                    585: =cut
                    586:
                    587: sub sudb_view {
                    588:     my $self = shift;
                    589:
                    590:     my $lines = $self->cmd( String => 'sudb view', expect => $success ) || [];
                    591:
                    592:     return unless @{$lines};
                    593:
                    594:     unless ( $PRIVATE{'Decode'} ) {
                    595:         return $lines;
                    596:     }
                    597:
                    598:     my @sus;
                    599:     foreach ( @{$lines} ) {
                    600:         next unless $_;
                    601:         if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
                    602:             my %s = (
                    603:                 suid => $1,
                    604:                 type => $2,
                    605:                 cir  => $3,
                    606:                 mir  => $4,
                    607:                 mac  => $5,
                    608:             );
                    609:
                    610:             $s{'mac'} =~ s/\s//g;
                    611:             $s{'mac'} = uc( $s{'mac'} );
                    612:
                    613:             push @sus, \%s;
                    614:         }
                    615:     }
                    616:
                    617:     return \@sus;
                    618: }
                    619:
                    620: =pod
                    621:
                    622: =head2 B<sudb_add> - Adds an su to the sudb
                    623:
                    624: Takes the following paramaters
                    625:
                    626:     suid : numeric,
                    627:     type : (reg|pr)
                    628:     cir  : numeric,
                    629:     mir  : numeric,
                    630:     mac  : Almost any format, it will be reformatted,
                    631:
                    632: and returns true on success or undef otherwise.
                    633:
                    634:   $t->sudb_add($suid, 'reg', $cir, $mir, $mac);
                    635:
                    636: You should save_sudb() after calling this, or your changes  will be lost
                    637: when the AP is rebooted.
                    638:
                    639: =cut
                    640:
                    641: sub sudb_add {
                    642:     my $self = shift;
                    643:     my $suid = shift;
                    644:     my $type = shift;
                    645:     my $cir  = shift;
                    646:     my $mir  = shift;
                    647:     my $mac  = shift;
                    648:
                    649:     if ( $suid =~ /\D/ ) {
                    650:         $self->last_error("Invalid suid '$suid'");
                    651:         return;
                    652:     }
                    653:
                    654:     unless ( lc($type) eq 'reg' || lc($type) eq 'pr' ) {
                    655:         $self->last_error("Invalid type '$type'");
                    656:         return;
                    657:     }
                    658:
                    659:     if ( $cir =~ /\D/ ) {
                    660:         $self->last_error("Invalid CIR '$cir'");
                    661:         return;
                    662:     }
                    663:
                    664:     if ( $mir =~ /\D/ ) {
                    665:         $self->last_error("Invalid MIR '$mir'");
                    666:         return;
                    667:     }
                    668:
                    669:     my $new_mac = $mac;
                    670:     $new_mac =~ s/[^0-9A-Fa-f]//;
                    671:     unless ( length $new_mac == 12 ) {
                    672:         $self->last_error("Invalid MAC '$mac'");
                    673:         return;
                    674:     }
                    675:     $new_mac = join $SPACE, $new_mac =~ /../g;
                    676:
                    677:     my $string =
                    678:       'sudb add ' . $suid . $SPACE . $type . $SPACE . $cir . $SPACE . $mir . $SPACE
                    679:       . $new_mac;
                    680:
                    681:     return $self->cmd( String => $string, expect => $success );
                    682: }
                    683:
                    684: =pod
                    685:
                    686: =head2 B<sudb_delete> - removes an su from the sudb
                    687:
                    688: Takes either 'all' or the  suid of the su to delete
                    689: and returns true on success or undef otherwise.
                    690:
                    691:   $t->sudb_delete($suid);
                    692:
                    693: You should save_sudb() after calling this, or your changes  will be lost
                    694: when the AP is rebooted.
                    695:
                    696: =cut
                    697:
                    698: sub sudb_delete {
                    699:     my $self = shift;
                    700:     my $suid = shift;
                    701:
                    702:     #if (lc($suid) ne 'all' || $suid =~ /\D/) {
                    703:     if ( $suid =~ /\D/ ) {
                    704:         $self->last_error("Invalid suid '$suid'");
                    705:         return;
                    706:     }
                    707:
                    708:     return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
                    709: }
                    710:
                    711: =pod
                    712:
                    713: =head2 B<sudb_modify> - changes the su information in the sudb
                    714:
                    715: Takes either the  suid of the su to change
                    716: as well as what you are changing, either "cir, mir or su2su"
                    717: and returns true on success or undef otherwise.
                    718:
                    719: cir and mir also take a value to set the cir/mir to.
                    720:
                    721: su2su takes a group id parameter that is in hex.
                    722:
                    723:   $t->sudb_modify($suid, 'cir', 512);
                    724:
                    725: You should save_sudb() after calling this, or your changes  will be lost
                    726: when the AP is rebooted.
                    727:
                    728: =cut
                    729:
                    730: sub sudb_modify {
                    731:     my $self  = shift;
                    732:     my $suid  = shift;
                    733:     my $opt   = shift;
                    734:     my $value = shift;
                    735:
                    736:     if ( $suid =~ /\D/ ) {
                    737:         $self->last_error("Invalid suid '$suid'");
                    738:         return;
                    739:     }
                    740:
                    741:     if ( lc($opt) eq 'cir' or lc($opt) eq 'mir' ) {
                    742:         if ( $value =~ /\D/ ) {
                    743:             $self->last_error("Invalid $opt '$value'");
                    744:             return;
                    745:         }
                    746:     }
                    747:     elsif ( lc($opt) eq 'su2su' ) {
                    748:         if ( $value =~ /[^0-9A-Za-f]/ ) {
                    749:             $self->last_error("Invalid MAC '$value'");
                    750:             return;
                    751:         }
                    752:     }
                    753:     else {
                    754:         $self->last_error("Invalid option '$opt'");
                    755:         return;
                    756:     }
                    757:
                    758:     my $string = 'sudb modify ' . $suid . $SPACE . $opt . $SPACE . $value;
                    759:
                    760:     return $self->cmd( String => $string, expect => $success );
                    761: }
                    762:
                    763: =pod
                    764:
                    765: =head2 B<enable_tftpd> - enable the TFTP server
                    766:
                    767: runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
                    768:
                    769: =cut
                    770:
                    771: sub enable_tftpd {
                    772:     my $self = shift;
                    773:
                    774:     my $vals = $self->tftpd( args => 'on' );
                    775:
                    776:     if ( ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'listen' ) {
                    777:         return $vals;
                    778:     }
                    779:     else {
                    780:         return;
                    781:     }
                    782: }
                    783:
                    784: =pod
                    785:
                    786: =head2 B<disable_tftpd> - disable the TFTP server
                    787:
                    788: runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
                    789:
                    790: =cut
                    791:
                    792: sub disable_tftpd {
                    793:     my $self = shift;
                    794:
                    795:     my $vals = $self->tftpd( args => 'off' );
                    796:
                    797:     if ( ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled' ) {
                    798:         return $vals;
                    799:     }
                    800:     else {
                    801:         return;
                    802:     }
                    803: }
                    804:
                    805: =pod
                    806:
                    807: =head2 B<cmd> - runs a command on the AP.
                    808:
                    809: This does most of the work.  At the heart, it calls Net::Telnet::cmd()
                    810: but it also does some special stuff for Trango.
                    811:
                    812: Normally returns the last lines from from the command
                    813:
                    814: If you are using this, rather than one of the "easy" methods above,
                    815: you probably want to read through the source of this module to see how
                    816: some of the other commands are called.
                    817:
                    818: In addition to the Net::Telnet::cmd() options, it also accepts these:
                    819:
                    820: I<decode>
                    821: - if this is true, then it will send the output lines to _decode_lines()
                    822: and then returns the decoded output
                    823:
                    824: I<no_prompt>
                    825: - if this is true, it does not wait for a prompt, so you are not stuck
                    826: waiting for something that will never happen.
                    827:
                    828: I<cmd_disconnects>
                    829: - if this is true, it then sets logged_in() to false, then it will
                    830: close() the connection and set is_connected() to false
                    831:
                    832: I<expect>
                    833: - if this is set (usually to 'Success.') it will check for that in the
                    834: last line of output and if it does not, will return undef because the
                    835: command probably failed
                    836:
                    837: I<args>
                    838: - a string containing the command line options that are passed to the
                    839: command
                    840:
                    841:     $t->cmd( String => 'exit', no_prompt => 1, cmd_disconnects => 1 );
                    842:
                    843: =cut
                    844:
                    845: sub cmd {
                    846:     my $self = shift;
                    847:
                    848:     my @valid_net_telnet_opts = qw(
                    849:       String
                    850:       Output
                    851:       Cmd_remove_mode
                    852:       Errmode
                    853:       Input_record_separator
                    854:       Ors
                    855:       Output_record_separator
                    856:       Prompt
                    857:       Rs
                    858:       Timeout
                    859:     );
                    860:
                    861:     my %cfg;
                    862:     if ( @_ == 1 ) {
                    863:         $cfg{'String'} = shift;
                    864:     }
                    865:     elsif ( @_ > 1 ) {
                    866:         %cfg = @_;
                    867:     }
                    868:
                    869:     $cfg{'Timeout'} ||= $self->Timeout;
                    870:
                    871:     unless ( $cfg{'String'} ) {
                    872:         $self->last_error("No command passed");
                    873:         return;
                    874:     }
                    875:
                    876:     unless ( $self->is_connected ) {
                    877:         $self->last_error("Not connected");
                    878:         return;
                    879:     }
                    880:
                    881:     unless ( $self->logged_in ) {
                    882:         $self->last_error("Not logged in");
                    883:         return;
                    884:     }
                    885:
                    886:     my %cmd;
                    887:     foreach (@valid_net_telnet_opts) {
                    888:         if ( exists $cfg{$_} ) {
                    889:             $cmd{$_} = $cfg{$_};
                    890:         }
                    891:     }
                    892:     if ( $cfg{'args'} ) {
                    893:         $cmd{'String'} .= $SPACE . $cfg{'args'};
                    894:     }
                    895:
                    896:     my @lines;
                    897:     if ( $cfg{'no_prompt'} ) {
                    898:         $self->print( $cmd{'String'} );
                    899:         @lines = $self->lastline;
                    900:     }
                    901:     else {
                    902:         @lines = $self->SUPER::cmd(%cmd);
                    903:     }
                    904:
                    905:     $self->last_lines( \@lines );
                    906:
                    907:     my $vals = 1;
                    908:     if ( $PRIVATE{'Decode'} && $cfg{'decode'} ) {
                    909:         if ( $cfg{'decode'} eq 'each' ) {
                    910:             $vals = _decode_each_line(@lines);
                    911:         }
                    912:         elsif ( $cfg{'decode'} eq 'sulog' ) {
                    913:             $vals = _decode_sulog(@lines);
                    914:         }
                    915:         elsif ( $cfg{'decode'} eq 'maclist' ) {
                    916:             $vals = _decode_maclist(@lines);
1.41      andrew    917:             if (! $vals) {
                    918:                 $self->last_error("Error decoding maclist");
                    919:             }
1.40      andrew    920:         }
                    921:         else {
                    922:             $vals = _decode_lines(@lines);
                    923:         }
                    924:     }
                    925:
                    926:     $self->last_vals($vals);
                    927:
                    928:     my $last = $self->lastline;
                    929:
                    930:     if ( ( not $cfg{'expect'} ) || $last =~ /$cfg{'expect'}$/ ) {
                    931:         if ( $cfg{'cmd_disconnects'} ) {
                    932:             $self->logged_in(0);
                    933:             $self->close;
                    934:             $self->is_connected(0);
                    935:         }
                    936:
                    937:         if ( $PRIVATE{'Decode'} && $cfg{'decode'} ) {
                    938:             return $vals;
                    939:         }
                    940:         else {
                    941:             return \@lines;
                    942:         }
                    943:     }
                    944:     else {
                    945:         my $err;
                    946:         if (grep { /\[ERR\]/ } @lines) {
                    947:             $err = _decode_lines(@lines);
1.43    ! andrew    948:         }
1.40      andrew    949:
1.42      andrew    950:         if (ref $err eq 'HASH' && $err->{ERR}) {
1.40      andrew    951:             $self->last_error($err->{ERR} );
                    952:         } else {
1.42      andrew    953:             $self->last_error("Error with command ($cmd{'String'}): $last");
1.43    ! andrew    954:            $self->_clear();
1.40      andrew    955:         }
                    956:         return;
                    957:     }
                    958: }
                    959:
                    960: #=item _decode_lines
                    961:
                    962: sub _decode_lines {
                    963:     my @lines = @_;
                    964:
                    965:     my %conf;
                    966:
                    967:     my $key = $EMPTY;
                    968:     my $val = undef;
                    969:     my @vals;
                    970:     my $in_key = 0;
                    971:     my $in_val = 1;
                    972:
                    973:     foreach my $line (@lines) {
                    974:         next if $line =~ /$success$/;
                    975:
                    976:         my @chars = split //, $line;
                    977:
                    978:         my $last_key = $EMPTY;
                    979:         foreach my $c (@chars) {
                    980:
                    981:             if ( $c eq '[' || $c eq "\r" || $c eq "\n" ) {
                    982:                 if ( $c eq '[' ) {
                    983:                     $in_key = 1;
                    984:                     $in_val = 0;
                    985:                 }
                    986:                 else {
                    987:                     $in_key = 0;
                    988:                     $in_val = 1;
                    989:                 }
                    990:
                    991:                 if ($key) {
                    992:                     $key =~ s/^\s+//;
                    993:                     $key =~ s/\s+$//;
                    994:
                    995:                     if ($val) {
                    996:                         $val =~ s/^\s+//;
                    997:                         $val =~ s/\s+$//;
                    998:                     }
                    999:
                   1000:                     if ( $key eq 'Checksum' && $last_key ) {
                   1001:
                   1002:                         # Special case for these bastids.
                   1003:                         my $new = $last_key;
                   1004:                         $new =~ s/\s+\S+$//;
                   1005:                         $key = $new . $SPACE . $key;
                   1006:                     }
                   1007:
                   1008:                     $conf{$key} = $val;
                   1009:                     $last_key   = $key;
                   1010:                     $key        = $EMPTY;
                   1011:                 }
                   1012:                 elsif ($val) {
                   1013:                     push @vals, $val;
                   1014:                 }
                   1015:                 $val = $EMPTY;
                   1016:
                   1017:             }
                   1018:             elsif ( $c eq ']' ) {
                   1019:                 $in_val = 1;
                   1020:                 $in_key = 0;
                   1021:                 $c      = shift @chars;
                   1022:
                   1023:             }
                   1024:             elsif ($in_key) {
                   1025:                 $key .= $c;
                   1026:
                   1027:             }
                   1028:             elsif ($in_val) {
                   1029:                 $val .= $c;
                   1030:             }
                   1031:         }
                   1032:     }
                   1033:
                   1034:     unless ($key) {
                   1035:         push @vals, $val;
                   1036:     }
                   1037:
                   1038:     if ( @vals == 1 ) {
                   1039:         $val = $vals[0];
                   1040:     }
                   1041:     elsif (@vals) {
                   1042:         $val = \@vals;
                   1043:     }
                   1044:     else {
                   1045:         $val = undef;
                   1046:     }
                   1047:
                   1048:     if (%conf) {
                   1049:         $conf{_pre} = $val if $val;
                   1050:         return \%conf;
                   1051:     }
                   1052:     else {
                   1053:         return $val;
                   1054:     }
                   1055: }
                   1056:
                   1057: #=item _decode_each_line
                   1058:
                   1059: sub _decode_each_line {
                   1060:     my @lines = @_;
                   1061:     my @decoded;
                   1062:     foreach my $line (@lines) {
                   1063:         my $decoded = _decode_lines($line);
                   1064:         push @decoded, $decoded if defined $decoded;
                   1065:     }
                   1066:     return \@decoded;
                   1067: }
                   1068:
                   1069: #=item _decode_sulog
                   1070:
                   1071: sub _decode_sulog {
                   1072:     my @lines = @_;
                   1073:     my @decoded;
                   1074:     my $last_tm;
                   1075:     foreach my $line (@lines) {
                   1076:         my $decoded = _decode_lines($line);
                   1077:
                   1078:         if ( defined $decoded ) {
                   1079:             if ( $decoded->{'tm'} ) {
                   1080:                 $last_tm = $decoded->{'tm'};
                   1081:                 next;
                   1082:             }
                   1083:             else {
                   1084:                 $decoded->{'tm'} = $last_tm;
                   1085:             }
                   1086:             next unless $last_tm;
                   1087:
                   1088:             push @decoded, $decoded if defined $decoded;
                   1089:         }
                   1090:     }
                   1091:     return \@decoded;
                   1092: }
                   1093:
                   1094: #=item _decode_maclist
                   1095:
                   1096: sub _decode_maclist {
                   1097:     my @lines = @_;
                   1098:     my @decoded;
                   1099:     my $total_entries = 0;
                   1100:     my $current_tm    = 0;
                   1101:     foreach my $line (@lines) {
                   1102:         $line =~ s/\r?\n$//;
                   1103:         my ( $mac, $loc, $tm ) = $line =~ /
                   1104:             ([0-9a-fA-F ]{17})\s+
                   1105:             (.*)\s+
                   1106:             tm\s+
                   1107:             (\d+)
                   1108:         /x;
                   1109:
                   1110:         if ($mac) {
                   1111:             $mac =~ s/\s+//g;
                   1112:             $loc =~ s/^\s+//;
                   1113:             $loc =~ s/\s+$//;
                   1114:
                   1115:             my $suid = undef;
                   1116:             if ( $loc =~ /suid\s+=\s+(\d+)/ ) {
                   1117:                 $suid = $1;
                   1118:                 $loc  = undef;
                   1119:             }
                   1120:
                   1121:             push @decoded,
                   1122:               {
                   1123:                 mac  => $mac,
                   1124:                 loc  => $loc,
                   1125:                 tm   => $tm,
                   1126:                 suid => $suid,
                   1127:               };
                   1128:         }
                   1129:         elsif ( $line =~ /(\d+)\s+entries/ ) {
                   1130:             $total_entries = $1;
                   1131:         }
                   1132:         elsif ( $line =~ /current tm = (\d+)\s+sec/ ) {
                   1133:             $current_tm = $1;
                   1134:         }
                   1135:     }
                   1136:
                   1137:     map { $_->{'cur_tm'} = $current_tm } @decoded;
                   1138:
                   1139:     if ( scalar @decoded == $total_entries ) {
                   1140:         return \@decoded;
                   1141:     }
                   1142:     else {
                   1143:         return;
                   1144:     }
                   1145: }
                   1146:
                   1147: 1;    # End of Net::Telnet::Trango
                   1148: __END__
                   1149:
                   1150: =head1 SEE ALSO
                   1151:
                   1152: Trango Documentation -
                   1153: L<http://www.trangobroadband.com/support/product_docs.htm>
                   1154:
                   1155: L<Net::Telnet>
                   1156:
                   1157: =head1 TODO
                   1158:
                   1159: There are still a lot of commands that are not accessed directly.  If
                   1160: you call them (as cmd("command + args") or whatever) and it works,
                   1161: please send me examples that work and I will try to get it incorporated
                   1162: into the next version of the script.
                   1163:
                   1164: I also want to be able to parse the different types of output from
                   1165: commands like su, sudb all and anything else that would be better
                   1166: available as a perl datastructure.
                   1167:
                   1168: =head1 AUTHOR
                   1169:
                   1170: Andrew Fresh E<lt>andrew@rraz.netE<gt>
                   1171:
                   1172: =head1 SUPPORT
                   1173:
                   1174: You can find documentation for this module with the perldoc command.
                   1175:
                   1176:     perldoc Net::Telnet::Trango
                   1177:
                   1178: You can also look for information at:
                   1179:
                   1180: =over 4
                   1181:
                   1182: =item * AnnoCPAN: Annotated CPAN documentation
                   1183:
                   1184: L<http://annocpan.org/dist/Net-Telnet-Trango>
                   1185:
                   1186: =item * CPAN Ratings
                   1187:
                   1188: L<http://cpanratings.perl.org/d/Net-Telnet-Trango>
                   1189:
                   1190: =item * RT: CPAN's request tracker
                   1191:
                   1192: L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Telnet-Trango>
                   1193:
                   1194: =item * Search CPAN
                   1195:
                   1196: L<http://search.cpan.org/dist/Net-Telnet-Trango>
                   1197:
                   1198: =back
                   1199:
                   1200: =head1 COPYRIGHT AND LICENSE
                   1201:
                   1202: Copyright (C) 2005,2006,2007 by Andrew Fresh
                   1203:
                   1204: This program is free software; you can redistribute it and/or modify it
                   1205: under the same terms as Perl itself.
                   1206:
                   1207: =cut

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