Annotation of trango/Net-Telnet-Trango/lib/Net/Telnet/Trango.pm, Revision 1.28
1.14 andrew 1: package Net::Telnet::Trango;
1.28 ! mike 2: # $RedRiver: Trango.pm,v 1.27 2007/01/31 18:53:29 mike 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:
1.28 ! mike 509: my ($type, $sep1, $subtype, $sep2, $ver) = $banner =~
! 510: /Welcome to Trango Broadband Wireless (\S+)([\s-]+)(\S+)([\s-]+)(.+)$/i;
! 511:
! 512: $type .= $sep1 . $subtype;
! 513: $ver = $subtype . $sep2 . $ver;
1.14 andrew 514:
515: $self->login_banner($banner);
516: $self->host_type($type);
517: $self->firmware_version($ver);
518:
519: return 1;
520: }
521:
522: =pod
523:
1.26 andrew 524: =item B<su_password>
1.14 andrew 525:
1.26 andrew 526: - Set the password on SUs connected to the AP.
527:
528: su_password('new_password'[, 'suid']) If no suid is specified,
1.23 mike 529: the default is "all".
1.14 andrew 530:
1.26 andrew 531: $t->su_password('good_pass', 5);
532:
1.14 andrew 533: =cut
534:
535: sub su_password
536: {
537: my $self = shift;
538: my $new_pass = shift || '';
1.23 mike 539: my $su = shift || 'all';
1.14 andrew 540:
541: unless (defined $new_pass) {
1.25 andrew 542: $self->last_error("No new password");
1.14 andrew 543: #return undef;
544: }
545:
546: return $self->cmd(String => 'su password ' .
547: $su . ' ' .
548: $new_pass . ' ' .
549: $new_pass,
550: expect => $success,
551: );
552: }
553:
554: =pod
555:
1.26 andrew 556: =item B<su_ipconfig>
557:
558: - Change IP configuration on SUs connected to
559: the AP.
1.14 andrew 560:
1.26 andrew 561: su_ipconfig( 'suid', 'new_ip', 'new_subnet', 'new_gateway' )
562:
563: $t->su_ipconfig( 5, '10.0.1.5', '255.255.255.0', '10.0.1.1' );
1.14 andrew 564:
565: =cut
566:
567: sub su_ipconfig
568: {
569: my $self = shift;
570:
571: my $suid = shift;
572: my $new_ip = shift;
573: my $new_subnet = shift;
574: my $new_gateway = shift;
575:
1.25 andrew 576: if ($suid =~ /\D/) {
577: $self->last_error("Invalid suid '$suid'");
578: return undef;
579: }
580: unless ($new_ip) {
581: $self->last_error("no new_ip passed");
582: return undef;
583: }
584: unless ($new_subnet) {
585: $self->last_error("no new_subnet passed");
586: return undef;
587: }
588: unless ($new_gateway) {
589: $self->last_error("no new_gateway passed");
590: return undef;
591: }
1.14 andrew 592:
593: # su ipconfig <suid> <new ip> <new subnet> <new gateway>
594: return $self->cmd(String => 'su ipconfig ' .
595: $suid . ' ' .
596: $new_ip . ' ' .
597: $new_subnet . ' ' .
598: $new_gateway,
599: expect => $success,
600: );
601: }
602:
603: =pod
604:
1.26 andrew 605: =item B<sudb_view>
1.14 andrew 606:
1.26 andrew 607: - Returns the output from the sudb view command
1.14 andrew 608:
1.26 andrew 609: returns a reference to an array of hashes each containing these keys
610: 'suid', 'type', 'cir', 'mir' and 'mac'
1.14 andrew 611:
612: =cut
613:
614: sub sudb_view
615: {
616: my $self = shift;
617:
618: my @lines = $self->cmd( String => 'sudb view', expect => $success );
619:
620: return undef unless @lines;
621:
622: unless ($PRIVATE{'Decode'}) {
1.27 mike 623: return \@lines;
1.14 andrew 624: }
625:
626: my @sus;
627: foreach (@lines) {
628: next unless $_;
629: if (/^\[(\d+)\]\s+(\d+)\s+(\d+)\s+(\d+)\s+([0-9A-Fa-f\s]+)$/) {
630: my %s = (
631: suid => $1,
632: type => $2,
633: cir => $3,
634: mir => $4,
635: mac => $5,
636: );
637:
638: $s{'mac'} =~ s/\s//g;
639: $s{'mac'} = uc($s{'mac'});
640:
641: push @sus, \%s;
642: }
643: }
644:
645: return \@sus;
646: }
647:
648: =pod
649:
1.26 andrew 650: =item B<sudb_add>
1.14 andrew 651:
652: Takes the following paramaters
653:
654: suid : numeric,
655: type : (reg|pr)
656: cir : numeric,
657: mir : numeric,
658: mac : Almost any format, it will be reformatted,
659:
660: and returns true on success or undef otherwise.
661:
1.26 andrew 662: $t->sudb_add($suid, 'reg', $cir, $mir, $mac);
663:
1.14 andrew 664: You should save_sudb() after calling this, or your changes will be lost
665: when the AP is rebooted.
666:
667: =cut
668:
669: sub sudb_add
670: {
671: my $self = shift;
672: my $suid = shift;
673: my $type = shift;
674: my $cir = shift;
675: my $mir = shift;
676: my $mac = shift;
677:
678: if ($suid =~ /\D/) {
1.25 andrew 679: $self->last_error("Invalid suid '$suid'");
1.14 andrew 680: return undef;
681: }
682:
683: unless (lc($type) eq 'reg' || lc($type) eq 'pr') {
1.25 andrew 684: $self->last_error("Invalid type '$type'");
1.14 andrew 685: return undef;
686: }
687:
688: if ($cir =~ /\D/) {
1.25 andrew 689: $self->last_error("Invalid CIR '$cir'");
1.14 andrew 690: return undef;
691: }
692:
693: if ($mir =~ /\D/) {
1.25 andrew 694: $self->last_error("Invalid MIR '$mir'");
1.14 andrew 695: return undef;
696: }
697:
698: my $new_mac = $mac;
699: $new_mac =~ s/[^0-9A-Fa-f]//;
700: unless (length $new_mac == 12) {
1.25 andrew 701: $self->last_error("Invalid MAC '$mac'");
1.14 andrew 702: return undef;
703: }
704: $new_mac = join ' ', $new_mac =~ /../g;
705:
706: my $string = 'sudb add ' .
707: $suid . ' ' .
708: $type . ' ' .
709: $cir . ' ' .
710: $mir . ' ' .
711: $new_mac;
712:
713:
714: return $self->cmd( String => $string, expect => $success );
715: }
716:
717: =pod
718:
1.26 andrew 719: =item B<sudb_delete>
1.14 andrew 720:
721: Takes either 'all' or the suid of the su to delete
722: and returns true on success or undef otherwise.
723:
1.26 andrew 724: $t->sudb_delete($suid);
725:
1.14 andrew 726: You should save_sudb() after calling this, or your changes will be lost
727: when the AP is rebooted.
728:
729: =cut
730:
731: sub sudb_delete
732: {
733: my $self = shift;
734: my $suid = shift;
735:
1.25 andrew 736: #if (lc($suid) ne 'all' || $suid =~ /\D/) {
737: if ($suid =~ /\D/) {
738: $self->last_error("Invalid suid '$suid'");
1.14 andrew 739: return undef;
740: }
741:
742: return $self->cmd( String => 'sudb delete ' . $suid, expect => $success );
743: }
744:
745: =pod
746:
1.26 andrew 747: =item B<sudb_modify>
1.14 andrew 748:
1.26 andrew 749: Takes either the suid of the su to change
1.14 andrew 750: as well as what you are changing, either "cir, mir or su2su"
751: and returns true on success or undef otherwise.
752:
753: cir and mir also take a value to set the cir/mir to.
754:
755: su2su takes a group id parameter that is in hex.
756:
1.26 andrew 757: $t->sudb_modify($suid, 'cir', 512);
758:
1.14 andrew 759: You should save_sudb() after calling this, or your changes will be lost
760: when the AP is rebooted.
761:
762: =cut
763:
764: sub sudb_modify
765: {
766: my $self = shift;
767: my $suid = shift;
768: my $opt = shift;
769: my $value = shift;
770:
771: if ($suid =~ /\D/) {
1.25 andrew 772: $self->last_error("Invalid suid '$suid'");
1.14 andrew 773: return undef;
774: }
775:
776: if (lc($opt) eq 'cir' or lc($opt) eq 'mir') {
777: if ($value =~ /\D/) {
1.25 andrew 778: $self->last_error("Invalid $opt '$value'");
1.14 andrew 779: return undef;
780: }
781: } elsif (lc($opt) eq 'su2su') {
782: if ($value =~ /[^0-9A-Za-f]/) {
1.25 andrew 783: $self->last_error("Invalid MAC '$value'");
1.14 andrew 784: return undef;
785: }
786: } else {
1.25 andrew 787: $self->last_error("Invalid option '$opt'");
1.14 andrew 788: return undef;
789: }
790:
791: my $string = 'sudb modify ' . $suid . ' ' . $opt . ' ' . $value;
792:
793: return $self->cmd( String => $string, expect => $success );
794: }
795:
796: =pod
797:
1.26 andrew 798: =item B<enable_tftpd>
799:
800: - enable the TFTP server
1.14 andrew 801:
802: runs C<tftpd(args =E<gt> 'on')> and makes sure that Tftpd is now 'listen'ing
803:
804: =cut
805:
806: sub enable_tftpd
807: {
808: my $self = shift;
809:
810: my $vals = $self->tftpd( args => 'on' );
811:
812: if ($vals->{'Tftpd'} eq 'listen') {
813: return $vals;
814: } else {
815: return undef;
816: }
817: }
818:
819: =pod
820:
1.26 andrew 821: =item B<disable_tftpd>
822:
823: - disable the TFTP server
1.14 andrew 824:
825: runs C<tftpd(args =E<gt> 'off')> and makes sure that Tftpd is now 'disabled'
826:
827: =cut
828:
829: sub disable_tftpd
830: {
831: my $self = shift;
832:
833: my $vals = $self->tftpd( args => 'off' );
834:
835: if (ref $vals eq 'HASH' && $vals->{'Tftpd'} eq 'disabled') {
836: return $vals;
837: } else {
838: return undef;
839: }
840: }
841:
842: =pod
843:
1.26 andrew 844: =item B<cmd>
845:
846: - runs a command on the AP.
1.14 andrew 847:
1.20 andrew 848: This does most of the work. At the heart, it calls Net::Telnet::cmd()
849: but it also does some special stuff for Trango.
1.14 andrew 850:
851: Normally returns the last lines from from the command
852:
1.26 andrew 853: If you are using this, rather than one of the "easy" methods above,
854: you probably want to read through the source of this module to see how
855: some of the other commands are called.
856:
857: In addition to the Net::Telnet::cmd() options, it also accepts these:
1.14 andrew 858:
859: I<decode>
1.26 andrew 860:
1.20 andrew 861: - if this is true, then it will send the output lines to _decode_lines()
862: and then returns the decoded output
1.14 andrew 863:
1.26 andrew 864: I<no_prompt>
865:
866: - if this is true, it does not wait for a prompt, so you are not stuck
867: waiting for something that will never happen.
868:
1.14 andrew 869: I<cmd_disconnects>
1.26 andrew 870:
1.20 andrew 871: - if this is true, it then sets logged_in() to false, then it will
1.26 andrew 872: close() the connection and set is_connected() to false
1.14 andrew 873:
874: I<expect>
1.26 andrew 875:
1.20 andrew 876: - if this is set (usually to 'Success.') it will check for that in the
877: last line of output and if it does not, will return undef because the
878: command probably failed
1.14 andrew 879:
880: I<args>
1.26 andrew 881:
1.20 andrew 882: - a string containing the command line options that are passed to the
883: command
1.14 andrew 884:
1.26 andrew 885: $t->cmd( String => 'exit', no_prompt => 1, cmd_disconnects => 1 );
886:
1.14 andrew 887: =cut
888:
889: sub cmd
890: {
891: my $self = shift;
892:
893: my @valid_net_telnet_opts = qw(
894: String
895: Output
896: Cmd_remove_mode
897: Errmode
898: Input_record_separator
899: Ors
900: Output_record_separator
901: Prompt
902: Rs
903: Timeout
904: );
905:
906: my %cfg;
907: if (@_ == 1) {
908: $cfg{'String'} = shift;
909: } elsif (@_ > 1) {
910: %cfg = @_;
911: }
912:
913: $cfg{'Timeout'} ||= $self->Timeout;
914:
915: unless ($cfg{'String'}) {
1.25 andrew 916: $self->last_error("No command passed");
1.14 andrew 917: return undef;
918: }
919:
920: unless ($self->is_connected) {
1.25 andrew 921: $self->last_error("Not connected");
1.14 andrew 922: return undef;
923: }
924:
925: unless ($self->logged_in) {
1.25 andrew 926: $self->last_error("Not logged in");
1.14 andrew 927: return undef;
928: }
929:
930:
931: my %cmd;
932: foreach (@valid_net_telnet_opts) {
933: if (exists $cfg{$_}) {
934: $cmd{$_} = $cfg{$_};
935: }
936: }
937: if ($cfg{'args'}) {
938: $cmd{'String'} .= ' ' . $cfg{'args'};
939: }
940: my @lines;
941: unless ($cfg{'no_prompt'}) {
942: @lines = $self->SUPER::cmd(%cmd);
943: } else {
944: $self->print($cmd{'String'});
945: @lines = $self->lastline;
946: }
947:
948: $self->last_lines(\@lines);
949:
950: my $vals = 1;
951: if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
952: if ($cfg{'decode'} eq 'each') {
953: $vals = _decode_each_line(@lines);
954: } elsif ($cfg{'decode'} eq 'sulog') {
955: $vals = _decode_sulog(@lines);
956: } elsif ($cfg{'decode'} eq 'maclist') {
957: $vals = _decode_maclist(@lines);
958: } else {
959: $vals = _decode_lines(@lines);
960: }
961: }
962:
963: $self->last_vals($vals);
964:
965:
966: my $last = $self->lastline;
967:
968: if ((not $cfg{'expect'}) || $last =~ /$cfg{'expect'}$/) {
969: if ($cfg{'cmd_disconnects'}) {
970: $self->logged_in(0);
971: $self->close;
972: $self->is_connected(0);
973: }
974:
975: if ($PRIVATE{'Decode'} && $cfg{'decode'}) {
976: return $vals;
977: } else {
1.27 mike 978: return \@lines;
1.14 andrew 979: }
980: } else {
1.25 andrew 981: $self->last_error("Error with command ($cfg{'String'}): $last");
1.14 andrew 982: return undef;
983: }
984: }
985:
986: #=item _decode_lines
987:
988: sub _decode_lines
989: {
990: my @lines = @_;
991:
992: my %conf;
993:
994: my $key = '';
1.17 andrew 995: my $val = undef;
1.27 mike 996: my @vals;
1.14 andrew 997: my $in_key = 0;
1.17 andrew 998: my $in_val = 1;
1.14 andrew 999:
1000: foreach my $line (@lines) {
1001: next if $line =~ /$success$/;
1002:
1003: my @chars = split //, $line;
1004:
1005: my $last_key = '';
1006: foreach my $c (@chars) {
1007:
1008: if ($c eq '[' || $c eq "\r" || $c eq "\n") {
1009: if ($c eq '[') {
1010: $in_key = 1;
1011: $in_val = 0;
1012: } else {
1013: $in_key = 0;
1.17 andrew 1014: $in_val = 1;
1.14 andrew 1015: }
1016:
1017: if ($key) {
1018: $key =~ s/^\s+//;
1019: $key =~ s/\s+$//;
1020:
1.27 mike 1021: if ($val) {
1.17 andrew 1022: $val =~ s/^\s+//;
1023: $val =~ s/\s+$//;
1024: }
1.14 andrew 1025:
1026: if ($key eq 'Checksum' && $last_key) {
1027: # Special case for these bastids.
1028: my $new = $last_key;
1029: $new =~ s/\s+\S+$//;
1030: $key = $new . " " . $key;
1031: }
1032:
1.27 mike 1033: $conf{$key} = $val;
1.14 andrew 1034: $last_key = $key;
1035: $key = '';
1.27 mike 1036: } elsif ($val) {
1037: push @vals, $val;
1038: }
1039: $val = '';
1.14 andrew 1040:
1041: } elsif ($c eq ']') {
1042: $in_val = 1;
1043: $in_key = 0;
1044: $c = shift @chars;
1045:
1046: } elsif ($in_key) {
1047: $key .= $c;
1048:
1049: } elsif ($in_val) {
1050: $val .= $c;
1051: }
1052: }
1053: }
1054:
1.27 mike 1055: unless ($key) {
1056: push @vals, $val;
1057: }
1058:
1059: if (@vals == 1) {
1060: $val = $vals[0];
1061: } elsif (@vals) {
1062: $val= \@vals;
1063: } else {
1064: $val = undef;
1065: }
1066:
1.14 andrew 1067: if (%conf) {
1.27 mike 1068: $conf{_pre} = $val if $val;
1.14 andrew 1069: return \%conf;
1070: } else {
1.17 andrew 1071: return $val;
1.14 andrew 1072: }
1073: }
1074:
1075: #=item _decode_each_line
1076:
1077: sub _decode_each_line
1078: {
1079: my @lines = @_;
1080: my @decoded;
1081: foreach my $line (@lines) {
1082: my $decoded = _decode_lines($line);
1083: push @decoded, $decoded if defined $decoded;
1084: }
1085: return \@decoded;
1086: }
1087:
1088: #=item _decode_sulog
1089:
1090: sub _decode_sulog
1091: {
1092: my @lines = @_;
1093: my @decoded;
1094: my $last_tm;
1095: foreach my $line (@lines) {
1096: my $decoded = _decode_lines($line);
1097:
1098: if (defined $decoded) {
1099: if ($decoded->{'tm'}) {
1100: $last_tm = $decoded->{'tm'};
1101: next;
1102: } else {
1103: $decoded->{'tm'} = $last_tm;
1104: }
1105: next unless $last_tm;
1106:
1107: push @decoded, $decoded if defined $decoded;
1108: }
1109: }
1110: return \@decoded;
1111: }
1112:
1113: #=item _decode_maclist
1114:
1115: sub _decode_maclist
1116: {
1117: my @lines = @_;
1118: my @decoded;
1119: my $total_entries = 0;
1120: my $current_tm = 0;
1121: foreach my $line (@lines) {
1122: $line =~ s/\r?\n$//;
1123: my ($mac, $loc, $tm) = $line =~ /
1124: ([0-9a-fA-F ]{17})\s+
1125: (.*)\s+
1126: tm\s+
1127: (\d+)
1128: /x;
1129:
1130: if ($mac) {
1131: $mac =~ s/\s+//g;
1132: $loc =~ s/^\s+//;
1133: $loc =~ s/\s+$//;
1134:
1135: my $suid = undef;
1136: if ($loc =~ /suid\s+=\s+(\d+)/) {
1137: $suid = $1;
1138: $loc = undef;
1139: }
1140:
1141: push @decoded, {
1142: mac => $mac,
1143: loc => $loc,
1144: tm => $tm,
1145: suid => $suid,
1146: };
1147: } elsif ($line =~ /(\d+)\s+entries/) {
1148: $total_entries = $1;
1149: } elsif ($line =~ /current tm = (\d+)\s+sec/) {
1150: $current_tm = $1
1151: }
1152: }
1.15 andrew 1153:
1154: map { $_->{'cur_tm'} = $current_tm } @decoded;
1155:
1.14 andrew 1156: if (scalar @decoded == $total_entries) {
1157: return \@decoded;
1158: } else {
1159: # XXX we should have a way to set last error, not sure why we don't
1160: return undef;
1161: }
1162: }
1163:
1164: 1;
1165: __END__
1166:
1167: =back
1168:
1169: =head1 SEE ALSO
1170:
1.26 andrew 1171: Trango Documentation - http://www.trangobroadband.com/support/product_docs.htm
1.14 andrew 1172:
1173: L<Net::Telnet>
1174:
1175: =head1 TODO
1176:
1.20 andrew 1177: There are still a lot of commands that are not accessed directly. If
1178: you call them (as cmd("command + args") or whatever) and it works,
1179: please send me examples that work and I will try to get it incorporated
1180: into the next version of the script.
1181:
1182: I also want to be able to parse the different types of output from
1183: commands like su, sudb all and anything else that would be better
1184: available as a perl datastructure.
1.14 andrew 1185:
1186: =head1 AUTHOR
1187:
1188: Andrew Fresh E<lt>andrew@rraz.netE<gt>
1189:
1190: =head1 COPYRIGHT AND LICENSE
1191:
1.26 andrew 1192: Copyright (C) 2005,2006,2007 by Andrew Fresh
1.14 andrew 1193:
1194: This library is free software; you can redistribute it and/or modify
1195: it under the same terms as Perl itself, either Perl version 5.8.7 or,
1196: at your option, any later version of Perl 5 you may have available.
1197:
1198: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>