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