Bind acl file generate from CSV ip list using bash
<p>I would like to generate geoiplist.acl file from a csv file. acl file format:</p>

<pre><code>acl "A1" {
    31.14.133.39/32;
    37.221.172.0/23;
acl "A2" {
    5.145.149.142/32;
    57.72.6.0/24;
......
</code></pre>

<p>the csv file: <a href="http://download.db-ip.com/free/dbip-country-2016-09.csv.gz" rel="nofollow">http://download.db-ip.com/free/dbip-country-2016-09.csv.gz</a></p>

<p>Here are sample lines from CSV file with IP_Start, IP_End and Country columns.</p>

<pre><code>"0.0.0.0","0.255.255.255","US"
"1.0.0.0","1.0.0.255","AU"
"1.0.1.0","1.0.3.255","CN"
"1.0.4.0","1.0.7.255","AU"
"1.0.8.0","1.0.15.255","CN"
"1.0.16.0","1.0.31.255","JP"
"1.0.32.0","1.0.63.255","CN"
"1.0.64.0","1.0.127.255","JP"
"1.0.128.0","1.0.255.255","TH"
"1.1.0.0","1.1.0.255","CN"
</code></pre>

<p>I got some references from here: <a href="http://geoip.site/" rel="nofollow">http://geoip.site/</a> but their acl don't have complete list.</p>

<p>Anyone can help me to do this in bash code please. Thanks in advance.</p>

<p>The issue here is that DB-IP provide the begin and end value of each range in human readable IP address format. Why they have done this, I'm not sure, because the more universal (easier to process) format is to simply present these values in integer form.</p>

<p>In any case, I have modified the Python script on <a href="http://geoip.site/" rel="nofollow">http://geoip.site/</a> to handle this and included the DB-IP database URL within the script; the ACL file generated from their CSV file is now also available to download from <a href="http://geoip.site/download/DB-IP/GeoIP.acl" rel="nofollow">http://geoip.site/download/DB-IP/GeoIP.acl</a></p>

<p>Note I have already identified some issues with this database:</p>

<ol>
<li>The entry <code>"::","2001:1ff:ffff:ffff:ffff:ffff:ffff:ffff","US"</code> exists in it. This is obviously complete rubbish and also broke the Python script (I've improved the error detection code to handle this) and is also one of the reasons for point 4 below.</li>
<li><code>"224.0.0.0","255.255.255.255","CH"</code> is an interesting entry. I'm not entirely sure how they have deemed the entire multicast block of the IPv4 address space to be delegated to Switzerland, or why it exists in their database at all.</li>
<li>The statistical analysis (available on <a href="http://geoip.site/" rel="nofollow">http://geoip.site/</a>) suggests that their DB/CSV file spans 100% of the IPv4 address space outside <code>224.0.0.0/3</code> (multicast). That's 3,758,096,384 addresses. But we already know that several address blocks should not exist in here, the obvious ones being <code>10.0.0.0/8</code>, <code>172.16.0.0/12</code> and <code>192.168.0.0/16</code> (and indeed others; further investigation reveals the entry <code>"192.168.0.0","192.169.31.255","US"</code> exists, which covers <code>192.168.0.0/16</code> and beyond). So this result looks questionable.</li>
<li>The statistical analysis also reports that their DB/CSV spans 100% of the IPv6 address space. This is primarily because they have mapped <code>3000::/4</code> (and various other smaller address blocks) to the <code>US</code>, which is wrong (see <a href="http://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml" rel="nofollow">http://www.iana.org/assignments/ipv6-unicast-address-assignments/ipv6-unicast-address-assignments.xhtml</a> where <code>3000::/4</code> is listed as <code>RESERVED</code>). This mapping originates from the entry <code>"2c10::","ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff","US"</code>. The other 2 databases on <a href="http://geoip.site/" rel="nofollow">http://geoip.site/</a> are nowhere close to this magnitude of coverage across the IPv6 address space (both are currently less than 0.1%), so this result also looks questionable.</li>
</ol>

<p>Given all of the above, I would question the accuracy of their database and contact them about it. But feel free to download the <a href="http://geoip.site/download/DB-IP/GeoIP.acl" rel="nofollow">http://geoip.site/download/DB-IP/GeoIP.acl</a> file if you wish to use it.</p>

<p>Finally, I would not have even attempted this in BASH. The conversions required to produce this file from their CSV file are only available in more advanced languages like Python; BASH just wouldn't cut this (well, not my BASH).</p>

<p>I hope this has helped resolved your query/problem.</p>

python bash csv bind9