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

1.14      andrew      1: package Net::Telnet::Trango;
1.29    ! andrew      2: # $RedRiver: Trango.pm,v 1.28 2007/02/01 17:10:07 mike 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
1.27      mike       68: instead return a reference to an array of the lines that were returned
                     69: from the command.
1.14      andrew     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<firmware_version>
1.14      andrew    183:
1.26      andrew    184: - returns the firmware version
1.14      andrew    185:
1.26      andrew    186: Returns the firmware version if available, otherwise undef.
1.14      andrew    187:
1.26      andrew    188: It should be available after a successful open().
1.14      andrew    189:
1.26      andrew    190: =item B<host_type>
                    191:
                    192: - return the type of host you are connected to.
1.14      andrew    193:
                    194: returns the type of host from the login banner for example M5830S or M5300S.
                    195:
1.26      andrew    196: Should be available after a successful open().
                    197:
                    198: =item B<is_connected>
                    199:
                    200: - Status of the connection to host.
                    201:
                    202: returns 1 when connected, undef otherwise.
                    203:
                    204: =item B<logged_in>
1.14      andrew    205:
1.26      andrew    206: - Status of being logged in to the host.
1.14      andrew    207:
1.26      andrew    208: returns 1 after a successful login(), 0 if it failed and undef if
                    209: login() was never called.
1.14      andrew    210:
1.26      andrew    211: =item B<login_banner>
1.14      andrew    212:
1.26      andrew    213: - The banner when first connecting to the host.
1.14      andrew    214:
1.20      andrew    215: returns the banner that is displayed when first connected at login.
1.26      andrew    216: Only set after a successful open().
1.14      andrew    217:
1.26      andrew    218: =item B<last_lines>
1.14      andrew    219:
1.26      andrew    220: - The last lines of output from the last cmd().
1.14      andrew    221:
1.26      andrew    222: returns, as an array ref, the output from the last cmd() that was run.
1.14      andrew    223:
1.26      andrew    224: =item B<last_error>
1.25      andrew    225:
1.26      andrew    226: - A text output of the last error that was encountered.
                    227:
                    228: returns the last error reported.  Probably contains the last entry in
                    229: last_lines.
1.25      andrew    230:
1.14      andrew    231: =back
                    232:
                    233: =head2 ALIASES
                    234:
                    235: =over
                    236:
1.26      andrew    237: =item B<bye>
1.14      andrew    238:
1.26      andrew    239: - alias of L<exit()>.
1.14      andrew    240:
1.26      andrew    241: =item B<restart>
1.14      andrew    242:
1.26      andrew    243: - alias of L<reboot()>.
1.14      andrew    244:
                    245: =back
                    246:
                    247: =head2 COMMANDS
                    248:
1.19      andrew    249: Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>,
                    250: as such they accept the same options as C<cmd()>.
                    251: Specifically they take a named paramater "args", for example:
1.14      andrew    252: C<tftpd(args =E<gt> 'on')> would enable tftpd
                    253:
                    254: =over
                    255:
1.26      andrew    256: =item B<tftpd>
                    257:
                    258: - Returns a hash ref of the decoded output from the
                    259: command.
1.14      andrew    260:
1.26      andrew    261: Also see enable_tftpd() and disable_tftpd() as those check that it was
                    262: successfully changed.
1.14      andrew    263:
1.26      andrew    264: =item B<ver>
1.14      andrew    265:
1.26      andrew    266: - Returns a hash ref of the decoded output from the
                    267: command.
1.14      andrew    268:
1.26      andrew    269: =item B<sysinfo>
1.14      andrew    270:
1.26      andrew    271: - Returns a hash ref of the decoded output from the
                    272: command.
1.14      andrew    273:
1.26      andrew    274: =item B<exit>
1.14      andrew    275:
1.26      andrew    276: - exits the command session with the Trango and closes
                    277: the connection
