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

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

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