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

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

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