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

1.14      andrew      1: package Net::Telnet::Trango;
1.18    ! andrew      2: # $RedRiver: Trango.pm,v 1.17 2006/09/12 01:17:46 andrew Exp $
1.14      andrew      3: use strict;
                      4: use warnings;
                      5: use base 'Net::Telnet';
                      6:
                      7: =pod
                      8:
                      9: =head1 NAME
                     10:
                     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:
                    222: Most of these are just shortcuts to C<cmd(String =E<gt> METHOD)>, as such they accept the same options as C<cmd()>.  Specifically they take a named paramater "args", for example:
                    223: C<tftpd(args =E<gt> 'on')> would enable tftpd
                    224:
                    225: =over
                    226:
                    227: =item tftpd
                    228:
                    229: Returns a hash ref of the decoded output from the command.
                    230:
                    231: Also see enable_tftpd() and disable_tftpd() as those check for correct output
                    232:
                    233: =item ver
                    234:
                    235: Returns a hash ref of the decoded output from the command.
                    236:
                    237: =item sysinfo
                    238:
                    239: Returns a hash ref of the decoded output from the command.
                    240:
                    241: =item exit
                    242:
                    243: exits the command session with the trango and closes the connection
                    244:
                    245: =item reboot
                    246:
                    247: reboots the trango and closes the connection
                    248:
1.17      andrew    249: =item remarks
                    250:
1.18    ! andrew    251: Takes an optional argument, which sets the remarks.
        !           252: If there is no argument, returns the current remarks.
1.17      andrew    253:
1.14      andrew    254: =item sulog
                    255:
                    256: returns an array ref of hashes containing each log line.
                    257:
                    258: =item save_sudb
                    259:
                    260: returns true on success, undef on failure
                    261:
                    262: =item syslog
                    263:
                    264: returns the output from the syslog command
                    265:
                    266: =item pipe
                    267:
                    268: returns the output from the pipe command
                    269:
                    270: =item maclist
                    271:
                    272: returns the output from the maclist command
                    273:
                    274: =item maclist_reset
                    275:
                    276: resets the maclist.  No useful output.
                    277:
                    278: =item eth_list
                    279:
                    280: returns the output from the eth list command
                    281:
1.17      andrew    282: =item su_info
                    283:
                    284: You need to pass in args => <suid> and it will return the info for that suid.
                    285:
                    286: =item save_ss
                    287:
                    288: saves the config.  Returns 1 on success, undef on failure.
                    289:
