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

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

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