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

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

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