Advertisement
savsanta

iplist.php

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