1.14      andrew    278:
1.26      andrew    279: =item B<reboot>
1.14      andrew    280:
1.26      andrew    281: - reboots the Trango and closes the connection
1.14      andrew    282:
1.26      andrew    283: =item B<remarks>
1.14      andrew    284:
1.26      andrew    285: - Set or retrieve the remarks.
1.17      andrew    286:
1.18      andrew    287: Takes an optional argument, which sets the remarks.
                    288: If there is no argument, returns the current remarks.
1.17      andrew    289:
1.26      andrew    290:   my $old_remarks = $t->remarks();
                    291:   $t->remarks($new_remarks);
                    292:
                    293: =item B<sulog>
                    294:
                    295: - returns an array ref of hashes containing each log
                    296: line.
                    297:
                    298: =item B<save_sudb>
1.14      andrew    299:
1.26      andrew    300: - returns true on success, undef on failure
1.14      andrew    301:
1.26      andrew    302: =item B<syslog>
1.14      andrew    303:
1.26      andrew    304: - returns the output from the syslog command
1.14      andrew    305:
1.26      andrew    306: =item B<pipe>
1.14      andrew    307:
1.26      andrew    308: - returns the output from the pipe command
1.14      andrew    309:
1.26      andrew    310: =item B<maclist>
1.14      andrew    311:
1.26      andrew    312: -  returns the output from the maclist command
1.14      andrew    313:
1.26      andrew    314: =item B<maclist_reset>
1.14      andrew    315:
1.26      andrew    316: - resets the maclist.
1.14      andrew    317:
1.26      andrew    318: No useful output.
1.14      andrew    319:
1.26      andrew    320: =item B<eth_list>
1.14      andrew    321:
1.26      andrew    322: - returns the output from the eth list command
1.14      andrew    323:
1.26      andrew    324: =item B<su_info>
1.14      andrew    325:
1.26      andrew    326: - returns information about the SU.
1.17      andrew    327:
1.26      andrew    328: You need to pass in the $suid and it will return the info for that suid.
1.17      andrew    329:
1.26      andrew    330:   $t->su_info($suid);
1.17      andrew    331:
1.26      andrew    332: =item B<save_ss>
                    333:
                    334: - saves the config.
                    335:
                    336: Returns 1 on success, undef on failure.
1.17      andrew    337:
1.14      andrew    338: =cut
                    339:
                    340:
1.27      mike      341: my $success = 'Success\\.';
1.14      andrew    342: my %COMMANDS = (
                    343:   tftpd       => { decode => 'all',   expect => $success },
                    344:   ver         => { decode => 'all' },
                    345:   sysinfo     => { decode => 'all',   expect => $success },
                    346:   updateflash => { decode => 'all',   expect => $success },
                    347:   sulog       => { decode => 'sulog', expect => $success },
                    348:   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
                    349:   reboot      => { no_prompt => 1, cmd_disconnects => 1 },
1.17      andrew    350:   remarks     => { decode => 'all', expect => $success },
1.14      andrew    351:   save_sudb   => { String => 'save sudb', expect => $success },
                    352:   syslog      => { expect => $success },
                    353:   'pipe'      => { }, # XXX needs a special decode
                    354:   maclist     => { decode => 'maclist' },
                    355:   maclist_reset => { String => 'maclist reset', expect => 'done' },
                    356:   eth_link    => { String => 'eth link', expect => $success },
1.17      andrew    357:   su_info     => { String => 'su info', decode => 'all', expect => $success },
                    358:   save_ss     => { String => 'save ss', expect => $success },
1.21      andrew    359:   opmode      => { decode => 'all',   expect => $success },
1.14      andrew    360:   # eth r, w and reset???
                    361:   #su password???
                    362:   #_bootloader
                    363:   #temp
                    364:   #heater
                    365: );
                    366:
                    367: my %ALIASES = (
                    368:   bye     => 'exit',
                    369:   restart => 'reboot',
1.27      mike      370:   Host    => 'host',
1.14      andrew    371: );
                    372:
                    373: my %ACCESS = map { $_ => 1 } qw(
                    374:   firmware_version
                    375:   host_type
                    376:   is_connected
                    377:   logged_in
                    378:   login_banner
                    379:   Timeout
                    380:   last_lines
                    381:   last_vals
1.25      andrew    382:   last_error
1.29    ! andrew    383:   Decode
1.14      andrew    384: );
                    385:
                    386: sub AUTOLOAD
                    387: {
                    388:   my $self = shift;
                    389:
                    390:   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
                    391:     or die "Weird: $AUTOLOAD";
                    392:
                    393:   if (exists $ALIASES{$method}) {
                    394:     $method = $ALIASES{$method};
                    395:     return $self->$method(@_);
                    396:   }
                    397:
                    398:   if (exists $COMMANDS{$method}) {
                    399:     $COMMANDS{$method}{'String'} ||= $method;
1.17      andrew    400:     $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
1.14      andrew    401:     return $self->cmd(%{ $COMMANDS{$method} }, @_);
                    402:   }
                    403:
                    404:   if (exists $ACCESS{$method}) {
                    405:     my $prev = $PRIVATE{$method};
                    406:     ($PRIVATE{$method}) = @_ if @_;
                    407:     return $prev;
                    408:   }
                    409:
                    410:   $method = "SUPER::$method";
                    411:   return $self->$method(@_);
                    412: }
                    413:
                    414: =pod
                    415:
