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

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

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