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