SHARE
TWEET

Untitled

a guest Jun 19th, 2017 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. #This script generates PTR records for use as our defaults. Use primarily to "undo" customer PTR records if requested, or if reclaimed
  3. #By Sprayberry
  4. #Bug reports or problems should go to asprayberry@theplanet
  5. #I am aware that this script could do with some optimizing, but I care more about having something that works.
  6. #If you give the script data that is not an IP/CIDR range or a netmask in the proper format and it gives you garbage or an error, that is not a bug. That is USER ERROR. If you cannot figure out the right data to give this script, become an hero
  7.  
  8. #If these modules are not installed, first: PANIC AND RUN AWAY. Second: Pull yourself together and go bug your administrator
  9. use strict;
  10. use warnings;
  11. use Net::Netmask;
  12. use Getopt::Long;
  13. use Data::Validate::IP qw/is_ipv4/;
  14.  
  15. #setting empty values for possible command line options
  16. my $mask = '';
  17. my $help = '';
  18.  
  19. #set valid command line arguments
  20. GetOptions('mask|m=s' => \$mask, 'help|h' => \$help);
  21.  
  22. #print help if the help variable is true(boolean)
  23. help() if ($help);
  24.  
  25. #if no arguments are supplied, the help prompt is displayed
  26. if ( @ARGV == 0 || @ARGV eq "" ) {
  27.     print "------------------------------------\nNO ARGUMENTS!!\nPrinting Help....\n------------------------------------\n";
  28.     help();
  29. }
  30.  
  31. #shifts first argument out of the @ARGV array and into the $IP variable
  32. my $ip = shift;
  33.  
  34. #checks to see if the IP supplied is actually a CIDR. If the IP is a CIDR, it generates the mask from the CIDR, validates the IP, and generates the records based on the mask
  35. if ($ip =~ m/^[\d.]+\/\d{1,2}/) {
  36.     my @testip = split(/\//, $ip);
  37.     my $block = new Net::Netmask($ip);
  38.     validate($testip[0]);
  39.     $mask = $block->mask();
  40.     my $record = record($block->base());
  41.     my $first = trim($block->first());
  42.     my $last = trim($block->last());
  43.     genrecords($record, $first, $last);
  44. }
  45.  
  46. #checks if the mask consists of numbers or periods. If no match, it creates records for the entire C block of the IP specified
  47. if ($mask !~ m/[\d.]+/) {
  48.     validate($ip);
  49.     genrecords(record($ip), 0, 255);
  50.     exit;
  51. }
  52. #if a mask is valid and supplied, it will generate records based on the netmask provided
  53. else {
  54.     validate($ip);
  55.     validate($mask);
  56.     my $block = new Net::Netmask($ip, $mask);
  57.     my $record = record($block->base());
  58.         my $first = trim($block->first());
  59.         my $last = trim($block->last());
  60.     genrecords($record, $first, $last);
  61. }
  62.  
  63. #this generates the record template to be used. The template is the complete record save for the last octet in use, which is unique for each record. The complete record is generated in the function below
  64. sub record {
  65.     my $ip = shift;
  66.     my ($first, $second, $third) = split(/\./, $ip);
  67.     my $convention = "static.theplanet.com.";
  68.     my $zone = "\." . sprintf("%x", $third) . "." . sprintf("%x", $second ) . sprintf("%x", $first) . "\.";
  69.     my $template = $zone . $convention;
  70.     return($template);
  71. }
  72.  
  73.  
  74. #this part actualy prints the records to the user
  75. sub genrecords {
  76.     my $record = shift;
  77.     my $first = shift;
  78.     my $last = shift;
  79.     $last++;
  80.     until ( $first == $last ) {
  81.     print "$first\tIN\tPTR\t" . sprintf("%x", $first) . $record . "\n";
  82.     $first++;
  83.     }
  84.     exit;
  85. }
  86.  
  87.  
  88. #gets last octect of given IP
  89. sub trim {
  90.     my $ip = shift;
  91.     my @octets = split(/\./, $ip);
  92.     return($octets[3]);
  93. }
  94.  
  95.  
  96. #makes sure that the IP addresses supplied are VALID, so the script does not produce garbage records.
  97. sub validate{
  98.     my $ip = shift;
  99.     if (is_ipv4($ip)) {
  100.         return();
  101.     }
  102.     else {
  103.         print "------------------------------------\n$ip is NOT a valid IP or netmask.\n\nPlease do the needful and check the same.\nPerhaps you should read the documentation?\n$0 -help\n------------------------------------\n";
  104.         exit;
  105.     }
  106. }
  107.  
  108. #prints somewhat useful information on using the script. RTFM!
  109. sub help{
  110.     print <<"EOF";
  111. This script will take three different kinds of arguments. The records are printed in DNS record format.
  112.  
  113. EG:
  114.  
  115. 196     IN      PTR     c4.0.00.static.theplanet.com.
  116.  
  117. Examples of using the script are as follows:
  118.  
  119. ------------
  120. IP address:
  121.  
  122. $0 123.123.123.123
  123.  
  124. Supplying a plain IP address will generate records for the entire C block.
  125. ------------
  126. CIDR range:
  127.  
  128. $0 123.123.123.123/24
  129.  
  130. This will generate PTR records for the block specified
  131. ------------
  132. If you want to generate records based on a bare IP in combination with a netmask, you will have to use the mask flag:
  133.     -mask OR -m
  134.  
  135. $0 123.123.123.123 -mask 255.255.255.248
  136. $0 123.123.123.123 -m 255.255.255.248
  137.  
  138. ############
  139. This script is built to accept IP addresses and ranges in the C Class ONLY! Not tested or built for dealing with A or B class blocks.
  140. ############
  141.  
  142. EOF
  143.  
  144. exit;
  145. }
  146.  
  147. 1;
RAW Paste Data
Top