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