1.26      andrew    416: =item B<open>
                    417:
                    418: - Open a connection to a Trango AP.
1.14      andrew    419:
1.20      andrew    420: Calls Net::Telnet::open() then makes sure you get a password prompt so
                    421: you are ready to login() and parses the login banner so you can get
                    422: host_type() and firmware_version()
1.14      andrew    423:
                    424: =cut
                    425:
                    426: sub open
                    427: {
                    428:   my $self = shift;
                    429:
                    430:   unless ( $self->SUPER::open(@_) ) {
1.27      mike      431:     $self->last_error("Couldn't connect to " . $self->host . ":  $!");
1.29    ! andrew    432:     return;
1.14      andrew    433:   }
                    434:
                    435:   ## Get to login prompt
                    436:   unless ($self->waitfor(
                    437:       -match => '/password: ?$/i',
                    438:       -errmode => "return",
                    439:     ) ) {
1.27      mike      440:     $self->last_error("problem connecting to host (" . $self->host . "): " .
1.25      andrew    441:         $self->lastline);
1.29    ! andrew    442:     return;
1.14      andrew    443:   }
                    444:
                    445:   $self->parse_login_banner($self->lastline);
                    446:
                    447:   $self->is_connected(1);
                    448:
                    449:   return $self->is_connected;
                    450: }
                    451:
                    452: =pod
                    453:
1.26      andrew    454: =item B<login>
                    455:
                    456: - Login to the AP.
1.14      andrew    457:
1.20      andrew    458: Calls open() if not already connected, then sends the password and sets
                    459: logged_in() if successful
1.14      andrew    460:
                    461: =cut
                    462:
                    463: sub login
                    464: {
                    465:   my $self = shift;
                    466:
                    467:   unless ($self->is_connected) {
1.29    ! andrew    468:     $self->open or return;
1.14      andrew    469:   }
                    470:
                    471:   my $password = shift;
                    472:
                    473:   $self->print($password);
                    474:   unless ($self->waitfor(
                    475:     -match => $self->prompt,
                    476:     -errmode => "return",
                    477:   ) ) {
1.27      mike      478:     $self->last_error("login ($self->host) failed: " . $self->lastline);
1.29    ! andrew    479:     return;
1.14      andrew    480:   }
                    481:
                    482:   $self->logged_in(1);
                    483:
                    484:   return $self->logged_in;
                    485: }
                    486:
                    487: =pod
                    488:
1.26      andrew    489: =item B<parse_login_banner>
                    490:
                    491: - Converts the login_banner to some useful
                    492: variables.
