Annotation of trango/Net-Telnet-Trango/scripts/su.cgi, Revision 1.1
1.1 ! andrew 1: #!/usr/bin/perl
! 2: # $RedRiver$
! 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:
! 16: my $host_file = '/conf/wstationinfo/hosts.xml';
! 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/;
! 25: use XML::Simple;
! 26: use Net::Telnet::Trango;
! 27:
! 28: use File::Basename;
! 29:
! 30: my $me = basename($0);
! 31:
! 32: print header,
! 33: start_html('Trango SU Utilities'),
! 34: h1('Trango SU Utilities');
! 35:
! 36: my $aps = get_aps($host_file);
! 37:
! 38: if (param()) {
! 39:
! 40: my $AP = param('AP');
! 41:
! 42: unless (exists $aps->{$AP}) {
! 43: print h3("AP '$AP' does not exist!");
! 44: print end_html;
! 45: exit;
! 46: }
! 47:
! 48: my $sumac = param('sumac');
! 49:
! 50: $sumac =~ s/[^0-9A-Fa-f]//g;
! 51: $sumac = uc($sumac);
! 52:
! 53: my $suid = param('suid');
! 54:
! 55: if (length $sumac == 12) {
! 56: add_su($aps->{$AP}, $sumac);
! 57: } elsif (length $suid) {
! 58: testrflink($aps->{$AP}, $suid);
! 59: } else {
! 60: print h3("Invalid SUID '$suid' and MAC '$sumac'");
! 61: show_form($aps, $default_mac);
! 62: }
! 63:
! 64: } else {
! 65: show_form($aps, $default_mac);
! 66: }
! 67:
! 68:
! 69: print end_html;
! 70:
! 71:
! 72: sub get_aps
! 73: {
! 74: my $file = shift;
! 75:
! 76: my $conf = Read_Hosts($file);
! 77:
! 78: my %aps;
! 79:
! 80: foreach my $ap (@{ $conf }) {
! 81: if ($ap->{'group'} eq 'Trango') {
! 82: my $name = $ap->{'comment'} || $ap->{'name'};
! 83: $aps{ $name } = $ap;
! 84: }
! 85: }
! 86:
! 87: return \%aps;
! 88:
! 89: return {
! 90: 'rrlhcwap0000' => {
! 91: group => 'Trango',
! 92: version => 1,
! 93: name => '192.168.1.1',
! 94: port => 161,
! 95: Read_Community => 'private',
! 96: Write_Community => 'private',
! 97: }
! 98: };
! 99:
! 100: }
! 101:
! 102: sub show_form
! 103: {
! 104: my $aps = shift;
! 105:
! 106: my %cache = ();
! 107: my @ap_names = sort {
! 108: my @a = $a =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
! 109: my @b = $b =~ /(\d+)\.(\d+)\.(\d+)\.(\d+)/;
! 110:
! 111: if (@a) {
! 112: $cache{$a} ||= pack('C4' => @a);
! 113: } else {
! 114: $cache{$a} ||= lc($a);
! 115: }
! 116: if (@b) {
! 117: $cache{$b} ||= pack('C4' => @b);
! 118: } else {
! 119: $cache{$b} ||= lc($b);
! 120: }
! 121:
! 122: $cache{$a} cmp $cache{$b};
! 123: } keys %{ $aps };
! 124:
! 125: print p(start_form(-method => 'GET'),
! 126: 'AP: ', popup_menu(-name=>'AP', -values=>\@ap_names),br,
! 127: 'SUMAC: ', textfield(-name=>'sumac',-default=>$default_mac),br,
! 128: 'SUID: ', textfield(-name=>'suid',-default=>$default_suid),br,
! 129: submit,
! 130: end_form);
! 131:
! 132: print p('Fill in the SUMAC if you wish to add an SU ',
! 133: 'or fill in the SUID to run an rflinktest.');
! 134:
! 135: return 1;
! 136: }
! 137:
! 138: sub login
! 139: {
! 140: my $host = shift;
! 141: my $password = shift;
! 142:
! 143: my $t = new Net::Telnet::Trango ( Timeout => 5 );
! 144:
! 145: #$t->input_log('/tmp/telnet_log');
! 146: #$t->dump_log('/tmp/telnet_log');
! 147:
! 148: unless ($t->open( Host => $host )) {
! 149: print h3("Error connecting!");
! 150: $t->close;
! 151: return undef;
! 152: }
! 153:
! 154: unless ($t->login( $password ) ) {
! 155: print h3("Couldn't log in: $!");
! 156: $t->exit;
! 157: $t->close;
! 158: return undef;
! 159: }
! 160:
! 161: return $t;
! 162: }
! 163:
! 164: sub add_su
! 165: {
! 166: my $ap = shift;
! 167: my $sumac = shift;
! 168:
! 169: my $t = login($ap->{'name'}, $ap->{'Telnet_Password'});
! 170:
! 171: my $cur_sus = $t->sudb_view;
! 172:
! 173: my $new_suid = next_suid($cur_sus);
! 174:
! 175: foreach my $su (@{ $cur_sus }) {
! 176: if ($sumac eq $su->{'mac'}) {
! 177: print h3("MAC '$sumac' already in AP '$ap->{'name'}' with SUID '$su->{'suid'}'");
! 178: $t->exit;
! 179: $t->close;
! 180: return undef;
! 181: }
! 182: }
! 183:
! 184: unless ($t->sudb_add(
! 185: $new_suid, 'reg', $default_cir, $default_mir, $sumac
! 186: ) ) {
! 187: print h3("Error adding SU!");
! 188: $t->exit;
! 189: $t->close;
! 190: return undef;
! 191: }
! 192:
! 193: my $new_sus = $t->sudb_view;
! 194: my $added = 0;
! 195: foreach my $su (@{ $new_sus }) {
! 196: if ($su->{'suid'} == $new_suid) {
! 197: $added = 1;
! 198: last;
! 199: }
! 200: }
! 201:
! 202: unless ($added) {
! 203: print h3("Couldn't add su id: $new_suid");
! 204: $t->exit;
! 205: $t->close;
! 206: return undef;
! 207: }
! 208:
! 209: unless ($t->save_sudb) {
! 210: print h3("Couldn't save sudb");
! 211: $t->exit;
! 212: $t->close;
! 213: return undef;
! 214: }
! 215:
! 216: print p(
! 217: "Added new SU with ID '$new_suid' " .
! 218: "and MAC '$sumac' " .
! 219: "to '$ap->{'name'}'. " .
! 220: '<a href="' . $me . '?' .
! 221: 'AP=' . $ap->{'name'} . '&' .
! 222: 'suid=' . $new_suid .
! 223: '">Test SU RFLink</a>'
! 224: );
! 225:
! 226: $t->exit;
! 227: $t->close;
! 228: return 1;
! 229:
! 230: }
! 231:
! 232: sub testrflink
! 233: {
! 234: my $ap = shift;
! 235: my $suid = shift;
! 236:
! 237: my $t = login($ap->{'name'}, $ap->{'Telnet_Password'});
! 238:
! 239: my $result = $t->su_testrflink( $suid );
! 240:
! 241: unless ($result) {
! 242: print h3("Error testing SU rflink!");
! 243: $t->exit;
! 244: $t->close;
! 245: return undef;
! 246: }
! 247:
! 248: my @keys = ('suid', 'AP Tx', 'AP Rx', 'SU Rx');
! 249:
! 250: my @table;
! 251: foreach my $su (@{ $result }) {
! 252: next unless ref $su eq 'HASH';
! 253: next unless exists $su->{'suid'};
! 254: $su->{'suid'} =~ s/\D//g;
! 255: next unless $su->{'suid'};
! 256:
! 257: push @table, td([ @{ $su }{ @keys } ]);
! 258: }
! 259:
! 260: print table({-border=>1,-cellspacing=>0,-cellpadding=>1},
! 261: caption($ap->{'name'} . ': su testrflink ' . $suid),
! 262: Tr({-align=>'CENTER', -valign=>'TOP'},
! 263: [ th(\@keys), @table ]
! 264: )
! 265: );
! 266:
! 267: $t->exit;
! 268: $t->close;
! 269: return 1;
! 270:
! 271: }
! 272:
! 273: sub next_suid
! 274: {
! 275: my $sudb = shift;
! 276:
! 277: my $next_id = $Start_SUID;
! 278:
! 279: my %ids = map { $_->{'suid'} => 1 } @{ $sudb };
! 280:
! 281: my $next_key = sprintf('%04d', $next_id);
! 282: while (exists $ids{$next_key}) {
! 283: $next_id++;
! 284: $next_key = sprintf('%04d', $next_id);
! 285: }
! 286:
! 287: return $next_id;
! 288: }
! 289:
! 290: sub Read_Hosts
! 291: {
! 292: my $file = shift;
! 293: my $xs = XML::Simple->new();
! 294:
! 295: my %Default_SNMP = (
! 296: Read_Community => 'public',
! 297: Telnet_Password => 'password',
! 298: port => 161,
! 299: version => 1,
! 300: );
! 301:
! 302: my @hosts;
! 303: my $hosts = $xs->XMLin($file,
! 304: keyattr => [ 'Station' ],
! 305: ForceArray => qr/^Host$/,
! 306: );
! 307:
! 308: foreach my $group (keys %{ $hosts }) {
! 309: next if $group eq 'Read_Community';
! 310:
! 311: foreach my $host (@{ $hosts->{$group}->{Host} }) {
! 312: foreach my $item ('Read_Community', 'Telnet_Password', 'port', 'version') {
! 313: $host->{$item} =
! 314: $host->{$item} ||
! 315: $hosts->{$group}->{$item} ||
! 316: $hosts->{$item} ||
! 317: $Default_SNMP{$item};
! 318: }
! 319: $host->{group} = $group;
! 320:
! 321: push @hosts, $host;
! 322: }
! 323: }
! 324:
! 325: return \@hosts;
! 326: }
! 327:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>