1.14      andrew    290: =cut
                    291:
                    292:
                    293: my $success = 'Success.';
                    294: my %COMMANDS = (
                    295:   tftpd       => { decode => 'all',   expect => $success },
                    296:   ver         => { decode => 'all' },
                    297:   sysinfo     => { decode => 'all',   expect => $success },
                    298:   updateflash => { decode => 'all',   expect => $success },
                    299:   sulog       => { decode => 'sulog', expect => $success },
                    300:   'exit'      => { no_prompt => 1, cmd_disconnects => 1 },
                    301:   reboot      => { no_prompt => 1, cmd_disconnects => 1 },
1.17      andrew    302:   remarks     => { decode => 'all', expect => $success },
1.14      andrew    303:   save_sudb   => { String => 'save sudb', expect => $success },
                    304:   syslog      => { expect => $success },
                    305:   'pipe'      => { }, # XXX needs a special decode
                    306:   maclist     => { decode => 'maclist' },
                    307:   maclist_reset => { String => 'maclist reset', expect => 'done' },
                    308:   eth_link    => { String => 'eth link', expect => $success },
1.17      andrew    309:   su_info     => { String => 'su info', decode => 'all', expect => $success },
                    310:   save_ss     => { String => 'save ss', expect => $success },
1.14      andrew    311:   # eth r, w and reset???
                    312:   #su password???
                    313:   #_bootloader
                    314:   #temp
                    315:   #heater
                    316: );
                    317:
                    318: my %ALIASES = (
                    319:   bye     => 'exit',
                    320:   restart => 'reboot',
                    321: );
                    322:
                    323: my %ACCESS = map { $_ => 1 } qw(
                    324:   firmware_version
                    325:   host_type
                    326:   Host
                    327:   is_connected
                    328:   logged_in
                    329:   login_banner
                    330:   Timeout
                    331:   last_lines
                    332:   last_vals
                    333: );
                    334:
                    335: sub AUTOLOAD
                    336: {
                    337:   my $self = shift;
                    338:
                    339:   my ($method) = (our $AUTOLOAD) =~ /^.*::(\w+)$/
                    340:     or die "Weird: $AUTOLOAD";
                    341:
                    342:   if (exists $ALIASES{$method}) {
                    343:     $method = $ALIASES{$method};
                    344:     return $self->$method(@_);
                    345:   }
                    346:
                    347:   if (exists $COMMANDS{$method}) {
                    348:     $COMMANDS{$method}{'String'} ||= $method;
1.17      andrew    349:     $COMMANDS{$method}{'args'} .= ' ' . shift if (@_ == 1);
1.14      andrew    350:     return $self->cmd(%{ $COMMANDS{$method} }, @_);
                    351:   }
                    352:
                    353:   if (exists $ACCESS{$method}) {
                    354:     my $prev = $PRIVATE{$method};
                    355:     ($PRIVATE{$method}) = @_ if @_;
                    356:     return $prev;
                    357:   }
                    358:
                    359:   $method = "SUPER::$method";
                    360:   return $self->$method(@_);
                    361: }
                    362:
                    363: =pod
                    364:
                    365: =item open
                    366:
                    367: 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()
                    368:
                    369: =cut
                    370:
                    371: sub open
                    372: {
                    373:   my $self = shift;
                    374:
                    375:   unless ( $self->SUPER::open(@_) ) {
                    376:     #$! = "Couldn't connect to " . $self->Host . ":  $!";
                    377:     return undef;
                    378:   }
                    379:
                    380:   ## Get to login prompt
                    381:   unless ($self->waitfor(
                    382:       -match => '/password: ?$/i',
                    383:       -errmode => "return",
                    384:     ) ) {
                    385:     #$! = "problem connecting to host (" . $self->Host . "): " .
                    386:     #    $self->lastline;
                    387:     return undef;
                    388:   }
                    389:
                    390:   $self->parse_login_banner($self->lastline);
                    391:
                    392:   $self->is_connected(1);
                    393:
                    394:   return $self->is_connected;
                    395: }
                    396:
                    397: =pod
                    398:
                    399: =item login
                    400:
                    401: Calls open() if not already connected, then sends the password and sets logged_in() if successful
                    402:
                    403: =cut
                    404:
                    405: sub login
                    406: {
                    407:   my $self = shift;
                    408:
                    409:   unless ($self->is_connected) {
                    410:     $self->open or return undef;
                    411:   }
                    412:
                    413:   my $password = shift;
                    414:
                    415:   $self->print($password);
                    416:   unless ($self->waitfor(
                    417:     -match => $self->prompt,
                    418:     -errmode => "return",
                    419:   ) ) {
                    420:     #$! = "login ($self->Host) failed: " . $self->lastline;
                    421:     return undef;
                    422:   }
                    423:
                    424:   $self->logged_in(1);
                    425:
                    426:   return $self->logged_in;
                    427: }
                    428:
                    429: =pod
                    430:
                    431: =item parse_login_banner
                    432:
                    433: 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()
                    434:
                    435: =cut
                    436:
                    437: sub parse_login_banner
                    438: {
                    439:   my $self = shift;
                    440:
                    441:   if (@_) {
                    442:     $self->login_banner(@_);
                    443:   }
                    444:
                    445:   my $banner = $self->login_banner;
                    446:
                    447:   my ($type, $ver) = $banner =~
                    448:     /Welcome to Trango Broadband Wireless (\S+)[\s-]+(.+)$/i;
                    449:
                    450:   $self->login_banner($banner);
                    451:   $self->host_type($type);
                    452:   $self->firmware_version($ver);
                    453:
                    454:   return 1;
                    455: }
                    456:
                    457: =pod
                    458:
                    459: =item su_password
                    460:
                    461: C<su_password('all'|suid, 'new_password')>
                    462:
                    463: =cut
                    464:
                    465: sub su_password
                    466: {
                    467:   my $self     = shift;
                    468:   my $su       = shift || '!';
                    469:   my $new_pass = shift || '';
                    470:
                    471:   unless (defined $su) {
                    472:     warn "No su passed!"
                    473:     #return undef;
                    474:   }
                    475:
                    476:   unless (defined $new_pass) {
                    477:     warn "No new password!"
                    478:     #return undef;
                    479:   }
                    480:
                    481:   return $self->cmd(String => 'su password ' .
                    482:                      $su . ' ' .
                    483:                      $new_pass . ' ' .
                    484:                      $new_pass,
                    485:                      expect => $success,
                    486:                     );
                    487: }
                    488:
                    489: =pod
                    490:
                    491: =item su_ipconfig
                    492:
                    493: C<su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )>
                    494:
                    495: =cut
                    496:
                    497: sub su_ipconfig
                    498: {
                    499:        my $self        = shift;
                    500:
                    501:        my $suid        = shift;
                    502:        my $new_ip      = shift;
                    503:        my $new_subnet  = shift;
                    504:        my $new_gateway = shift;
                    505:
                    506:        return undef unless $suid =~ /^\d+$/;
                    507:        return undef unless $new_ip;
                    508:        return undef unless $new_subnet;
                    509:        return undef unless $new_gateway;
                    510:
                    511:        # su ipconfig <suid> <new ip> <new subnet> <new gateway>
                    512:        return $self->cmd(String => 'su ipconfig ' .
                    513:                      $suid       . ' ' .
                    514:                      $new_ip     . ' ' .
                    515:                      $new_subnet . ' ' .
                    516:                      $new_gateway,
                    517:                      expect => $success,
                    518:                     );
                    519: }
                    520:
                    521: =pod
                    522:
                    523: =item sudb_view
                    524:
                    525: returns a reference to an array of hashes each containing:
                    526:
                    527:   suid
                    528:   type
                    529:   cir
                    530:   mir
                    531:   mac
                    532:
                    533: =cut
                    534:
                    535: sub sudb_view
                    536: {
                    537:   my $self = shift;
                    538:
                    539:   my @lines = $self->cmd( String => 'sudb view', expect => $success );
                    540:
                    541:   return undef unless @lines;
                    542:
                    543:   unless ($PRIVATE{'Decode'}) {
                    544:     return @lines;
                    545:   }
                    546:
                    547:   my @sus;
                    548:   foreach (@lines) {
                    549:     next unless $_;
                    550:     if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
                    551:       my %s = (
                    552:         suid => $1,
                    553:         type => $2,
                    554:         cir  => $3,
                    555:         mir  => $4,
                    556:         mac  => $5,
                    557:       );
                    558:
                    559:          $s{'mac'} =~ s/\s//g;
                    560:          $s{'mac'} = uc($s{'mac'});
                    561:
                    562:       push @sus, \%s;
                    563:     }
                    564:   }
                    565:
                    566:   return \@sus;
                    567: }
                    568:
                    569: =pod
                    570:
                    571: =item sudb_add
                    572:
                    573: Takes the following paramaters
                    574:
                    575:        suid : numeric,
                    576:        type : (reg|pr)
                    577:        cir  : numeric,
                    578:        mir  : numeric,
                    579:        mac  : Almost any format, it will be reformatted,
                    580:
                    581: and returns true on success or undef otherwise.
                    582:
                    583: You should save_sudb() after calling this, or your changes  will be lost
                    584: when the AP is rebooted.
                    585:
                    586: =cut
                    587:
                    588: sub sudb_add
                    589: {
                    590:        my $self = shift;
                    591:        my $suid = shift;
                    592:        my $type = shift;
                    593:        my $cir  = shift;
                    594:        my $mir  = shift;
                    595:        my $mac  = shift;
                    596:
                    597:        if ($suid =~ /\D/) {
                    598:                return undef;
                    599:        }
                    600:
                    601:        unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
                    602:                warn "Invalid type '$type'!";
                    603:                return undef;
                    604:        }
                    605:
                    606:        if ($cir =~ /\D/) {
                    607:                warn "Invalid CIR '$cir'!";
                    608:                return undef;
                    609:        }
                    610:
                    611:        if ($mir =~ /\D/) {
                    612:                warn "Invalid MIR '$mir'!";
                    613:                return undef;
                    614:        }
                    615:
                    616:        my $new_mac = $mac;
                    617:        $new_mac =~ s/[^0-9A-Fa-f]//;
                    618:        unless (length $new_mac == 12) {
                    619:                warn "Invalid MAC '$mac'!";
                    620:                return undef;
                    621:        }
                    622:        $new_mac = join ' ', $new_mac =~ /../g;
                    623:
                    624:        my $string = 'sudb add ' .
                    625:                $suid . ' ' .
                    626:                $type . ' ' .
                    627:                $cir  . ' ' .
                    628:                $mir  . ' ' .
                    629:                $new_mac;
                    630:
                    631:
                    632:        return $self->cmd( String => $string, expect => $success );
                    633: }
                    634:
                    635: =pod
                    636:
                    637: =item sudb_delete
                    638:
                    639: Takes either 'all' or the  suid of the su to delete
                    640: and returns true on success or undef otherwise.
                    641:
                    642: You should save_sudb() after calling this, or your changes  will be lost
                    643: when the AP is rebooted.
                    644:
                    645: =cut
                    646:
                    647: sub sudb_delete
                    648: {
                    649:        my $self = shift;
                    650:        my $suid = shift;
                    651:
                    652:        if (lc($suid) ne 'all' || $suid =~ /\D/) {
                    653:                return undef;
                    654:        }
                    655:
                    656:        return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
                    657: }
                    658:
                    659: =pod
                    660:
                    661: =item sudb_modify
                    662:
                    663: Takes either the  suid of the su to delete
                    664: as well as what you are changing, either "cir, mir or su2su"
                    665: and returns true on success or undef otherwise.
                    666:
                    667: cir and mir also take a value to set the cir/mir to.
                    668:
                    669: su2su takes a group id parameter that is in hex.
                    670:
                    671: You should save_sudb() after calling this, or your changes  will be lost
                    672: when the AP is rebooted.
                    673:
                    674: =cut
                    675:
                    676: sub sudb_modify
                    677: {
                    678:        my $self  = shift;
                    679:        my $suid  = shift;
                    680:        my $opt   = shift;
                    681:        my $value = shift;
                    682:
                    683:        if ($suid =~ /\D/) {
                    684:                return undef;
                    685:        }
                    686:
                    687:        if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
                    688:                if ($value =~ /\D/) {
                    689:                        return undef;
                    690:                }
                    691:        } elsif (lc($opt) eq 'su2su') {
                    692:                if ($value =~ /[^0-9A-Za-f]/) {
                    693:                        return undef;
                    694:                }
                    695:        } else {
                    696:                return undef;
                    697:        }
                    698:
                    699:        my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
                    700:
                    701:        return $self->cmd( String => $string, expect => $success );
                    702: }
                    703:
                    704: =pod
                    705:
                    706: =item enable_tftpd
                    707:
                    708: runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
                    709:
                    710: =cut
                    711:
                    712: sub enable_tftpd
                    713: {
                    714:   my $self = shift;
                    715:
                    716:   my $vals = $self->tftpd( args => 'on' );
                    717:
                    718:   if ($vals->{'Tftpd'} eq 'listen') {
                    719:     return $vals;
                    720:   } else {
                    721:     return undef;
                    722:   }
                    723: }
                    724:
                    725: =pod
                    726:
                    727: =item disable_tftpd
                    728:
                    729: runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
                    730:
                    731: =cut
                    732:
                    733: sub disable_tftpd
                    734: {
                    735:   my $self = shift;
                    736:
                    737:   my $vals = $self->tftpd( args => 'off' );
                    738:
                    739:   if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
                    740:     return $vals;
                    741:   } else {
                    742:     return undef;
                    743:   }
                    744: }
                    745:
                    746: =pod
                    747:
                    748: =item cmd
                    749:
                    750: This does most of the work.  At the heart, it calls Net::Telnet::cmd() but it also does some special stuff for Trango.
                    751:
                    752: Normally returns the last lines from from the command
                    753:
                    754: Also accepts these options:
                    755:
                    756: I<decode>
                    757: - if this is true, then it will send the output lines to _decode_lines() and then returns the decoded output
                    758:
                    759: I<cmd_disconnects>
                    760: - if this is true, it then sets logged_in() to false, then it will close() the connection and then sets is_connected() to false
                    761:
                    762: I<expect>
                    763: - 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
                    764:
                    765: I<args>
                    766: - a string containing the command line options that are passed to the command
                    767:
                    768: =cut
                    769:
                    770: sub cmd
                    771: {
                    772:   my $self = shift;
                    773:
                    774:   my @valid_net_telnet_opts = qw(
                    775:     String
                    776:     Output
                    777:     Cmd_remove_mode
                    778:     Errmode
                    779:     Input_record_separator
                    780:     Ors
                    781:     Output_record_separator
                    782:     Prompt
                    783:     Rs
                    784:     Timeout
                    785:   );
                    786:
                    787:   my %cfg;
                    788:   if (@_ == 1) {
                    789:     $cfg{'String'} = shift;
                    790:   } elsif (@_ > 1) {
                    791:     %cfg = @_;
                    792:   }
                    793:
                    794:   $cfg{'Timeout'} ||= $self->Timeout;
                    795:
                    796:   unless ($cfg{'String'}) {
                    797:     #$! = "No command passed";
                    798:     #warn "No command passed\n";
                    799:     return undef;
                    800:   }
                    801:
                    802:   unless ($self->is_connected) {
                    803:     #$! = "Not connected";
                    804:     #warn "Not connected\n";
                    805:     return undef;
                    806:   }
                    807:
                    808:   unless ($self->logged_in) {
                    809:     #$! = "Not logged in";
                    810:     #warn "Not logged in\n";
                    811:     return undef;
                    812:   }
                    813:
                    814:
                    815:   my %cmd;
                    816:   foreach (@valid_net_telnet_opts) {
                    817:     if (exists $cfg{$_}) {
                    818:       $cmd{$_} = $cfg{$_};
                    819:     }
                    820:   }
                    821:   if ($cfg{'args'}) {
                    822:     $cmd{'String'} .= ' ' . $cfg{'args'};
                    823:   }
                    824:   my @lines;
                    825:   unless ($cfg{'no_prompt'}) {
                    826:     @lines = $self->SUPER::cmd(%cmd);
                    827:   } else {
                    828:     $self->print($cmd{'String'});
                    829:     @lines = $self->lastline;
                    830:   }
                    831:
                    832:   $self->last_lines(\@lines);
                    833:
                    834:   my $vals = 1;
                    835:   if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
                    836:     if ($cfg{'decode'} eq 'each') {
                    837:       $vals = _decode_each_line(@lines);
                    838:     } elsif ($cfg{'decode'} eq 'sulog') {
                    839:       $vals = _decode_sulog(@lines);
                    840:     } elsif ($cfg{'decode'} eq 'maclist') {
                    841:       $vals = _decode_maclist(@lines);
                    842:     } else {
                    843:       $vals = _decode_lines(@lines);
                    844:     }
                    845:   }
                    846:
                    847:   $self->last_vals($vals);
                    848:
                    849:
                    850:   my $last = $self->lastline;
                    851:
                    852:   if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
                    853:     if ($cfg{'cmd_disconnects'}) {
                    854:       $self->logged_in(0);
                    855:       $self->close;
                    856:       $self->is_connected(0);
                    857:     }
                    858:
                    859:     if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
                    860:       return $vals;
                    861:     } else {
                    862:       return @lines;
                    863:     }
                    864:   } else {
                    865:     #$! = "Error with command ($cfg{'string'}): $last";
                    866:     return undef;
                    867:   }
                    868: }
                    869:
                    870: #=item _decode_lines
                    871:
                    872: sub _decode_lines
                    873: {
                    874:   my @lines = @_;
                    875:
                    876:   my %conf;
                    877:
                    878:   my $key = '';
1.17      andrew    879:   my $val = undef;
1.14      andrew    880:   my $in_key = 0;
1.17      andrew    881:   my $in_val = 1;
1.14      andrew    882:
                    883:   foreach my $line (@lines) {
                    884:     next if $line =~ /$success$/;
                    885:
                    886:     my @chars = split //, $line;
                    887:
                    888:     my $last_key = '';
                    889:     foreach my $c (@chars) {
                    890:
                    891:       if ($c eq '[' || $c eq "\r" || $c eq "\n") {
                    892:         if ($c eq '[') {
                    893:           $in_key = 1;
                    894:           $in_val = 0;
                    895:         } else {
                    896:           $in_key = 0;
1.17      andrew    897:           $in_val = 1;
1.14      andrew    898:         }
                    899:
                    900:         if ($key) {
                    901:           $key =~ s/^\s+//;
                    902:           $key =~ s/\s+$//;
                    903:
1.17      andrew    904:           if (defined $val) {
                    905:             $val =~ s/^\s+//;
                    906:             $val =~ s/\s+$//;
                    907:           }
1.14      andrew    908:
                    909:           if ($key eq 'Checksum' && $last_key) {
                    910:             # Special case for these bastids.
                    911:             my $new = $last_key;
                    912:             $new =~ s/\s+\S+$//;
                    913:             $key = $new . " " . $key;
                    914:           }
                    915:
                    916:           $last_key = $key;
                    917:           $conf{$key} = $val;
                    918:           $key = '';
                    919:           $val = '';
                    920:         }
                    921:
                    922:       } elsif ($c eq ']') {
                    923:         $in_val = 1;
                    924:         $in_key = 0;
                    925:         $c = shift @chars;
                    926:
                    927:       } elsif ($in_key) {
                    928:         $key .= $c;
                    929:
                    930:       } elsif ($in_val) {
                    931:         $val .= $c;
                    932:       }
                    933:     }
                    934:   }
                    935:
                    936:   if (%conf) {
                    937:     return \%conf;
                    938:   } else {
1.17      andrew    939:     return $val;
1.14      andrew    940:   }
                    941: }
                    942:
                    943: #=item _decode_each_line
                    944:
                    945: sub _decode_each_line
                    946: {
                    947:   my @lines = @_;
                    948:   my @decoded;
                    949:   foreach my $line (@lines) {
                    950:     my $decoded = _decode_lines($line);
                    951:     push @decoded, $decoded if defined $decoded;
                    952:   }
                    953:   return \@decoded;
                    954: }
                    955:
                    956: #=item _decode_sulog
                    957:
                    958: sub _decode_sulog
                    959: {
                    960:   my @lines = @_;
                    961:   my @decoded;
                    962:   my $last_tm;
                    963:   foreach my $line (@lines) {
                    964:     my $decoded = _decode_lines($line);
                    965:
                    966:     if (defined $decoded) {
                    967:       if ($decoded->{'tm'}) {
                    968:         $last_tm = $decoded->{'tm'};
                    969:         next;
                    970:       } else {
                    971:         $decoded->{'tm'} = $last_tm;
                    972:       }
                    973:       next unless $last_tm;
                    974:
                    975:       push @decoded, $decoded if defined $decoded;
                    976:     }
                    977:   }
                    978:   return \@decoded;
                    979: }
                    980:
                    981: #=item _decode_maclist
                    982:
                    983: sub _decode_maclist
                    984: {
                    985:        my @lines = @_;
                    986:        my @decoded;
                    987:        my $total_entries = 0;
                    988:        my $current_tm = 0;
                    989:        foreach my $line (@lines) {
                    990:                $line =~ s/\r?\n$//;
                    991:                my ($mac, $loc, $tm) = $line =~ /
                    992:                        ([0-9a-fA-F ]{17})\s+
                    993:                        (.*)\s+
                    994:                        tm\s+
                    995:                        (\d+)
                    996:                /x;
                    997:
                    998:                if ($mac) {
                    999:                        $mac =~ s/\s+//g;
                   1000:                        $loc =~ s/^\s+//;
                   1001:                        $loc =~ s/\s+$//;
                   1002:
                   1003:                        my $suid = undef;
                   1004:                        if ($loc =~ /suid\s+=\s+(\d+)/) {
                   1005:                                $suid = $1;
                   1006:                                $loc = undef;
                   1007:                        }
                   1008:
                   1009:                        push @decoded, {
                   1010:                                mac  => $mac,
                   1011:                                loc  => $loc,
                   1012:                                tm   => $tm,
                   1013:                                suid => $suid,
                   1014:                        };
                   1015:                } elsif ($line =~ /(\d+)\s+entries/) {
                   1016:                        $total_entries = $1;
                   1017:                } elsif ($line =~ /current tm = (\d+)\s+sec/) {
                   1018:                        $current_tm = $1
                   1019:                }
                   1020:        }
1.15      andrew   1021:
                   1022:        map { $_->{'cur_tm'} = $current_tm } @decoded;
                   1023:
1.14      andrew   1024:        if (scalar @decoded == $total_entries) {
                   1025:                return \@decoded;
                   1026:        } else {
                   1027:                # XXX we should have a way to set last error, not sure why we don't
                   1028:                return undef;
                   1029:        }
                   1030: }
                   1031:
                   1032: 1;
                   1033: __END__
                   1034:
                   1035: =back
                   1036:
                   1037: =head1 SEE ALSO
                   1038:
                   1039: Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm
                   1040:
                   1041: L<Net::Telnet>
                   1042:
                   1043: =head1 TODO
                   1044:
                   1045: 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.
                   1046:
                   1047: 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.
                   1048:
                   1049: =head1 AUTHOR
                   1050:
                   1051: Andrew Fresh E<lt>andrew@rraz.netE<gt>
                   1052:
                   1053: =head1 COPYRIGHT AND LICENSE
                   1054:
                   1055: Copyright (C) 2005 by Andrew Fresh
                   1056:
                   1057: This library is free software; you can redistribute it and/or modify
                   1058: it under the same terms as Perl itself, either Perl version 5.8.7 or,
                   1059: at your option, any later version of Perl 5 you may have available.
                   1060:
                   1061:
                   1062: =cut

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