Changeset 15


Ignore:
Timestamp:
Oct 25, 2004, 5:32:05 PM (18 years ago)
Author:
Kris Deugau
Message:

Updated db2rwhois.pl for new database format

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/cgi-bin/extras/db2rwhois.pl

    r2 r15  
    1 #!/usr/bin/perl -W -T
    2 # db2rwhois.pl
     1#!/usr/bin/perl
     2# -T
     3# ipdb/cgi-bin/extras/db2rwhois.pl
    34# Pull data from ipdb and mangle it into RWHOIS
    4 # Initial version 03/26/2004 kdeugau
     5# Initial version 03/26/2004 kdeugau against IPDB v1
     6###
     7# Revision info
     8# $Date$
     9# SVN revision $Rev$
     10# Last update by $Author$
     11###
    512
     13use strict;
     14use warnings;
    615use DBI;
    716use NetAddr::IP;
     17use IPDB qw(:ALL);
    818
    919$ENV{"PATH"} = "/bin;/usr/bin";
     20
     21my $rwhoisDataPath = "/etc/rwhoisd";
    1022
    1123# We'll pull this out of the database instead.  Just make sure our
    1224# master blocks are actually listed....  <g>
    1325#@masterblocks = (
    14 #               (new NetAddr::IP "10.0.0.0/8"),
    15 #               (new NetAddr::IP "172.16.0.0/12"),
    16 #               (new NetAddr::IP "192.168.0.0/16")
     26#               (new NetAddr::IP "209.91.128.0/18"),
     27#               (new NetAddr::IP "66.186.64.0/19"),
     28#               (new NetAddr::IP "192.168.99.0/24")
    1729#               );
    1830
    19 $dbh = DBI->connect("dbi:mysql:ipdb", "root", "")
    20   or die $DBI::errstr;
     31my $dbh = connectDB;
     32
     33my @masterblocks;
    2134
    2235# Fill in data about our master blocks as allocated from ARIN
     
    2437# Note that this ASS-U-MEs that we do not add master IP blocks-
    2538# there should probably be a separate system for doing that.
    26 $sth = $dbh->prepare("select * from masterblocks;");
     39my $sth = $dbh->prepare("select * from masterblocks;");
    2740$sth->execute;
    28 $i=0;
    29 while (@data = $sth->fetchrow_array()) {
    30   # Get the "full" network number+CIDR mask
    31   $cidr = $data[0].".".$data[2].$data[1];
     41my $i=0;
     42GETMASTERS: while (my @data = $sth->fetchrow_array()) {
    3243
    33   $masterblocks[$i] = new NetAddr::IP $cidr;
     44  if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0)/) {
     45    next GETMASTERS;
     46  }
     47  $masterblocks[$i] = new NetAddr::IP $data[0];
    3448
     49print "$masterblocks[$i]\n";
     50
     51  my $date;
    3552  chomp ($date = `/bin/date +"%Y%m%d"`);
    3653
    3754# Whew!  Ugly little varmint.
    38   $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
     55  my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
    3956    "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
    4057
    41   open MASTERFILE,">$masterfilename";
     58# Need check here to create tree for netblock?
     59
     60  open MASTERFILE,">$rwhoisDataPath/$masterfilename";
    4261
    4362  print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
     
    4665        "IP-Network: $masterblocks[$i]\n".
    4766        "IP-Network-Block: ".$masterblocks[$i]->range."\n".
    48         "Organization: $data[3]\n".
    49         "Tech-Contact: $data[9]\n".
     67        "Organization: Friendly ISP\n".
     68        "Tech-Contact: noc\@example.com\n".
    5069        "Admin-Contact: ISP-ARIN-HANDLE\n".
    51         "Created: 20040401000000000\n".
     70        "Abuse-Contact: abuse\@example.com\n".
     71        "Created: 20040308\n".
    5272        "Updated: $date\n".
    5373        "Updated-By: noc\@example.com\n";
     
    6181# "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.
    6282
    63 #$sth = $dbh->prepare("select * from blocks;");
    64 $sth = $dbh->prepare("select * from tmpblocks;");
     83# Make sure to remove the private netblocks from this.
     84# No use or point in broadcasting our use of them.
     85$sth = $dbh->prepare("select * from allocations where not (cidr <<= '192.168.0.0/16') and not (cidr <<= '172.16.0.0/12') and not (cidr <<= '10.0.0.0/8') and custid='6750400'");
    6586$sth->execute;
    6687
    67 while (@data = $sth->fetchrow_array()) {
     88while (my @data = $sth->fetchrow_array()) {
    6889
    6990# We get master block info from @masterblocks.
     
    81102
    82103  # Get the "full" network number
    83   $netnum = $data[0].".".$data[2];
    84 
    85   $cidr = $netnum.$data[1];
    86 
    87   $net = new NetAddr::IP $cidr;
     104  my $net = new NetAddr::IP $data[0];
    88105
    89106# Assumptions:  All data in ipdb is public
    90107# If not, we need another field to indicate "public/private".
    91108
    92   foreach $master (@masterblocks) {
     109  foreach my $master (@masterblocks) {
    93110    if ($master->contains($net)) {
    94111
    95112# Whew!  Ugly little varmint.
    96       $masterfilename = "net-".$master->addr."-".$master->masklen.
     113      my $masterfilename = "net-".$master->addr."-".$master->masklen.
    97114        "/data/network/".$master->addr."-".$master->masklen.".txt";
    98115
    99       open MASTERFILE,">>$masterfilename";
     116      open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
     117        or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
    100118
     119# cidr custid type city description notes maskbits
     120
     121# Creation date in record to eventually be "correct";  near-term will
     122# be master's creation date;  immediate is today's date.
     123my $date;
    101124      chomp ($date = `/bin/date +"%Y%m%d"`);
     125if ($data[4] =~ /^\s*$/) { $data[4] = 'Friendly ISP'; }
    102126      print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
    103127        "Auth-Area: $master\n".
     
    105129        "IP-Network: $net\n".
    106130        "IP-Network-Block: ".$net->range."\n".
    107         "Organization: $data[5]\n".
     131        "Organization: $data[4]\n".
    108132#       "Tech-Contact: $data[9]\n".
    109133        "Tech-Contact: abuse\@example.com\n".
    110134        "Admin-Contact: ISP-ARIN-HANDLE\n".
    111         "Created: <date from db> (or today's date?)\n".
     135        "Created: $date\n".
    112136        "Updated: $date\n".
    113137        "Updated-By: noc\@example.com\n";
Note: See TracChangeset for help on using the changeset viewer.