1.14      andrew    493:
1.20      andrew    494: Takes a login banner (what you get when you first connect to the Trango)
                    495: or reads what is already in login_banner() then parses it and sets
                    496: host_type() and firmware_version() as well as login_banner()
1.14      andrew    497:
                    498: =cut
                    499:
                    500: sub parse_login_banner
                    501: {
                    502:   my $self = shift;
                    503:
                    504:   if (@_) {
                    505:     $self->login_banner(@_);
                    506:   }
                    507:
                    508:   my $banner = $self->login_banner;
                    509:
1.28      mike      510:   my ($type, $sep1, $subtype, $sep2, $ver) = $banner =~
                    511:     /Welcome to Trango Broadband Wireless (\S+)([\s-]+)(\S+)([\s-]+)(.+)$/i;
                    512:
                    513:   $type .= $sep1 . $subtype;
                    514:   $ver = $subtype . $sep2 . $ver;
1.14      andrew    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.29    ! andrew    544:     #return;
1.14      andrew    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'");
1.29    ! andrew    579:                return;
1.25      andrew    580:        }
                    581:        unless ($new_ip) {
                    582:                $self->last_error("no new_ip passed");
1.29    ! andrew    583:                return;
1.25      andrew    584:        }
                    585:        unless ($new_subnet) {
                    586:                $self->last_error("no new_subnet passed");
1.29    ! andrew    587:                return;
1.25      andrew    588:        }
                    589:        unless ($new_gateway) {
                    590:                $self->last_error("no new_gateway passed");
1.29    ! andrew    591:                return;
1.25      andrew    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:
1.29    ! andrew    619:   my $lines = $self->cmd( String => 'sudb view', expect => $success ) || [];
1.14      andrew    620:
1.29    ! andrew    621:   return unless @{ $lines };
1.14      andrew    622:
                    623:   unless ($PRIVATE{'Decode'}) {
1.29    ! andrew    624:     return $lines;
1.14      andrew    625:   }
                    626:
                    627:   my @sus;
1.29    ! andrew    628:   foreach (@{ $lines }) {
1.14      andrew    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.29    ! andrew    681:                return;
1.14      andrew    682:        }
                    683:
                    684:        unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
1.25      andrew    685:                $self->last_error("Invalid type '$type'");
1.29    ! andrew    686:                return;
1.14      andrew    687:        }
                    688:
                    689:        if ($cir =~ /\D/) {
1.25      andrew    690:                $self->last_error("Invalid CIR '$cir'");
1.29    ! andrew    691:                return;
1.14      andrew    692:        }
                    693:
                    694:        if ($mir =~ /\D/) {
1.25      andrew    695:                $self->last_error("Invalid MIR '$mir'");
1.29    ! andrew    696:                return;
1.14      andrew    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.29    ! andrew    703:                return;
1.14      andrew    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.29    ! andrew    740:                return;
1.14      andrew    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.29    ! andrew    774:                return;
1.14      andrew    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.29    ! andrew    780:                        return;
1.14      andrew    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.29    ! andrew    785:                        return;
1.14      andrew    786:                }
                    787:        } else {
1.25      andrew    788:                $self->last_error("Invalid option '$opt'");
1.29    ! andrew    789:                return;
1.14      andrew    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 {
1.29    ! andrew    816:     return;
1.14      andrew    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 {
1.29    ! andrew    839:     return;
1.14      andrew    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.29    ! andrew    918:     return;
1.14      andrew    919:   }
                    920:
                    921:   unless ($self->is_connected) {
1.25      andrew    922:     $self->last_error("Not connected");
1.29    ! andrew    923:     return;
1.14      andrew    924:   }
                    925:
                    926:   unless ($self->logged_in) {
1.25      andrew    927:     $self->last_error("Not logged in");
1.29    ! andrew    928:     return;
1.14      andrew    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 {
1.27      mike      979:       return \@lines;
1.14      andrew    980:     }
                    981:   } else {
1.25      andrew    982:     $self->last_error("Error with command ($cfg{'String'}): $last");
1.29    ! andrew    983:     return;
1.14      andrew    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.27      mike      997:   my @vals;
1.14      andrew    998:   my $in_key = 0;
1.17      andrew    999:   my $in_val = 1;
1.14      andrew   1000:
                   1001:   foreach my $line (@lines) {
                   1002:     next if $line =~ /$success$/;
                   1003:
                   1004:     my @chars = split //, $line;
                   1005:
                   1006:     my $last_key = '';
                   1007:     foreach my $c (@chars) {
                   1008:
                   1009:       if ($c eq '[' || $c eq "\r" || $c eq "\n") {
                   1010:         if ($c eq '[') {
                   1011:           $in_key = 1;
                   1012:           $in_val = 0;
                   1013:         } else {
                   1014:           $in_key = 0;
1.17      andrew   1015:           $in_val = 1;
1.14      andrew   1016:         }
                   1017:
                   1018:         if ($key) {
                   1019:           $key =~ s/^\s+//;
                   1020:           $key =~ s/\s+$//;
                   1021:
1.27      mike     1022:           if ($val) {
1.17      andrew   1023:             $val =~ s/^\s+//;
                   1024:             $val =~ s/\s+$//;
                   1025:           }
1.14      andrew   1026:
                   1027:           if ($key eq 'Checksum' && $last_key) {
                   1028:             # Special case for these bastids.
                   1029:             my $new = $last_key;
                   1030:             $new =~ s/\s+\S+$//;
                   1031:             $key = $new . " " . $key;
                   1032:           }
                   1033:
1.27      mike     1034:           $conf{$key} = $val;
1.14      andrew   1035:           $last_key = $key;
                   1036:           $key = '';
1.27      mike     1037:         } elsif ($val) {
                   1038:                        push @vals, $val;
                   1039:                }
                   1040:         $val = '';
1.14      andrew   1041:
                   1042:       } elsif ($c eq ']') {
                   1043:         $in_val = 1;
                   1044:         $in_key = 0;
                   1045:         $c = shift @chars;
                   1046:
                   1047:       } elsif ($in_key) {
                   1048:         $key .= $c;
                   1049:
                   1050:       } elsif ($in_val) {
                   1051:         $val .= $c;
                   1052:       }
                   1053:     }
                   1054:   }
                   1055:
1.27      mike     1056:   unless ($key) {
                   1057:     push @vals, $val;
                   1058:   }
                   1059:
                   1060:   if (@vals == 1) {
                   1061:     $val = $vals[0];
                   1062:   } elsif (@vals) {
                   1063:     $val= \@vals;
                   1064:   } else {
                   1065:     $val = undef;
                   1066:   }
                   1067:
1.14      andrew   1068:   if (%conf) {
1.27      mike     1069:     $conf{_pre} = $val if $val;
1.14      andrew   1070:     return \%conf;
                   1071:   } else {
1.17      andrew   1072:     return $val;
1.14      andrew   1073:   }
                   1074: }
                   1075:
                   1076: #=item _decode_each_line
                   1077:
                   1078: sub _decode_each_line
                   1079: {
                   1080:   my @lines = @_;
                   1081:   my @decoded;
                   1082:   foreach my $line (@lines) {
                   1083:     my $decoded = _decode_lines($line);
                   1084:     push @decoded, $decoded if defined $decoded;
                   1085:   }
                   1086:   return \@decoded;
                   1087: }
                   1088:
                   1089: #=item _decode_sulog
                   1090:
                   1091: sub _decode_sulog
                   1092: {
                   1093:   my @lines = @_;
                   1094:   my @decoded;
                   1095:   my $last_tm;
                   1096:   foreach my $line (@lines) {
                   1097:     my $decoded = _decode_lines($line);
                   1098:
                   1099:     if (defined $decoded) {
                   1100:       if ($decoded->{'tm'}) {
                   1101:         $last_tm = $decoded->{'tm'};
                   1102:         next;
                   1103:       } else {
                   1104:         $decoded->{'tm'} = $last_tm;
                   1105:       }
                   1106:       next unless $last_tm;
                   1107:
                   1108:       push @decoded, $decoded if defined $decoded;
                   1109:     }
                   1110:   }
                   1111:   return \@decoded;
                   1112: }
                   1113:
                   1114: #=item _decode_maclist
                   1115:
                   1116: sub _decode_maclist
                   1117: {
                   1118:        my @lines = @_;
                   1119:        my @decoded;
                   1120:        my $total_entries = 0;
                   1121:        my $current_tm = 0;
                   1122:        foreach my $line (@lines) {
                   1123:                $line =~ s/\r?\n$//;
                   1124:                my ($mac, $loc, $tm) = $line =~ /
                   1125:                        ([0-9a-fA-F ]{17})\s+
                   1126:                        (.*)\s+
                   1127:                        tm\s+
                   1128:                        (\d+)
                   1129:                /x;
                   1130:
                   1131:                if ($mac) {
                   1132:                        $mac =~ s/\s+//g;
                   1133:                        $loc =~ s/^\s+//;
                   1134:                        $loc =~ s/\s+$//;
                   1135:
                   1136:                        my $suid = undef;
                   1137:                        if ($loc =~ /suid\s+=\s+(\d+)/) {
                   1138:                                $suid = $1;
                   1139:                                $loc = undef;
                   1140:                        }
                   1141:
                   1142:                        push @decoded, {
                   1143:                                mac  => $mac,
                   1144:                                loc  => $loc,
                   1145:                                tm   => $tm,
                   1146:                                suid => $suid,
                   1147:                        };
                   1148:                } elsif ($line =~ /(\d+)\s+entries/) {
                   1149:                        $total_entries = $1;
                   1150:                } elsif ($line =~ /current tm = (\d+)\s+sec/) {
                   1151:                        $current_tm = $1
                   1152:                }
                   1153:        }
1.15      andrew   1154:
                   1155:        map { $_->{'cur_tm'} = $current_tm } @decoded;
                   1156:
1.14      andrew   1157:        if (scalar @decoded == $total_entries) {
                   1158:                return \@decoded;
                   1159:        } else {
                   1160:                # XXX we should have a way to set last error, not sure why we don't
1.29    ! andrew   1161:                return;
1.14      andrew   1162:        }
                   1163: }
                   1164:
                   1165: 1;
                   1166: __END__
                   1167:
                   1168: =back
                   1169:
                   1170: =head1 SEE ALSO
                   1171:
1.26      andrew   1172: Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm
1.14      andrew   1173:
                   1174: L<Net::Telnet>
                   1175:
                   1176: =head1 TODO
                   1177:
1.20      andrew   1178: There are still a lot of commands that are not accessed directly.  If
                   1179: you call them (as cmd("command + args") or whatever) and it works,
                   1180: please send me examples that work and I will try to get it incorporated
                   1181: into the next version of the script.
                   1182:
                   1183: I also want to be able to parse the different types of output from
                   1184: commands like su, sudb all and anything else that would be better
                   1185: available as a perl datastructure.
1.14      andrew   1186:
                   1187: =head1 AUTHOR
                   1188:
                   1189: Andrew Fresh E<lt>andrew@rraz.netE<gt>
                   1190:
                   1191: =head1 COPYRIGHT AND LICENSE
                   1192:
1.26      andrew   1193: Copyright (C) 2005,2006,2007 by Andrew Fresh
1.14      andrew   1194:
                   1195: This library is free software; you can redistribute it and/or modify
                   1196: it under the same terms as Perl itself, either Perl version 5.8.7 or,
                   1197: at your option, any later version of Perl 5 you may have available.
                   1198:
                   1199: =cut

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