Annotation of trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm, Revision 1.57
1.40 andrew 1: package Net::Telnet::Trango;
2:
1.57 ! andrew 3: # $RedRiver: Trango.pm,v 1.56 2009/07/08 17:16:41 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.57 ! andrew 46: our $VERSION = '0.03';
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:]])
! 653: \s+
! 654: (\d+)
! 655: \s+
! 656: ([[:xdigit:]\s]+)
! 657: $/ixms
! 658: )
! 659: {
1.40 andrew 660: my %s = (
1.57 ! andrew 661: suid => $1,
! 662: su2su => $2 ? $2 : undef,
! 663: type => $3 == 1 ? 'reg' : $3 == 5 ? 'pri' : $3,
! 664: cir => $4,
! 665: mir => $5,
! 666: mac => $6,
1.40 andrew 667: );
668:
1.57 ! andrew 669: $s{'mac'} =~ s/\s//gxms;
1.40 andrew 670: $s{'mac'} = uc( $s{'mac'} );
671:
672: push @sus, \%s;
673: }
674: }
675:
676: return \@sus;
677: }
678:
679: =pod
680:
681: =head2 B<sudb_add> - Adds an su to the sudb
682:
683: Takes the following paramaters
684:
685: suid : numeric,
686: type : (reg|pr)
687: cir : numeric,
688: mir : numeric,
689: mac : Almost any format, it will be reformatted,
690:
691: and returns true on success or undef otherwise.
692:
693: $t->sudb_add($suid, 'reg', $cir, $mir, $mac);
694:
695: You should save_sudb() after calling this, or your changes will be lost
696: when the AP is rebooted.
697:
698: =cut
699:
700: sub sudb_add {
701: my $self = shift;
702: my $suid = shift;
703: my $type = shift;
704: my $cir = shift;
705: my $mir = shift;
706: my $mac = shift;
707:
708: if ( $suid =~ /\D/ ) {
709: $self->last_error("Invalid suid '$suid'");
710: return;
711: }
712:
713: unless ( lc($type) eq 'reg' || lc($type) eq 'pr' ) {
714: $self->last_error("Invalid type '$type'");
715: return;
716: }
717:
718: if ( $cir =~ /\D/ ) {
719: $self->last_error("Invalid CIR '$cir'");
720: return;
721: }
722:
723: if ( $mir =~ /\D/ ) {
724: $self->last_error("Invalid MIR '$mir'");
725: return;
726: }
727:
728: my $new_mac = $mac;
1.55 andrew 729: $new_mac =~ s/[^0-9A-Fa-f]//g;
1.40 andrew 730: unless ( length $new_mac == 12 ) {
731: $self->last_error("Invalid MAC '$mac'");
732: return;
733: }
734: $new_mac = join $SPACE, $new_mac =~ /../g;
735:
1.57 ! andrew 736: my $string
! 737: = 'sudb add '
! 738: . $suid
! 739: . $SPACE
! 740: . $type
! 741: . $SPACE
! 742: . $cir
! 743: . $SPACE
! 744: . $mir
! 745: . $SPACE
! 746: . $new_mac;
1.40 andrew 747:
748: return $self->cmd( String => $string, expect => $success );
749: }
750:
751: =pod
752:
753: =head2 B<sudb_delete> - removes an su from the sudb
754:
755: Takes either 'all' or the suid of the su to delete
756: and returns true on success or undef otherwise.
757:
758: $t->sudb_delete($suid);
759:
760: You should save_sudb() after calling this, or your changes will be lost
761: when the AP is rebooted.
762:
763: =cut
764:
765: sub sudb_delete {
766: my $self = shift;
767: my $suid = shift;
768:
769: #if (lc($suid) ne 'all' || $suid =~ /\D/) {
770: if ( $suid =~ /\D/ ) {
771: $self->last_error("Invalid suid '$suid'");
772: return;
773: }
774:
775: return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
776: }
777:
778: =pod
779:
780: =head2 B<sudb_modify> - changes the su information in the sudb
781:
782: Takes either the suid of the su to change
783: as well as what you are changing, either "cir, mir or su2su"
784: and returns true on success or undef otherwise.
785:
786: cir and mir also take a value to set the cir/mir to.
787:
788: su2su takes a group id parameter that is in hex.
789:
790: $t->sudb_modify($suid, 'cir', 512);
791:
792: You should save_sudb() after calling this, or your changes will be lost
793: when the AP is rebooted.
794:
795: =cut
796:
797: sub sudb_modify {
798: my $self = shift;
799: my $suid = shift;
800: my $opt = shift;
801: my $value = shift;
802:
803: if ( $suid =~ /\D/ ) {
804: $self->last_error("Invalid suid '$suid'");
805: return;
806: }
807:
808: if ( lc($opt) eq 'cir' or lc($opt) eq 'mir' ) {
809: if ( $value =~ /\D/ ) {
810: $self->last_error("Invalid $opt '$value'");
811: return;
812: }
813: }
814: elsif ( lc($opt) eq 'su2su' ) {
815: if ( $value =~ /[^0-9A-Za-f]/ ) {
816: $self->last_error("Invalid MAC '$value'");
817: return;
818: }
819: }
820: else {
821: $self->last_error("Invalid option '$opt'");
822: return;
823: }
824:
825: my $string = 'sudb modify ' . $suid . $SPACE . $opt . $SPACE . $value;
826:
827: return $self->cmd( String => $string, expect => $success );
828: }
829:
830: =pod
831:
832: =head2 B<enable_tftpd> - enable the TFTP server
833:
834: runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
835:
836: =cut
837:
838: sub enable_tftpd {
839: my $self = shift;
840:
841: my $vals = $self->tftpd( args => 'on' );
842:
843: if ( ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'listen' ) {
844: return $vals;
845: }
846: else {
847: return;
848: }
849: }
850:
851: =pod
852:
853: =head2 B<disable_tftpd> - disable the TFTP server
854:
855: runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
856:
857: =cut
858:
859: sub disable_tftpd {
860: my $self = shift;
861:
862: my $vals = $self->tftpd( args => 'off' );
863:
864: if ( ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled' ) {
865: return $vals;
866: }
867: else {
868: return;
869: }
870: }
871:
872: =pod
873:
874: =head2 B<cmd> - runs a command on the AP.
875:
876: This does most of the work. At the heart, it calls Net::Telnet::cmd()
877: but it also does some special stuff for Trango.
878:
879: Normally returns the last lines from from the command
880:
881: If you are using this, rather than one of the "easy" methods above,
882: you probably want to read through the source of this module to see how
883: some of the other commands are called.
884:
885: In addition to the Net::Telnet::cmd() options, it also accepts these:
886:
887: I<decode>
888: - if this is true, then it will send the output lines to _decode_lines()
889: and then returns the decoded output
890:
891: I<no_prompt>
892: - if this is true, it does not wait for a prompt, so you are not stuck
893: waiting for something that will never happen.
894:
895: I<cmd_disconnects>
896: - if this is true, it then sets logged_in() to false, then it will
897: close() the connection and set is_connected() to false
898:
899: I<expect>
900: - if this is set (usually to 'Success.') it will check for that in the
901: last line of output and if it does not, will return undef because the
902: command probably failed
903:
904: I<args>
905: - a string containing the command line options that are passed to the
906: command
907:
908: $t->cmd( String => 'exit', no_prompt => 1, cmd_disconnects => 1 );
909:
910: =cut
911:
912: sub cmd {
913: my $self = shift;
914:
915: my @valid_net_telnet_opts = qw(
1.57 ! andrew 916: String
! 917: Output
! 918: Cmd_remove_mode
! 919: Errmode
! 920: Input_record_separator
! 921: Ors
! 922: Output_record_separator
! 923: Prompt
! 924: Rs
! 925: Timeout
1.40 andrew 926: );
927:
928: my %cfg;
929: if ( @_ == 1 ) {
930: $cfg{'String'} = shift;
931: }
932: elsif ( @_ > 1 ) {
933: %cfg = @_;
934: }
935:
936: $cfg{'Timeout'} ||= $self->Timeout;
937:
938: unless ( $cfg{'String'} ) {
939: $self->last_error("No command passed");
940: return;
941: }
942:
943: unless ( $self->is_connected ) {
944: $self->last_error("Not connected");
945: return;
946: }
947:
948: unless ( $self->logged_in ) {
949: $self->last_error("Not logged in");
950: return;
951: }
952:
953: my %cmd;
954: foreach (@valid_net_telnet_opts) {
955: if ( exists $cfg{$_} ) {
956: $cmd{$_} = $cfg{$_};
957: }
958: }
959: if ( $cfg{'args'} ) {
960: $cmd{'String'} .= $SPACE . $cfg{'args'};
961: }
962:
1.44 mike 963: #print "Running cmd $cmd{String}\n";
1.40 andrew 964: my @lines;
965: if ( $cfg{'no_prompt'} ) {
966: $self->print( $cmd{'String'} );
967: @lines = $self->lastline;
968: }
969: else {
970: @lines = $self->SUPER::cmd(%cmd);
971: }
972:
973: $self->last_lines( \@lines );
974:
1.57 ! andrew 975: my $last = $self->lastline;
1.52 andrew 976: my $prompt = $self->prompt;
977: $prompt =~ s{^/}{}xms;
978: $prompt =~ s{/[gixms]*$}{}xms;
1.57 ! andrew 979: while ( @lines && $last =~ qr($prompt) ) {
1.52 andrew 980: pop @lines;
981: $last = $lines[-1];
982: }
983: $self->last_error($EMPTY);
984:
1.40 andrew 985: my $vals = 1;
986: if ( $PRIVATE{'Decode'} && $cfg{'decode'} ) {
987: if ( $cfg{'decode'} eq 'each' ) {
988: $vals = _decode_each_line(@lines);
989: }
990: elsif ( $cfg{'decode'} eq 'sulog' ) {
991: $vals = _decode_sulog(@lines);
992: }
993: elsif ( $cfg{'decode'} eq 'maclist' ) {
994: $vals = _decode_maclist(@lines);
1.57 ! andrew 995: if ( !$vals ) {
1.41 andrew 996: $self->last_error("Error decoding maclist");
1.46 andrew 997: }
998: }
999: elsif ( $cfg{'decode'} eq 'linktest' ) {
1000: $vals = _decode_linktest(@lines);
1.57 ! andrew 1001: if ( !$vals ) {
1.46 andrew 1002: $self->last_error("Error decoding linktest");
1.41 andrew 1003: }
1.40 andrew 1004: }
1005: else {
1006: $vals = _decode_lines(@lines);
1007: }
1008: }
1009: $self->last_vals($vals);
1010:
1011: if ( ( not $cfg{'expect'} ) || $last =~ /$cfg{'expect'}$/ ) {
1012: if ( $cfg{'cmd_disconnects'} ) {
1013: $self->logged_in(0);
1014: $self->close;
1015: $self->is_connected(0);
1016: }
1017:
1018: if ( $PRIVATE{'Decode'} && $cfg{'decode'} ) {
1019: return $vals;
1020: }
1021: else {
1022: return \@lines;
1023: }
1024: }
1025: else {
1026: my $err;
1.57 ! andrew 1027: if ( grep {/\[ERR\]/} @lines ) {
1.40 andrew 1028: $err = _decode_lines(@lines);
1.57 ! andrew 1029: }
1.40 andrew 1030:
1.57 ! andrew 1031: if ( ref $err eq 'HASH' && $err->{ERR} ) {
! 1032: $self->last_error( $err->{ERR} );
! 1033: }
! 1034: else {
1.42 andrew 1035: $self->last_error("Error with command ($cmd{'String'}): $last");
1.40 andrew 1036: }
1037: return;
1038: }
1039: }
1040:
1041: #=item _decode_lines
1042:
1043: sub _decode_lines {
1044: my @lines = @_;
1045:
1046: my %conf;
1047:
1048: my $key = $EMPTY;
1049: my $val = undef;
1050: my @vals;
1051: my $in_key = 0;
1052: my $in_val = 1;
1053:
1.57 ! andrew 1054: LINE: while ( my $line = shift @lines ) {
1.52 andrew 1055: next LINE if $line =~ /$success\Z/;
1056: next LINE if $line =~ /^ \*+ \s+ \d+ \s+ \*+ \Z/xms;
1057:
1058: # Special decode for sysinfo on a TrangoLink 45
1.57 ! andrew 1059: if ( $line =~ /^(.* Channel \s+ Table):\s*(.*)\Z/xms ) {
1.52 andrew 1060: my $key = $1;
1061: my $note = $2;
1062:
1063: my %vals;
1.57 ! andrew 1064: while ( $line = shift @lines ) {
! 1065: if ( $line =~ /^\Z/ ) {
1.52 andrew 1066: $conf{$key} = \%vals;
1067: $conf{$key}{note} = $note;
1068: next LINE;
1069: }
1070:
1071: my $decoded = _decode_lines($line);
1072: if ($decoded) {
1.57 ! andrew 1073: %vals = ( %vals, %{$decoded} );
1.52 andrew 1074: }
1075: }
1076: }
1.57 ! andrew 1077:
1.52 andrew 1078: # Another special decode for the TrangoLink
1.57 ! andrew 1079: elsif (
! 1080: $line =~ /^
1.52 andrew 1081: RF \s Band \s \#
1082: (\d+) \s+
1083: \( ([^\)]+) \) \s*
1084: (.*)$
1.57 ! andrew 1085: /xms
! 1086: )
! 1087: {
1.52 andrew 1088: my $num = $1;
1089: my $band = $2;
1090: my $extra = $3;
1091:
1.57 ! andrew 1092: if ( $extra =~ /\[/ ) {
1.52 andrew 1093: my $decoded = _decode_lines($extra);
1094: $conf{'RF Band'}{$num} = $decoded;
1095: }
1096: else {
1097: $conf{'RF Band'}{$num}{$extra} = 1;
1098: }
1099: next LINE;
1100: }
1.40 andrew 1101:
1102: my @chars = split //, $line;
1103:
1104: my $last_key = $EMPTY;
1105: foreach my $c (@chars) {
1106:
1107: if ( $c eq '[' || $c eq "\r" || $c eq "\n" ) {
1108: if ( $c eq '[' ) {
1109: $in_key = 1;
1110: $in_val = 0;
1111: }
1112: else {
1113: $in_key = 0;
1114: $in_val = 1;
1115: }
1116:
1117: if ($key) {
1118: $key =~ s/^\s+//;
1119: $key =~ s/\s+$//;
1120:
1121: if ($val) {
1122: $val =~ s/^\s+//;
1123: $val =~ s/\s+$//;
1124: }
1125:
1126: if ( $key eq 'Checksum' && $last_key ) {
1127:
1128: # Special case for these bastids.
1129: my $new = $last_key;
1130: $new =~ s/\s+\S+$//;
1131: $key = $new . $SPACE . $key;
1132: }
1133:
1134: $conf{$key} = $val;
1135: $last_key = $key;
1136: $key = $EMPTY;
1137: }
1138: elsif ($val) {
1139: push @vals, $val;
1140: }
1141: $val = $EMPTY;
1142:
1143: }
1144: elsif ( $c eq ']' ) {
1145: $in_val = 1;
1146: $in_key = 0;
1147: $c = shift @chars;
1148:
1149: }
1150: elsif ($in_key) {
1151: $key .= $c;
1152:
1153: }
1154: elsif ($in_val) {
1155: $val .= $c;
1156: }
1157: }
1158: }
1159:
1160: unless ($key) {
1161: push @vals, $val;
1162: }
1163:
1164: if ( @vals == 1 ) {
1165: $val = $vals[0];
1166: }
1167: elsif (@vals) {
1168: $val = \@vals;
1169: }
1170: else {
1171: $val = undef;
1172: }
1173:
1174: if (%conf) {
1175: $conf{_pre} = $val if $val;
1176: return \%conf;
1177: }
1178: else {
1179: return $val;
1180: }
1181: }
1182:
1183: #=item _decode_each_line
1184:
1185: sub _decode_each_line {
1186: my @lines = @_;
1187: my @decoded;
1188: foreach my $line (@lines) {
1189: my $decoded = _decode_lines($line);
1190: push @decoded, $decoded if defined $decoded;
1191: }
1192: return \@decoded;
1.45 andrew 1193: }
1194:
1195: #=item _decode_linktest
1196:
1197: sub _decode_linktest {
1198: my @lines = @_;
1199: my %decoded;
1200: foreach my $line (@lines) {
1201:
1.57 ! andrew 1202: if ( $line =~ s/^(\d+) \s+ //xms ) {
1.45 andrew 1203: my $line_id = $1;
1.57 ! andrew 1204: my ( $tm, $rt );
! 1205: if ( $line =~ s/\s+ (\d+ \s+ \w+) \s* $//xms ) {
! 1206: $rt = $1;
! 1207: }
! 1208: if ( $line =~ s/\s+ (\d+ \s+ \w+) \s* $//xms ) {
! 1209: $tm = $1;
! 1210: }
1.49 andrew 1211:
1.57 ! andrew 1212: my $d = _decode_lines( $line . "\n" );
! 1213: $decoded{tests}[$line_id] = $d;
1.49 andrew 1214: $decoded{tests}[$line_id]{'time'} = $tm;
1215: $decoded{tests}[$line_id]{rate} = $rt;
1.45 andrew 1216: }
1217:
1218: else {
1.57 ! andrew 1219: my $d = _decode_lines( $line . "\n" );
1.45 andrew 1220: if ($d) {
1.57 ! andrew 1221: while ( my ( $k, $v ) = each %{$d} ) {
1.45 andrew 1222: $decoded{$k} = $v;
1223: }
1.57 ! andrew 1224: }
1.45 andrew 1225: }
1226:
1227: }
1228: return \%decoded;
1.40 andrew 1229: }
1230:
1231: #=item _decode_sulog
1232:
1233: sub _decode_sulog {
1234: my @lines = @_;
1235: my @decoded;
1236: my $last_tm;
1237: foreach my $line (@lines) {
1238: my $decoded = _decode_lines($line);
1239:
1240: if ( defined $decoded ) {
1241: if ( $decoded->{'tm'} ) {
1242: $last_tm = $decoded->{'tm'};
1243: next;
1244: }
1245: else {
1246: $decoded->{'tm'} = $last_tm;
1247: }
1248: next unless $last_tm;
1249:
1250: push @decoded, $decoded if defined $decoded;
1251: }
1252: }
1253: return \@decoded;
1254: }
1255:
1256: #=item _decode_maclist
1257:
1258: sub _decode_maclist {
1259: my @lines = @_;
1260: my @decoded;
1261: my $total_entries = 0;
1262: my $current_tm = 0;
1263: foreach my $line (@lines) {
1264: $line =~ s/\r?\n$//;
1265: my ( $mac, $loc, $tm ) = $line =~ /
1266: ([0-9a-fA-F ]{17})\s+
1267: (.*)\s+
1268: tm\s+
1269: (\d+)
1270: /x;
1271:
1272: if ($mac) {
1273: $mac =~ s/\s+//g;
1274: $loc =~ s/^\s+//;
1275: $loc =~ s/\s+$//;
1276:
1277: my $suid = undef;
1278: if ( $loc =~ /suid\s+=\s+(\d+)/ ) {
1279: $suid = $1;
1280: $loc = undef;
1281: }
1282:
1283: push @decoded,
1.57 ! andrew 1284: {
1.40 andrew 1285: mac => $mac,
1286: loc => $loc,
1287: tm => $tm,
1288: suid => $suid,
1.57 ! andrew 1289: };
1.40 andrew 1290: }
1291: elsif ( $line =~ /(\d+)\s+entries/ ) {
1292: $total_entries = $1;
1293: }
1294: elsif ( $line =~ /current tm = (\d+)\s+sec/ ) {
1295: $current_tm = $1;
1296: }
1297: }
1298:
1299: map { $_->{'cur_tm'} = $current_tm } @decoded;
1300:
1301: if ( scalar @decoded == $total_entries ) {
1302: return \@decoded;
1303: }
1304: else {
1305: return;
1306: }
1307: }
1308:
1309: 1; # End of Net::Telnet::Trango
1310: __END__
1311:
1312: =head1 SEE ALSO
1313:
1314: Trango Documentation -
1315: L<http://www.trangobroadband.com/support/product_docs.htm>
1316:
1317: L<Net::Telnet>
1318:
1319: =head1 TODO
1320:
1321: There are still a lot of commands that are not accessed directly. If
1322: you call them (as cmd("command + args") or whatever) and it works,
1323: please send me examples that work and I will try to get it incorporated
1324: into the next version of the script.
1325:
1326: I also want to be able to parse the different types of output from
1327: commands like su, sudb all and anything else that would be better
1328: available as a perl datastructure.
1329:
1330: =head1 AUTHOR
1331:
1332: Andrew Fresh E<lt>andrew@rraz.netE<gt>
1333:
1334: =head1 SUPPORT
1335:
1336: You can find documentation for this module with the perldoc command.
1337:
1338: perldoc Net::Telnet::Trango
1339:
1340: You can also look for information at:
1341:
1342: =over 4
1343:
1344: =item * AnnoCPAN: Annotated CPAN documentation
1345:
1346: L<http://annocpan.org/dist/Net-Telnet-Trango>
1347:
1348: =item * CPAN Ratings
1349:
1350: L<http://cpanratings.perl.org/d/Net-Telnet-Trango>
1351:
1352: =item * RT: CPAN's request tracker
1353:
1354: L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Telnet-Trango>
1355:
1356: =item * Search CPAN
1357:
1358: L<http://search.cpan.org/dist/Net-Telnet-Trango>
1359:
1360: =back
1361:
1362: =head1 COPYRIGHT AND LICENSE
1363:
1364: Copyright (C) 2005,2006,2007 by Andrew Fresh
1365:
1366: This program is free software; you can redistribute it and/or modify it
1367: under the same terms as Perl itself.
1368:
1369: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>