{"id":223,"date":"2009-03-09T01:26:11","date_gmt":"2009-03-09T07:26:11","guid":{"rendered":"http:\/\/gregsowell.com\/?p=223"},"modified":"2009-02-21T10:08:24","modified_gmt":"2009-02-21T16:08:24","slug":"ipplan-and-rwhois-integration-script","status":"publish","type":"post","link":"https:\/\/gregsowell.com\/?p=223","title":{"rendered":"IPPlan and RWhois Integration Script"},"content":{"rendered":"<p>Pull all of your records from IPPlan and insert them into your RWhois server automatically.<\/p>\n<p>If you are an ISP or large enterprise you will need to keep up with your addressing and assignments somehow, right?  There are only a handful of products out there that will do this.  The company I work for is an ISP, so my favorite address manager is <a title=\"ipplan\" href=\"http:\/\/iptrack.sourceforge.net\/\" target=\"_blank\">IPPlan<\/a>.  It is really tailored to ISPs.<\/p>\n<p>It&#8217;s:<\/p>\n<ul>\n<li>Web-based<\/li>\n<li>Opensource\/Free<\/li>\n<li>Easy to use<\/li>\n<li>Ugly graphics, not that I mind!<\/li>\n<\/ul>\n<p>If you are an ISP, in the USA, then you own a chunk of addressing from <a title=\"arin\" href=\"http:\/\/arin.net\" target=\"_blank\">ARIN<\/a>.  When handing this addressing out, you are required by ARIN to add an entry either to their Rwhois server or your own for every allocation that is larger than a \/30.  So, if you hand someone a block of 8 or larger, you have to have an entry.  On top of that, they want you to have the updates in within 7 days of the allocation.  Keeping up with this can be cumbersome.  IPPlan can generate Swips to email to ARIN for updating, but you have to create a new &#8220;customer&#8221; in IPPlan every time you want to do an allocation for the swips to work properly&#8230;a HUGE pain.<\/p>\n<p>ARIN has a nice little<a title=\"rwhois\" href=\"http:\/\/projects.arin.net\/rwhois\/\" target=\"_blank\"> RWhois<\/a> server you can run on your own server to manage your reverse records.  It is all command line and there is no convenient way to administer this guy.<\/p>\n<p>My solution:<\/p>\n<p>I wrote a PHP script that queries the IPPlan database for each allocation that is larger than a \/30.  It takes the allocation and the description field and builds the text files necessary for the RWhois server and saves them to a folder on itself.  Rather than creating a new customer for each allocation, I just use the description field; this cuts down a two minute process to about 10 seconds.  Then, it deletes the old RWhois files on the RWhois server and FTPs the new ones over.  The RWhois service is then restarted and it indexes all the new information.  I schedule all this to run once a week to be in compliance with ARIN.<\/p>\n<p>For all the code and details, click below!!!<br \/>\n<!--more--><\/p>\n<h2>IPPlan Server<\/h2>\n<p>I&#8217;m having all the config files go into the \/root\/rwhois folder.  They will end up in folders called net1, net2 and so on.  The supernet will have a file that you will have to create yourself and place inside the rwhois folder.  The delete script on the rwhois server will delete all files, and this supernet file isn&#8217;t auto generated by the script, so you need to create it so the script can copy it at run time.<\/p>\n<p>Supernet file looks like the following (200.0.0.0-21.txt):<\/p>\n<blockquote><p>ID: NETBLK-MNSL-200.0.0.0\/21<br \/>\nAuth-Area: 200.0.0.0\/21<br \/>\nNetwork-Name: MNS-BLK-1-200.0.0.0\/21<br \/>\nIP-Network: 200.0.0.0\/21<br \/>\nIP-Network-Block: 200.0.0.0 &#8211; 200.0.7.255<br \/>\nOrganization: GregSowell, Inc.<br \/>\nTech-Contact: dnsadmin@gregsowell.com<br \/>\nAdmin-Contact: DNSADGregSowell-ARIN<\/p><\/blockquote>\n<p>You will have to adjust the files for your information.  You need one of these for each of the allocations you have from ARIN.  This is the only file you have to generate, the script will produce all of the smaller ones.<\/p>\n<p>This script does all the work for us.  He parses all the DB info.  You will need to replace all of my information with yours.<\/p>\n<p><code lang=\"php\"><br \/>\n<?php\n\/\/#########ipplan-rwhoisd.php#########\n\n\/\/database login info\n$dbhost = 'localhost';\n$dbuser = 'user';\n$dbpass = 'password';\n\n\/\/connect to db\n$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');\n\n\/\/ipplan database name\n$dbname = 'ipplan';\nmysql_select_db($dbname);\n\n\/\/setup our query\n$query  = \"select baseaddr, subnetsize, descrip from base;\";\n$result = mysql_query($query);\n\n\/\/make the base folder and teh subfolders for our config files\nmkdir(\"\/root\/rwhois\");\nmkdir(\"\/root\/rwhois\/net1\");\nmkdir(\"\/root\/rwhois\/net2\");\nmkdir(\"\/root\/rwhois\/net3\");\nmkdir(\"\/root\/rwhois\/net4\");\nmkdir(\"\/root\/rwhois\/net5\");\n\n\/\/loop through returned results\nwhile($row = mysql_fetch_array($result, MYSQL_ASSOC))\n{\n$netFolder = \"\";\n$ipAddr = \"\";\n$ipAddrEnd = \"\";\n\n\/\/if it is larger than a \/30 then continue\nif ($row['subnetsize'] > 4) {<br \/>\n  \/\/One for each of your supernets<br \/>\n  \/\/this is in the format of starting IP address, then ending IP address<br \/>\n  if ($row['baseaddr'] > ip2long(\"200.0.0.0\") and $row['baseaddr'] < ip2long(\"200.0.7.255\")) {\/\/net1\n    $netFolder = \"\/root\/rwhois\/net1\/\";\n  }\n  if ($row['baseaddr'] > ip2long(\"210.0.0.0\") and $row['baseaddr'] < ip2long(\"210.0.15.255\")) {\/\/net2\n    $netFolder = \"\/root\/rwhois\/net2\/\";\n  }\n  if ($row['baseaddr'] > ip2long(\"220.0.0.0\") and $row['baseaddr'] < ip2long(\"220.0.7.255\")) {\/\/net3\n    $netFolder = \"\/root\/rwhois\/net3\/\";\n  }\n  if ($row['baseaddr'] > ip2long(\"230.0.0.0\") and $row['baseaddr'] < ip2long(\"230.0.7.255\")) {\/\/net4\n    $netFolder = \"\/root\/rwhois\/net4\/\";\n  }\n  if ($row['baseaddr'] > ip2long(\"240.0.0.0\") and $row['baseaddr'] < ip2long(\"240.0.15.255\")) {\/\/net5\n    $netFolder = \"\/root\/rwhois\/net5\/\";\n  }\n  \/\/find subnet size\n  if ($row['subnetsize'] == 8) {\n    $subnet = \"29\";\n  }\n  if ($row['subnetsize'] == 16) {\n    $subnet = \"28\";\n  }\n  if ($row['subnetsize'] == 32) {\n    $subnet = \"27\";\n  }\n  if ($row['subnetsize'] == 64) {\n    $subnet = \"26\";\n  }\n  if ($row['subnetsize'] == 128) {\n    $subnet = \"25\";\n  }\n  if ($row['subnetsize'] == 256) {\n    $subnet = \"24\";\n  }\n  if ($row['subnetsize'] == 512) {\n    $subnet = \"23\";\n  }\n  if ($row['subnetsize'] == 1024) {\n    $subnet = \"22\";\n  }\n  if ($row['subnetsize'] == 2048) {\n    $subnet = \"21\";\n  }\n  if ($row['subnetsize'] == 4096) {\n    $subnet = \"20\";\n  }\n  \/\/this is the formatting for the files\n  $ipAddr = long2ip($row['baseaddr']);\n  $ipAddrEnd = long2ip($row['baseaddr'] + $row['subnetsize'] - 1);\n  $fp = fopen($netFolder . $ipAddr . \"-\" . $subnet . \".txt\", 'w');\n  \/\/change the ID to equal what you want\n  fwrite($fp, 'ID: NETBLK-GregSowell-' . $ipAddr . '\/' . $subnet . \"\\n\");\n  fwrite($fp, 'Auth-Area: ' . $ipAddr . '-' . $subnet . \"\\n\");\n  fwrite($fp, 'Network-Name: ' . $row['descrip'] . \"\\n\");\n  fwrite($fp, 'IP-Network: ' . $ipAddr . '\/' . $subnet . \"\\n\");\n  fwrite($fp, 'IP-Network-Block: ' . $ipAddr . \" - \" . $ipAddrEnd . \"\\n\");\n  \/\/change the org info to yours\n  fwrite($fp, 'Organization: GregSowell.com' . \"\\n\");\n  \/\/change the contact info to yours\n  fwrite($fp, 'Tech-Contact: dnsadmin@GregSowell.com' . \"\\n\");\n  \/\/change this to your admin contact\n  fwrite($fp, 'Admin-Contact: DNSADGregSowell-ARIN' . \"\\n\");\n  fclose($fp);\n}\n} \n\nmysql_close($conn);\n\n\n\/\/folders and files created, now let's send them to the server\n$ftp_server = \"1.1.1.1\";\n$ftp_user_name = \"user\";\n$ftp_user_pass = \"password\";\n\n\/\/ set up basic connection\n$conn_id = ftp_connect($ftp_server)  or die(\"Couldn't connect to $ftp_server\");\n\n\/\/ login with username and password\n$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);\necho $login_result . \"\\n\";\n\nftp_pasv($conn_id, true);\n\n\/\/set local dir where config files are sitting\n$dir = \"\/root\/rwhois\/net1\/\";\n\/\/set remote directory where files will be placed\n\/\/this will be the name of the subnet\n$remoteDir = \"\/usr\/local\/rwhoisd\/net-200.0.0.0-21\/data\/network\/\";\n\/\/ftp_chdir($conn_id, $remoteDir);\nif (ftp_chdir($conn_id, $remoteDir)) {\n    echo \"Current directory is now: \" . ftp_pwd($conn_id) . \"\\n\";\n} else { \n    echo \"Couldn't change directory\\n\";\n}\nif (is_dir($dir)) {\n\t\/\/place our supernet file in.  This will be located in our local rwhois directory and will never change.  You will have to create\n\t\/\/one of these for each of your zones.\n    ftp_put($conn_id, \"200.0.0.0.txt\", \"\/root\/rwhois\/200.0.0.0-21.txt\", FTP_BINARY);\n    if ($dh = opendir($dir)) {\n\t\t\/\/loop through the base dir and upload the files\n        while (($file = readdir($dh)) !== false) {\n\t\t$fileSend = $dir . $file;\n\t\t$remote_file = $file;\n\t\t\/\/ upload a file\n\t\tif (ftp_put($conn_id, $remote_file, $fileSend, FTP_BINARY)) {\n\t\t \/\/echo \"successfully uploaded $file\\n\";\n\t\t} else {\n\t\t echo \"There was a problem while uploading $file\\n\";\n\t\t}\n\t\t\/\/delete the file we just sent\n\t\tunlink($fileSend);\n        }\n        closedir($dh);\n    }\n}\n\/\/now we move to the next dir\n$dir = \"\/root\/rwhois\/net2\/\";\n$remoteDir = \"\/usr\/local\/rwhoisd\/net-210.0.0.0-20\/data\/network\/\";\nif (ftp_chdir($conn_id, $remoteDir)) {\n    echo \"Current directory is now: \" . ftp_pwd($conn_id) . \"\\n\";\n} else { \n    echo \"Couldn't change directory\\n\";\n}\nif (is_dir($dir)) {\n    ftp_put($conn_id, \"210.0.0.0-20.txt\", \"\/root\/rwhois\/210.0.0.0-20.txt\", FTP_BINARY);\n    if ($dh = opendir($dir)) {\n        while (($file = readdir($dh)) !== false) {\n            \/\/echo $file;\n\t\t$fileSend = $dir . $file;\n\t\t$remote_file = $file;\n\t\t\/\/ upload a file\n\t\tif (ftp_put($conn_id, $remote_file, $fileSend, FTP_BINARY)) {\n\t\t \/\/echo \"successfully uploaded $file\\n\";\n\t\t} else {\n\t\t echo \"There was a problem while uploading $file\\n\";\n\t\t}\n\t\tunlink($fileSend);\n        }\n        closedir($dh);\n    }\n}\n$dir = \"\/root\/rwhois\/net3\/\";\n$remoteDir = \"\/usr\/local\/rwhoisd\/net-220.0.0.0-21\/data\/network\/\";\n\/\/ftp_chdir($conn_id, $remoteDir);\nif (ftp_chdir($conn_id, $remoteDir)) {\n    echo \"Current directory is now: \" . ftp_pwd($conn_id) . \"\\n\";\n} else { \n    echo \"Couldn't change directory\\n\";\n}\nif (is_dir($dir)) {\n    ftp_put($conn_id, \"220.0.0.0-21.txt\", \"\/root\/rwhois\/220.0.0.0-21.txt\", FTP_BINARY);\n    if ($dh = opendir($dir)) {\n        while (($file = readdir($dh)) !== false) {\n            \/\/echo $file;\n\t\t$fileSend = $dir . $file;\n\t\t$remote_file = $file;\n\t\t\/\/ upload a file\n\t\tif (ftp_put($conn_id, $remote_file, $fileSend, FTP_BINARY)) {\n\t\t \/\/echo \"successfully uploaded $file\\n\";\n\t\t} else {\n\t\t echo \"There was a problem while uploading $file\\n\";\n\t\t}\n\t\tunlink($fileSend);\n        }\n        closedir($dh);\n    }\n}\n$dir = \"\/root\/rwhois\/net4\/\";\n$remoteDir = \"\/usr\/local\/rwhoisd\/net-230.0.0.0-21\/data\/network\/\";\nif (ftp_chdir($conn_id, $remoteDir)) {\n    echo \"Current directory is now: \" . ftp_pwd($conn_id) . \"\\n\";\n} else { \n    echo \"Couldn't change directory\\n\";\n}\nif (is_dir($dir)) {\n    ftp_put($conn_id, \"230.0.0.0-21.txt\", \"\/root\/rwhois\/230.0.0.0-21.txt\", FTP_BINARY);\n    if ($dh = opendir($dir)) {\n        while (($file = readdir($dh)) !== false) {\n\t\t$fileSend = $dir . $file;\n\t\t$remote_file = $file;\n\t\t\/\/ upload a file\n\t\tif (ftp_put($conn_id, $remote_file, $fileSend, FTP_BINARY)) {\n\t\t \/\/echo \"successfully uploaded $file\\n\";\n\t\t} else {\n\t\t echo \"There was a problem while uploading $file\\n\";\n\t\t}\n\t\tunlink($fileSend);\n        }\n        closedir($dh);\n    }\n}\n$dir = \"\/root\/rwhois\/net5\/\";\n$remoteDir = \"\/usr\/local\/rwhoisd\/net-240.0.0.0-20\/data\/network\/\";\nif (ftp_chdir($conn_id, $remoteDir)) {\n    echo \"Current directory is now: \" . ftp_pwd($conn_id) . \"\\n\";\n} else { \n    echo \"Couldn't change directory\\n\";\n}\nif (is_dir($dir)) {\n    ftp_put($conn_id, \"240.0.0.0-20.txt\", \"\/root\/rwhois\/240.0.0.0-20.txt\", FTP_BINARY);\n    if ($dh = opendir($dir)) {\n        while (($file = readdir($dh)) !== false) {\n\t\t$fileSend = $dir . $file;\n\t\t$remote_file = $file;\n\t\t\/\/ upload a file\n\t\tif (ftp_put($conn_id, $remote_file, $fileSend, FTP_BINARY)) {\n\t\t \/\/echo \"successfully uploaded $file\\n\";\n\t\t} else {\n\t\t echo \"There was a problem while uploading $file\\n\";\n\t\t}\n\t\tunlink($fileSend);\n        }\n        closedir($dh);\n    }\n}\n\/\/ close the connection\nftp_close($conn_id);\n?>\n<\/pre>\n<h2>RWhois Server<\/h2>\n<p>Once you have rwhois configured on this server and an FTP server, you can begin adding these scripts.<\/p>\n<p>The rwhoisCleanFolder.sh script is meant to run first.  It should run around an hour before you ftp job runs.  This guy deletes the files out of your rwhois folders.<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">#!\/bin\/sh\r\n#######rwhoisCleanFolder.sh######\r\n\r\nrm -f \/usr\/local\/rwhoisd\/net-200.0.0.0-21\/data\/network\/*.txt\r\nrm -f \/usr\/local\/rwhoisd\/net-210.0.0.0.0-20\/data\/network\/*.txt\r\nrm -f \/usr\/local\/rwhoisd\/net-220.0.0.0-21\/data\/network\/*.txt\r\nrm -f \/usr\/local\/rwhoisd\/net-230.0.0.0-21\/data\/network\/*.txt\r\nrm -f \/usr\/local\/rwhoisd\/net-240.0.0.0-20\/data\/network\/*.txt<\/code><\/pre>\n<p>The rwhoisIndex.sh script should be scheduled to run 15 mins or so after your files are on the server.  This indexes all of your config files.  It then stops and starts the rwhois server.  Once this script runs, your are done!<\/p>\n<pre class=\"gs-code\"><code class=\"language-plaintext\">#!\/bin\/sh\r\n#####rwhoisIndex.sh#####\r\n\r\n#index all ip zones\r\n\/usr\/local\/rwhoisd\/bin\/rwhois_indexer -c \/usr\/local\/rwhoisd\/rwhoisd.conf -i -v -A 200.0.0.0\/21 -C network -s txt\r\n\/usr\/local\/rwhoisd\/bin\/rwhois_indexer -c \/usr\/local\/rwhoisd\/rwhoisd.conf -i -v -A 210.0.0.0\/20 -C network -s txt\r\n\/usr\/local\/rwhoisd\/bin\/rwhois_indexer -c \/usr\/local\/rwhoisd\/rwhoisd.conf -i -v -A 220.0.0.0\/21 -C network -s txt\r\n\/usr\/local\/rwhoisd\/bin\/rwhois_indexer -c \/usr\/local\/rwhoisd\/rwhoisd.conf -i -v -A 230.0.0.0\/21 -C network -s txt\r\n\/usr\/local\/rwhoisd\/bin\/rwhois_indexer -c \/usr\/local\/rwhoisd\/rwhoisd.conf -i -v -A 240.0.0.0\/20 -C network -s txt\r\n\r\n#restart the rwhoisd service\r\n\/etc\/init.d\/rwhoisd stop\r\n\/etc\/init.d\/rwhoisd start<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Pull all of your records from IPPlan and insert them into your RWhois server automatically. If you are an ISP or large enterprise you\u2026<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,8,13],"tags":[],"class_list":["post-223","post","type-post","status-publish","format-standard","hentry","category-linux","category-networking","category-server"],"_links":{"self":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=223"}],"version-history":[{"count":23,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":248,"href":"https:\/\/gregsowell.com\/index.php?rest_route=\/wp\/v2\/posts\/223\/revisions\/248"}],"wp:attachment":[{"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gregsowell.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}