-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiplist-cgi.pl
107 lines (92 loc) · 2.58 KB
/
iplist-cgi.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/perl -wT
# CGI script for listing the IP addresses contained in a CIDR netblock.
# Written by Rayner Lucas, March 2005.
BEGIN {
my $base_module_dir = (-d '/home/magiccoo/perl' ? '/home/magiccoo/perl' : ( getpwuid($>) )[7] . '/perl/');
unshift @INC, map { $base_module_dir . $_ } @INC;
}
use CGI;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use Net::CIDR qw(:all);
use strict;
my $cgi = new CGI;
my $w = $cgi->param('ipw');
my $x = $cgi->param('ipx');
my $y = $cgi->param('ipy');
my $z = $cgi->param('ipz');
my $mask = $cgi->param('mask');
my $submit;
print $cgi->header;
print $cgi->start_html;
# Argument checking:
my $validate_fail = 0;
foreach my $octet ($w, $x, $y, $z) {
if (($octet !~ /^\d+$/) || ($octet < 0) || ($octet > 255)) {
$validate_fail = 1;
}
}
if ($mask !~ /^\d+$/) {
$validate_fail = 1;
}
# If the things that are meant to be numbers weren't numbers, complain.
if ($validate_fail) {
print "Invalid address or netmask.<br><br>";
print "<a href=\"../iplist.html\">Try again</a>";
print $cgi->end_html;
exit;
}
# Check that the address is valid.
if (!cidrvalidate("$w.$x.$y.$z")) {
print "Address specified was not valid.<br><br>";
print "<a href=\"../iplist.html\">Try again</a>";
print $cgi->end_html;
exit;
}
# Check netmask is within the limits of server resources and
# mathematical possibility.
if ($mask < 16 || $mask > 32) {
print "Netmask must be a value from 16 to 32.";
print "<a href=\"../iplist.html\">Try again</a>";
print $cgi->end_html;
exit;
}
# Right, lets get to work.
my $num = 2**(32 - $mask);
print "$num addresses<br><br>";
my @cidr_list = ("$w.$x.$y.$z/$mask");
my @range_list = cidr2range(@cidr_list);
print "IP address range: ";
print $range_list[0];
print "<br><br>";
# Find the starting address in the netblock and store it in $w, $x, $y, $z.
my @addresses = split(/\-/, $range_list[0]);
my @octets = split(/\./, $addresses[0]);
$w = $octets[0];
$x = $octets[1];
$y = $octets[2];
$z = $octets[3];
# Count up through the addresses and print each one.
for (my $count = 2**(32 - $mask); $count > 0; $count--) {
# If the other error checks work properly, this error should never happen.
if ($w > 255) {
print "Invalid address/mask specified: leftmost octet would be greater than 255.";
print $cgi->end_html;
exit;
}
print "$w.$x.$y.$z<br>";
$z++;
if ($z > 255) {
$y++;
$z = 0;
}
if ($y > 255) {
$x++;
$y = 0;
}
if ($x > 255) {
$w++;
$x = 0;
}
}
print $cgi->end_html;
exit;