Changeset 371 for trunk/cgi-bin/snCalc.cgi
- Timestamp:
- 11/27/07 12:27:45 (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cgi-bin/snCalc.cgi
r4 r371 3 3 use strict; 4 4 use warnings; 5 use CGI::Carp qw(fatalsToBrowser); 6 use NetAddr::IP; 7 use CommonWeb qw(:ALL);; 5 8 6 9 #file snCalc.cgi little subnet calculator app … … 11 14 print "Content-Type: text/html\n\n"; 12 15 13 open(HTML, "../startsn.html")|| die "Could not open star sn.html :$!";16 open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!"; 14 17 my $start = join('', <HTML>); 15 18 close(HTML); 16 19 print $start; 17 20 18 if($webvar{input} =~ m/(\/\d\d)/) 19 {20 $input =$1;21 } 22 else 23 {24 $input = '/29';21 # Clean up input so we don't divide by zero or something equally silly 22 if ($webvar{input} =~ m/(\d+)/) { 23 $input = 1*$1; 24 $input = 3 if $input < 3; 25 $input = 29 if $input > 29; # Not doing IPv6 yet... 26 } else { 27 $input = 29; 25 28 } 26 29 27 if(substr($input, 1, 2) > 32 || substr($input, 1, 2) < 24) 28 { 29 printAndExit("'$input' is an invalid netmask."); 30 } 31 else 32 { $input = substr($input, 1, 2); 33 my $ltinput = $input -1; 34 my $gtinput = $input +1; 35 36 print qq( <div class="center"> 37 <table align="center" cellspacing="3" cellpadding="3"><tr> 38 <td class="heading" align="center"> Results for /$ltinput</td><td class="heading" align="center"> Results for /$input</td><td class="heading" align="center"> Results for /$gtinput</td></tr> 39 ); 30 my $ltinput = $input - 1; 31 my $gtinput = $input + 1; 40 32 41 if( $input =~ m|.*24.*|){ 42 $input = 256; 43 $ltinput =128; 44 undef($gtinput); 45 } 46 elsif( $input =~ m|.*25.*|){ 47 $input = 128; 48 $ltinput = 64; 49 $gtinput = 256; 50 } 51 elsif( $input =~ m|.*26.*|){ 52 $input = 64; 53 $ltinput = 32; 54 $gtinput = 128; 55 } 56 elsif( $input =~ m|.*27.*|){ 57 $input = 32; 58 $ltinput = 16; 59 $gtinput = 64; 60 } 61 elsif( $input =~ m|.*28.*|){ 62 $input = 16; 63 $ltinput = 8; 64 $gtinput = 32; 65 } 66 elsif( $input =~ m|.*29.*|){ 67 $input = 8; 68 $ltinput = 4; 69 $gtinput = 16; 70 } 71 elsif( $input =~ m|.*30.*|){ 72 $input = 4; 73 $ltinput = 2; 74 $gtinput = 8; 75 } 76 elsif( $input =~ m|.*31.*|){ 77 $input = 2; 78 $ltinput = 1; 79 $gtinput = 4; 80 } 81 elsif( $input =~ m|.*32.*|){ 82 $input = 1; 83 $gtinput = 2; 84 undef($ltinput); 85 } 33 my $prenet = new NetAddr::IP "0.0.0.0/$ltinput"; 34 my $net = new NetAddr::IP "0.0.0.0/$input"; 35 my $postnet = new NetAddr::IP "0.0.0.0/$gtinput"; 86 36 87 my ($center, $left, $right) = ('','',''); 88 my $subnet; 89 90 #add the subnet masks 91 92 if($input) 93 { 94 $subnet = 256 - $input; 95 $center = qq(<div style="background-color: #00ff00">255.255.255.$subnet </div>); 96 } 37 print qq(<div class="center"> 38 <table align="center" cellspacing="3" cellpadding="3"> 39 <tr> 40 <td class="heading" align="center">Results for /$ltinput</td> 41 <td class="heading" align="center">Results for /$input</td> 42 <td class="heading" align="center">Results for /$gtinput</td> 43 </tr> 44 ); 97 45 98 if($ltinput) 99 { 100 $subnet = 256 - $ltinput; 101 $right = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>); 102 } 103 if($gtinput) 104 { 105 $subnet = 256 - $gtinput; 106 $left = qq(<div style="background-color: #00ff00">255.255.255.$subnet</div>); 107 } 46 print qq(<tr><td valign="top">\n). 47 qq( <div class="mask">).$prenet->mask."</div>\n". 48 qq( <div class="wildcard">).$prenet->wildcard."</div>\n". 49 getranges($ltinput). 50 qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n). 51 qq( <div class="mask">).$net->mask."</div>\n". 52 qq( <div class="wildcard">).$net->wildcard."</div>\n". 53 getranges($input). 54 qq(</td>\n<td valign="top">). 55 qq( <div class="mask">).$postnet->mask."</div>\n". 56 qq( <div class="wildcard">).$postnet->wildcard."</div>\n". 57 getranges($gtinput); 58 59 print "</td></tr>\n</table>\n"; 60 61 print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading"> 62 </div> 63 </body> 64 </html> 65 ); 66 67 # Just In Case 68 exit 0; 69 70 # subs 71 sub xrange { 72 my $block = shift; 73 my $masklen = shift; 74 my $data = $block->range; 75 if ($masklen >= 24) { 76 $data =~ s/\b0\.0\.0\./x.x.x./g; 77 } elsif ($masklen >=16) { 78 $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g; 79 } elsif ($masklen >=8) { 80 $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g; 81 } 82 return $data; 83 } # xrange() 108 84 109 85 110 for(my $i = 0; $i < 256; $i++) 111 { 112 #left display -- one less than requested 113 if(defined($gtinput) && $i % $gtinput == 0) 114 { 115 my $upper = $i + $gtinput -1; 116 $left .= "x.x.x.$i - x.x.x.$upper</br>\n"; 117 } 118 119 #center display -- the one requested 120 if($i % $input == 0 ) 121 { 122 my $upper = $i + $input - 1; 123 $center .= "x.x.x.$i - x.x.x.$upper </br>\n"; 124 } 125 126 #right display -- one more than requested 127 if(defined($ltinput) && $i % $ltinput == 0) 128 { 129 my $upper = $i + $ltinput -1; 130 $right .= "x.x.x.$i - x.x.x.$upper </br>\n"; 131 } 132 } 133 134 print qq(<tr><td valign="top" >$left</td> 135 <td valign="top" bgcolor="#d0e0e0">$center</td> 136 <td valign="top" >$right</td></tr> 137 ); 138 print "</table>\n"; 139 140 print '<input type="button" value="Back" onclick="history.go(-1)" class="heading"></div></body></html>'; 141 142 } 143 144 145 sub parse_post { 146 my $buffer; 147 if ($ENV{'REQUEST_METHOD'} eq "GET") { 148 $buffer=$ENV{'QUERY_STRING'} 149 } elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") { 150 read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); 86 sub getranges { 87 my $masklen = shift; 88 my $ret = ''; 89 my $super; 90 if ($masklen < 8) { 91 $super = new NetAddr::IP "0.0.0.0/0"; 92 } elsif ($masklen < 16) { 93 $super = new NetAddr::IP "0.0.0.0/8"; 94 } elsif ($masklen < 24) { 95 $super = new NetAddr::IP "0.0.0.0/16"; 151 96 } else { 152 $buffer = $ENV{'QUERY_STRING'}; 153 $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); 97 $super = new NetAddr::IP "0.0.0.0/24"; 154 98 } 155 my @pairs = split(/&/, $buffer); 156 my %webvarLocal; 157 foreach my $pair (@pairs) { 158 my ($name, $value) = split(/=/, $pair); 159 $name =~ tr/+/ /; 160 $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; 161 $value =~ tr/+/ /; 162 $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; 163 $value =~ s/\'/\\\'/g; 164 $webvarLocal{$name} = $value; 99 foreach my $net ($super->split($masklen)) { 100 $ret .= "\t".xrange($net,$masklen)."<br />\n"; 165 101 } 166 return %webvarLocal; 167 } 168 sub printAndExit 169 { 170 my $errStr = $_[0]; 171 print qq( 172 <center><p class="regular"> $errStr </p> 173 <input type="button" value="Back" onclick="history.go(-1)" class="heading"> 174 </center> 175 ); 176 exit(0); 177 } 102 return $ret; 103 } # getranges()
Note:
See TracChangeset
for help on using the changeset viewer.