Annotation of trango/Net-Telnet-Trango/scripts/su.cgi, Revision 1.2
1.1 andrew 1: #!/usr/bin/perl
1.2 ! andrew 2: # $RedRiver: su.cgi,v 1.1 2007/02/07 16:55:12 andrew Exp $
1.1 andrew 3: ########################################################################
4: # su.cgi *** a CGI for Trango SU utilities.
5: #
6: # 2007.02.07 #*#*# andrew fresh <andrew@mad-techies.org>
7: ########################################################################
8: # Copyright (C) 2007 by Andrew Fresh
9: #
10: # This program is free software; you can redistribute it and/or modify
11: # it under the same terms as Perl itself.
12: ########################################################################
13: use strict;
14: use warnings;
15:
1.2 ! andrew 16: my $host_file = 'su.yaml';
1.1 andrew 17:
18: my $default_mac = '0001DE';
19: my $default_suid = 'all';
20: my $default_cir = 256;
21: my $default_mir = 9999;
22: my $Start_SUID = 3;
23:
24: use CGI qw/:standard/;
1.2 ! andrew 25: use File::Basename;
! 26: use YAML qw/ LoadFile Dump /;
1.1 andrew 27: use Net::Telnet::Trango;
28:
1.2 ! andrew 29: my $me = basename($0);
1.1 andrew 30:
1.2 ! andrew 31: my $aps = get_aps($host_file);
1.1 andrew 32:
33: print header,
34: start_html('Trango SU Utilities'),
35: h1('Trango SU Utilities');
36:
37: if (param()) {
38:
39: my $AP = param('AP');
40:
41: unless (exists $aps->{$AP}) {
42: print h3("AP '$AP' does not exist!");
43: print end_html;
44: exit;
45: }
46:
47: my $sumac = param('sumac');
48:
49: $sumac =~ s/[^0-9A-Fa-f]//g;
50: $sumac = uc($sumac);
51:
52: my $suid = param('suid');
53:
54: if (length $sumac == 12) {
55: add_su($aps->{$AP}, $sumac);
56: } elsif (length $suid) {
57: testrflink($aps->{$AP}, $suid);
58: } else {
59: print h3("Invalid SUID '$suid' and MAC '$sumac'");
60: show_form($aps, $default_mac);
61: }
62:
63: } else {
64: show_form($aps, $default_mac);
65: }
66:
67:
68: print end_html;
69:
70:
71: sub get_aps
72: {
73: my $file = shift;
74:
1.2 ! andrew 75: my $conf = LoadFile($file);
1.1 andrew 76:
77: my %aps;
78:
1.2 ! andrew 79: foreach my $ap (keys %{ $conf }) {
! 80: next if $ap eq 'default';
! 81: $aps{ $ap } = $conf->{$ap};
! 82: if (ref $conf->{default} eq 'HASH') {
! 83: foreach my $k (keys %{ $conf->{default} }) {
! 84: $aps{ $ap }{$k} ||= $conf->{default}->{$k};
! 85: }
1.1 andrew 86: }
87: }
88:
89: return \%aps;
90:
91: return {
92: 'rrlhcwap0000' => {
1.2 ! andrew 93: name => '192.168.1.1',
! 94: password => 'trango',
1.1 andrew 95: }
96: };
97:
98: }
99:
100: sub show_form
101: {
102: my $aps = shift;
103:
104: my %cache = ();
105: my @ap_names = sort {
106: my @a = $a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
107: my @b = $b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
108:
109: if (@a) {
110: $cache{$a} ||= pack('C4' => @a);
111: } else {
112: $cache{$a} ||= lc($a);
113: }
114: if (@b) {
115: $cache{$b} ||= pack('C4' => @b);
116: } else {
117: $cache{$b} ||= lc($b);
118: }
119:
120: $cache{$a} cmp $cache{$b};
121: } keys %{ $aps };
122:
123: print p(start_form(-method => 'GET'),
1.2 ! andrew 124: 'AP: ', popup_menu(-name=>'AP', -values=>\@ap_names),br,
! 125: 'SUMAC: ', textfield( -name=>'sumac', -default=>$default_mac),br,
! 126: 'SUID: ', textfield( -name=>'suid', -default=>$default_suid),br,
1.1 andrew 127: submit,
128: end_form);
129:
130: print p('Fill in the SUMAC if you wish to add an SU ',
131: 'or fill in the SUID to run an rflinktest.');
132:
133: return 1;
134: }
135:
136: sub login
137: {
138: my $host = shift;
139: my $password = shift;
140:
141: my $t = new Net::Telnet::Trango ( Timeout => 5 );
142:
143: #$t->input_log('/tmp/telnet_log');
144: #$t->dump_log('/tmp/telnet_log');
145:
146: unless ($t->open( Host => $host )) {
147: print h3("Error connecting!");
148: $t->close;
149: return undef;
150: }
151:
152: unless ($t->login( $password ) ) {
153: print h3("Couldn't log in: $!");
154: $t->exit;
155: $t->close;
156: return undef;
157: }
158:
159: return $t;
160: }
161:
162: sub add_su
163: {
164: my $ap = shift;
165: my $sumac = shift;
166:
1.2 ! andrew 167: my $t = login($ap->{name}, $ap->{password});
1.1 andrew 168:
169: my $cur_sus = $t->sudb_view;
170:
171: my $new_suid = next_suid($cur_sus);
172:
173: foreach my $su (@{ $cur_sus }) {
1.2 ! andrew 174: if ($sumac eq $su->{mac}) {
! 175: print h3("MAC '$sumac' already in AP '$ap->{name}' " .
! 176: "with SUID '$su->{suid}'");
1.1 andrew 177: $t->exit;
178: $t->close;
179: return undef;
180: }
181: }
182:
183: unless ($t->sudb_add(
184: $new_suid, 'reg', $default_cir, $default_mir, $sumac
185: ) ) {
186: print h3("Error adding SU!");
187: $t->exit;
188: $t->close;
189: return undef;
190: }
191:
192: my $new_sus = $t->sudb_view;
193: my $added = 0;
194: foreach my $su (@{ $new_sus }) {
1.2 ! andrew 195: if ($su->{suid} == $new_suid) {
1.1 andrew 196: $added = 1;
197: last;
198: }
199: }
200:
201: unless ($added) {
202: print h3("Couldn't add su id: $new_suid");
203: $t->exit;
204: $t->close;
205: return undef;
206: }
207:
208: unless ($t->save_sudb) {
209: print h3("Couldn't save sudb");
210: $t->exit;
211: $t->close;
212: return undef;
213: }
214:
215: print p(
216: "Added new SU with ID '$new_suid' " .
217: "and MAC '$sumac' " .
1.2 ! andrew 218: "to '$ap->{name}'. " .
1.1 andrew 219: '<a href="' . $me . '?' .
1.2 ! andrew 220: 'AP=' . $ap->{name} . '&' .
1.1 andrew 221: 'suid=' . $new_suid .
222: '">Test SU RFLink</a>'
223: );
224:
225: $t->exit;
226: $t->close;
227: return 1;
228:
229: }
230:
231: sub testrflink
232: {
233: my $ap = shift;
234: my $suid = shift;
235:
1.2 ! andrew 236: my $t = login($ap->{name}, $ap->{password});
1.1 andrew 237:
238: my $result = $t->su_testrflink( $suid );
239:
240: unless ($result) {
241: print h3("Error testing SU rflink!");
242: $t->exit;
243: $t->close;
244: return undef;
245: }
246:
247: my @keys = ('suid', 'AP Tx', 'AP Rx', 'SU Rx');
248:
249: my @table;
250: foreach my $su (@{ $result }) {
251: next unless ref $su eq 'HASH';
1.2 ! andrew 252: next unless exists $su->{suid};
! 253: $su->{suid} =~ s/\D//g;
! 254: next unless $su->{suid};
1.1 andrew 255:
256: push @table, td([ @{ $su }{ @keys } ]);
257: }
258:
259: print table({-border=>1,-cellspacing=>0,-cellpadding=>1},
1.2 ! andrew 260: caption($ap->{name} . ': su testrflink ' . $suid),
1.1 andrew 261: Tr({-align=>'CENTER', -valign=>'TOP'},
262: [ th(\@keys), @table ]
263: )
264: );
265:
266: $t->exit;
267: $t->close;
268: return 1;
269:
270: }
271:
272: sub next_suid
273: {
274: my $sudb = shift;
275:
276: my $next_id = $Start_SUID;
277:
1.2 ! andrew 278: my %ids = map { $_->{suid} => 1 } @{ $sudb };
1.1 andrew 279:
280: my $next_key = sprintf('%04d', $next_id);
281: while (exists $ids{$next_key}) {
282: $next_id++;
283: $next_key = sprintf('%04d', $next_id);
284: }
285:
286: return $next_id;
287: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>