=================================================================== RCS file: /cvs/trango/Net-Telnet-Trango/scripts/su.cgi,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- trango/Net-Telnet-Trango/scripts/su.cgi 2007/02/07 16:55:12 1.1 +++ trango/Net-Telnet-Trango/scripts/su.cgi 2007/02/07 17:42:56 1.2 @@ -1,5 +1,5 @@ #!/usr/bin/perl -# $RedRiver$ +# $RedRiver: su.cgi,v 1.1 2007/02/07 16:55:12 andrew Exp $ ######################################################################## # su.cgi *** a CGI for Trango SU utilities. # @@ -13,7 +13,7 @@ use strict; use warnings; -my $host_file = '/conf/wstationinfo/hosts.xml'; +my $host_file = 'su.yaml'; my $default_mac = '0001DE'; my $default_suid = 'all'; @@ -22,19 +22,18 @@ my $Start_SUID = 3; use CGI qw/:standard/; -use XML::Simple; +use File::Basename; +use YAML qw/ LoadFile Dump /; use Net::Telnet::Trango; -use File::Basename; - my $me = basename($0); +my $aps = get_aps($host_file); + print header, start_html('Trango SU Utilities'), h1('Trango SU Utilities'); -my $aps = get_aps($host_file); - if (param()) { my $AP = param('AP'); @@ -73,14 +72,17 @@ { my $file = shift; - my $conf = Read_Hosts($file); + my $conf = LoadFile($file); my %aps; - foreach my $ap (@{ $conf }) { - if ($ap->{'group'} eq 'Trango') { - my $name = $ap->{'comment'} || $ap->{'name'}; - $aps{ $name } = $ap; + foreach my $ap (keys %{ $conf }) { + next if $ap eq 'default'; + $aps{ $ap } = $conf->{$ap}; + if (ref $conf->{default} eq 'HASH') { + foreach my $k (keys %{ $conf->{default} }) { + $aps{ $ap }{$k} ||= $conf->{default}->{$k}; + } } } @@ -88,12 +90,8 @@ return { 'rrlhcwap0000' => { - group => 'Trango', - version => 1, - name => '192.168.1.1', - port => 161, - Read_Community => 'private', - Write_Community => 'private', + name => '192.168.1.1', + password => 'trango', } }; @@ -123,9 +121,9 @@ } keys %{ $aps }; print p(start_form(-method => 'GET'), - 'AP: ', popup_menu(-name=>'AP', -values=>\@ap_names),br, - 'SUMAC: ', textfield(-name=>'sumac',-default=>$default_mac),br, - 'SUID: ', textfield(-name=>'suid',-default=>$default_suid),br, + 'AP: ', popup_menu(-name=>'AP', -values=>\@ap_names),br, + 'SUMAC: ', textfield( -name=>'sumac', -default=>$default_mac),br, + 'SUID: ', textfield( -name=>'suid', -default=>$default_suid),br, submit, end_form); @@ -166,15 +164,16 @@ my $ap = shift; my $sumac = shift; - my $t = login($ap->{'name'}, $ap->{'Telnet_Password'}); + my $t = login($ap->{name}, $ap->{password}); my $cur_sus = $t->sudb_view; my $new_suid = next_suid($cur_sus); foreach my $su (@{ $cur_sus }) { - if ($sumac eq $su->{'mac'}) { - print h3("MAC '$sumac' already in AP '$ap->{'name'}' with SUID '$su->{'suid'}'"); + if ($sumac eq $su->{mac}) { + print h3("MAC '$sumac' already in AP '$ap->{name}' " . + "with SUID '$su->{suid}'"); $t->exit; $t->close; return undef; @@ -193,7 +192,7 @@ my $new_sus = $t->sudb_view; my $added = 0; foreach my $su (@{ $new_sus }) { - if ($su->{'suid'} == $new_suid) { + if ($su->{suid} == $new_suid) { $added = 1; last; } @@ -216,9 +215,9 @@ print p( "Added new SU with ID '$new_suid' " . "and MAC '$sumac' " . - "to '$ap->{'name'}'. " . + "to '$ap->{name}'. " . 'Test SU RFLink' ); @@ -234,7 +233,7 @@ my $ap = shift; my $suid = shift; - my $t = login($ap->{'name'}, $ap->{'Telnet_Password'}); + my $t = login($ap->{name}, $ap->{password}); my $result = $t->su_testrflink( $suid ); @@ -250,15 +249,15 @@ my @table; foreach my $su (@{ $result }) { next unless ref $su eq 'HASH'; - next unless exists $su->{'suid'}; - $su->{'suid'} =~ s/\D//g; - next unless $su->{'suid'}; + next unless exists $su->{suid}; + $su->{suid} =~ s/\D//g; + next unless $su->{suid}; push @table, td([ @{ $su }{ @keys } ]); } print table({-border=>1,-cellspacing=>0,-cellpadding=>1}, - caption($ap->{'name'} . ': su testrflink ' . $suid), + caption($ap->{name} . ': su testrflink ' . $suid), Tr({-align=>'CENTER', -valign=>'TOP'}, [ th(\@keys), @table ] ) @@ -276,7 +275,7 @@ my $next_id = $Start_SUID; - my %ids = map { $_->{'suid'} => 1 } @{ $sudb }; + my %ids = map { $_->{suid} => 1 } @{ $sudb }; my $next_key = sprintf('%04d', $next_id); while (exists $ids{$next_key}) { @@ -286,42 +285,3 @@ return $next_id; } - -sub Read_Hosts -{ - my $file = shift; - my $xs = XML::Simple->new(); - - my %Default_SNMP = ( - Read_Community => 'public', - Telnet_Password => 'password', - port => 161, - version => 1, - ); - - my @hosts; - my $hosts = $xs->XMLin($file, - keyattr => [ 'Station' ], - ForceArray => qr/^Host$/, - ); - - foreach my $group (keys %{ $hosts }) { - next if $group eq 'Read_Community'; - - foreach my $host (@{ $hosts->{$group}->{Host} }) { - foreach my $item ('Read_Community', 'Telnet_Password', 'port', 'version') { - $host->{$item} = - $host->{$item} || - $hosts->{$group}->{$item} || - $hosts->{$item} || - $Default_SNMP{$item}; - } - $host->{group} = $group; - - push @hosts, $host; - } - } - - return \@hosts; -} -