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