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

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

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