#!/usr/bin/perl use strict; use warnings; #file snCalc.cgi little subnet calculator app my %webvar = parse_post(); my $input; print "Content-Type: text/html\n\n"; open(HTML, "../startsn.html")|| die "Could not open starsn.html :$!"; my $start = join('', ); close(HTML); print $start; if($webvar{input} =~ m/(\/\d\d)/) { $input = $1; } else { $input = '/29'; } if(substr($input, 1, 2) > 32 || substr($input, 1, 2) < 24) { printAndExit("'$input' is an invalid netmask."); } else { $input = substr($input, 1, 2); my $ltinput = $input -1; my $gtinput = $input +1; print qq(
); if( $input =~ m|.*24.*|){ $input = 256; $ltinput =128; undef($gtinput); } elsif( $input =~ m|.*25.*|){ $input = 128; $ltinput = 64; $gtinput = 256; } elsif( $input =~ m|.*26.*|){ $input = 64; $ltinput = 32; $gtinput = 128; } elsif( $input =~ m|.*27.*|){ $input = 32; $ltinput = 16; $gtinput = 64; } elsif( $input =~ m|.*28.*|){ $input = 16; $ltinput = 8; $gtinput = 32; } elsif( $input =~ m|.*29.*|){ $input = 8; $ltinput = 4; $gtinput = 16; } elsif( $input =~ m|.*30.*|){ $input = 4; $ltinput = 2; $gtinput = 8; } elsif( $input =~ m|.*31.*|){ $input = 2; $ltinput = 1; $gtinput = 4; } elsif( $input =~ m|.*32.*|){ $input = 1; $gtinput = 2; undef($ltinput); } my ($center, $left, $right) = ('','',''); my $subnet; #add the subnet masks if($input) { $subnet = 256 - $input; $center = qq(
255.255.255.$subnet
); } if($ltinput) { $subnet = 256 - $ltinput; $right = qq(
255.255.255.$subnet
); } if($gtinput) { $subnet = 256 - $gtinput; $left = qq(
255.255.255.$subnet
); } for(my $i = 0; $i < 256; $i++) { #left display -- one less than requested if(defined($gtinput) && $i % $gtinput == 0) { my $upper = $i + $gtinput -1; $left .= "x.x.x.$i - x.x.x.$upper
\n"; } #center display -- the one requested if($i % $input == 0 ) { my $upper = $i + $input - 1; $center .= "x.x.x.$i - x.x.x.$upper  
\n"; } #right display -- one more than requested if(defined($ltinput) && $i % $ltinput == 0) { my $upper = $i + $ltinput -1; $right .= "x.x.x.$i - x.x.x.$upper  
\n"; } } print qq( ); print "
Results for /$ltinput Results for /$input Results for /$gtinput
$left $center $right
\n"; print '
'; } sub parse_post { my $buffer; if ($ENV{'REQUEST_METHOD'} eq "GET") { $buffer=$ENV{'QUERY_STRING'} } elsif ($ENV{'REQUEST_METHOD'} eq 'POST' && $ENV{'CONTENT_TYPE'} eq "application/x-www-form-urlencoded") { read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); } else { $buffer = $ENV{'QUERY_STRING'}; $buffer || read(STDIN, $buffer, $ENV{CONTENT_LENGTH}); } my @pairs = split(/&/, $buffer); my %webvarLocal; foreach my $pair (@pairs) { my ($name, $value) = split(/=/, $pair); $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\'/\\\'/g; $webvarLocal{$name} = $value; } return %webvarLocal; } sub printAndExit { my $errStr = $_[0]; print qq(

$errStr

); exit(0); }