1 | #!/usr/bin/perl
|
---|
2 |
|
---|
3 | use strict;
|
---|
4 | use warnings;
|
---|
5 | use CGI::Carp qw(fatalsToBrowser);
|
---|
6 | use NetAddr::IP;
|
---|
7 | use CommonWeb qw(:ALL);;
|
---|
8 |
|
---|
9 | #file snCalc.cgi little subnet calculator app
|
---|
10 | use MyIPDB;
|
---|
11 |
|
---|
12 | my %webvar = parse_post();
|
---|
13 | my $input;
|
---|
14 |
|
---|
15 | print "Content-Type: text/html\n\n";
|
---|
16 |
|
---|
17 | open(HTML, "../startsn.html")|| die "Could not open startsn.html :$!";
|
---|
18 | my $start = join('', <HTML>);
|
---|
19 | close(HTML);
|
---|
20 | $start =~ s/\$\$WEBPATH\$\$/$IPDB::webpath/g;
|
---|
21 | print $start;
|
---|
22 |
|
---|
23 | # Clean up input so we don't divide by zero or something equally silly
|
---|
24 | if ($webvar{input} =~ m/(\d+)/) {
|
---|
25 | $input = 1*$1;
|
---|
26 | $input = 3 if $input < 3;
|
---|
27 | $input = 29 if $input > 29; # Not doing IPv6 yet...
|
---|
28 | } else {
|
---|
29 | $input = 29;
|
---|
30 | }
|
---|
31 |
|
---|
32 | my $ltinput = $input - 1;
|
---|
33 | my $gtinput = $input + 1;
|
---|
34 |
|
---|
35 | my $prenet = new NetAddr::IP "0.0.0.0/$ltinput";
|
---|
36 | my $net = new NetAddr::IP "0.0.0.0/$input";
|
---|
37 | my $postnet = new NetAddr::IP "0.0.0.0/$gtinput";
|
---|
38 |
|
---|
39 | print qq(<div class="center">
|
---|
40 | <table align="center" cellspacing="3" cellpadding="3">
|
---|
41 | <tr>
|
---|
42 | <td class="heading" align="center">Results for /$ltinput</td>
|
---|
43 | <td class="heading" align="center">Results for /$input</td>
|
---|
44 | <td class="heading" align="center">Results for /$gtinput</td>
|
---|
45 | </tr>
|
---|
46 | );
|
---|
47 |
|
---|
48 | print qq(<tr><td valign="top">\n).
|
---|
49 | qq( <div class="mask">).$prenet->mask."</div>\n".
|
---|
50 | qq( <div class="wildcard">).$prenet->wildcard."</div>\n".
|
---|
51 | getranges($ltinput).
|
---|
52 | qq(</td>\n<td valign="top" bgcolor="#d0e0e0">\n).
|
---|
53 | qq( <div class="mask">).$net->mask."</div>\n".
|
---|
54 | qq( <div class="wildcard">).$net->wildcard."</div>\n".
|
---|
55 | getranges($input).
|
---|
56 | qq(</td>\n<td valign="top">).
|
---|
57 | qq( <div class="mask">).$postnet->mask."</div>\n".
|
---|
58 | qq( <div class="wildcard">).$postnet->wildcard."</div>\n".
|
---|
59 | getranges($gtinput);
|
---|
60 |
|
---|
61 | print "</td></tr>\n</table>\n";
|
---|
62 |
|
---|
63 | print qq(<input type="button" value="Back" onclick="history.go(-1)" class="heading">
|
---|
64 | </div>
|
---|
65 | </body>
|
---|
66 | </html>
|
---|
67 | );
|
---|
68 |
|
---|
69 | # Just In Case
|
---|
70 | exit 0;
|
---|
71 |
|
---|
72 | # subs
|
---|
73 | sub xrange {
|
---|
74 | my $block = shift;
|
---|
75 | my $masklen = shift;
|
---|
76 | my $data = $block->range;
|
---|
77 | if ($masklen >= 24) {
|
---|
78 | $data =~ s/\b0\.0\.0\./x.x.x./g;
|
---|
79 | } elsif ($masklen >=16) {
|
---|
80 | $data =~ s/\b0\.0\.(\d+\.\d+)/x.x.$1/g;
|
---|
81 | } elsif ($masklen >=8) {
|
---|
82 | $data =~ s/\b0\.(\d+\.\d+\.\d+)/x.$1/g;
|
---|
83 | }
|
---|
84 | return $data;
|
---|
85 | } # xrange()
|
---|
86 |
|
---|
87 |
|
---|
88 | sub getranges {
|
---|
89 | my $masklen = shift;
|
---|
90 | my $ret = '';
|
---|
91 | my $super;
|
---|
92 | if ($masklen < 8) {
|
---|
93 | $super = new NetAddr::IP "0.0.0.0/0";
|
---|
94 | } elsif ($masklen < 16) {
|
---|
95 | $super = new NetAddr::IP "0.0.0.0/8";
|
---|
96 | } elsif ($masklen < 24) {
|
---|
97 | $super = new NetAddr::IP "0.0.0.0/16";
|
---|
98 | } else {
|
---|
99 | $super = new NetAddr::IP "0.0.0.0/24";
|
---|
100 | }
|
---|
101 | foreach my $net ($super->split($masklen)) {
|
---|
102 | $ret .= "\t".xrange($net,$masklen)."<br />\n";
|
---|
103 | }
|
---|
104 | return $ret;
|
---|
105 | } # getranges()
|
---|