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