source: trunk/cgi-bin/extras/db2rwhois.pl@ 311

Last change on this file since 311 was 311, checked in by Kris Deugau, 19 years ago

/trunk

Copy db2rwhois.pl from /branches/stable.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author
File size: 5.0 KB
Line 
1#!/usr/bin/perl
2# -T
3# ipdb/cgi-bin/extras/db2rwhois.pl
4# Pull data from ipdb and mangle it into RWHOIS
5# Initial version 03/26/2004 kdeugau against IPDB v1
6###
7# Revision info
8# $Date: 2006-03-28 20:27:06 +0000 (Tue, 28 Mar 2006) $
9# SVN revision $Rev: 311 $
10# Last update by $Author: kdeugau $
11###
12# Copyright (C) 2004,2005 - Kris Deugau
13
14use strict;
15use warnings;
16use DBI;
17use NetAddr::IP;
18use MyIPDB;
19
20$ENV{"PATH"} = "/bin;/usr/bin";
21
22my $rwhoisDataPath = "/etc/rwhoisd";
23
24my ($dbh,$msg) = connectDB_My;
25
26# For WHOIS purposes this may not be very useful. YMMV, we'll see.
27#initIPDBGlobals($dbh);
28
29my @masterblocks;
30
31# Fill in data about our master blocks as allocated from ARIN
32# We open separate files for each of these as appropriate.
33# Note that this ASS-U-MEs that we do not add master IP blocks-
34# there should probably be a separate system for doing that.
35my $sth = $dbh->prepare("select cidr,ctime from masterblocks;");
36$sth->execute;
37my $i=0;
38GETMASTERS: while (my @data = $sth->fetchrow_array()) {
39
40# Techically, we only need to exclude 204.138.172.0/24, as we "own" all of the other blocks.
41# However, 205.207.184.0/23 and 206.130.64.0/24 are, um, awkward.
42 if ($data[0] =~ /^(192.168.0.0|172.16.0.0|10.0.0.0|20[456])/) {
43 next GETMASTERS;
44 }
45 $masterblocks[$i] = new NetAddr::IP $data[0];
46 my ($ctime,undef) = split /\s/, $data[1];
47
48print "$masterblocks[$i] $data[1]\n";
49
50 my $date;
51 chomp ($date = `/bin/date +"%Y-%m-%d"`);
52
53# Whew! Ugly little varmint.
54 my $masterfilename = "net-".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.
55 "/data/network/".$masterblocks[$i]->addr."-".$masterblocks[$i]->masklen.".txt";
56
57# Need check here to create tree for netblock?
58
59 open MASTERFILE,">$rwhoisDataPath/$masterfilename";
60
61 print MASTERFILE "ID: NETBLK-ISP.$masterblocks[$i]\n".
62 "Auth-Area: $masterblocks[$i]\n".
63 "Network-Name: ISP-".$masterblocks[$i]->network."\n".
64 "IP-Network: $masterblocks[$i]\n".
65 "IP-Network-Block: ".$masterblocks[$i]->range."\n".
66 "Organization: Friendly ISP\n".
67 "Tech-Contact: noc\@example.com\n".
68 "Admin-Contact: ISP-ARIN-HANDLE\n".
69 "Abuse-Contact: abuse\@example.com\n".
70 "Created: $ctime\n".
71 "Updated: $date\n".
72 "Updated-By: noc\@example.com\n";
73
74 close MASTERFILE;
75 $i++;
76}
77
78# Now read out the data in the "main" delegation list, and check it
79# with the master blocks. We need to do this to decide which rWHOIS
80# "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.
81
82# Make sure to remove the private netblocks from this.
83# No use or point in broadcasting our use of them.
84# Also remove the details of our "reserved CORE/WAN" blocks; they're not critical.
85$sth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp ".
86 "from allocations where ".
87 "not (cidr <<= '192.168.0.0/16') and ".
88 "not (cidr <<= '172.16.0.0/12') and ".
89 "not (cidr <<= '10.0.0.0/8') and ".
90 "not (type = 'wr') and ".
91 "masklen(cidr) <=30");
92#" and (custid='6750400' or custid like '%-RES' or custid like '%-BUS')");
93$sth->execute;
94
95$i=0;
96while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime) = $sth->fetchrow_array) {
97
98# We get master block info from @masterblocks.
99 # ID: NETBLK-ISP.10.0.0.0/8
100 # Auth-Area: 10.0.0.0/8
101 # Network-Name: ISP-10.0.2.144
102 # IP-Network: 10.0.2.144.144/29
103 # IP-Network-Block: 10.0.2.144 - 10.0.2.151
104 # Organization: WidgetCorp
105 # Tech-Contact: bob@widgetcorp.com
106 # Admin-Contact: ISP-ARIN-HANDLE
107 # Created: 20040314
108 # Updated: 20040314
109 # Updated-By: noc@example.com
110
111 # Get the "full" network number
112 my $net = new NetAddr::IP $cidr;
113
114# Assumptions: All data in ipdb is public
115# If not, we need another field to indicate "public/private".
116
117 foreach my $master (@masterblocks) {
118 if ($master->contains($net)) {
119
120# Whew! Ugly little varmint.
121 my $masterfilename = "net-".$master->addr."-".$master->masklen.
122 "/data/network/".$master->addr."-".$master->masklen.".txt";
123
124 open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
125 or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
126
127# cidr custid type city description notes maskbits
128
129# Fill in a generic entry for nameless allocations
130if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
131
132 # Fix up datestamps. We don't *really* need sub-microsecond resolution on our exports...
133 ($ctime) = ($ctime =~ /^(\d+-\d+-\d+)\s+/);
134 ($mtime) = ($mtime =~ /^(\d+-\d+-\d+)\s+/);
135
136 print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
137 "Auth-Area: $master\n".
138 "Network-Name: ISP-".$net->network."\n".
139 "IP-Network: $net\n".
140 "IP-Network-Block: ".$net->range."\n".
141 "Organization: $desc\n".
142# "Tech-Contact: $data[9]\n".
143 "Tech-Contact: abuse\@example.com\n".
144 "Admin-Contact: ISP-ARIN-HANDLE\n".
145 "Created: $ctime\n".
146 "Updated: $mtime\n".
147 "Updated-By: noc\@example.com\n";
148 }
149 }
150
151
152
153 # print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
154 # "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\t| $data[9]\n";
155 # print "$data[0]\t| $data[1]\t| $data[2]\t| $data[3]\t| $data[4]\t| ".
156 # "$data[5]\t| $data[6]\t| $data[7]\t| $data[8]\n";
157 $i++;
158} # while fetchrow_array()
159
160
161$dbh->disconnect;
Note: See TracBrowser for help on using the repository browser.