This repository has been archived by the owner on Jul 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo2addresslist.py
57 lines (44 loc) · 1.59 KB
/
geo2addresslist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/python
import csv
import urllib2
import zipfile
import os
from urllib2 import urlopen
def download_db():
url = "https://download.maxmind.com/app/geoip_download?edition_id=GeoLite2-ASN-CSV&license_key=tu5iYvw1SbaCpzhs&suffix=zip"
file = "GeoLite2-ASN-CSV.zip"
response = urlopen(url)
CHUNK = 16 * 1024
with open(file, 'wb') as f:
while True:
chunk = response.read(CHUNK)
if not chunk:
break
f.write(chunk)
zip_ref = zipfile.ZipFile(file, 'r')
zip_ref.extractall()
zip_ref.close()
os.system("rm -f GeoLite2-ASN-CSV.zip && mv GeoLite*/* . && rm -f GeoLite2-ASN-Blocks-IPv6.csv && rm -f COPYRIGHT.txt LICENSE.txt && rm -rf GeoLite*/")
def parse_organization():
with open("freedom-addresslist.rsc", 'w') as f:
f.write("/ip firewall address-list")
f.write("\n")
f.write("remove [find list=freedom]")
f.write("\n")
with open("organization.txt", 'r') as f:
for line_terminated in f:
line = line_terminated.rstrip('\n')
generate_addresslist(line)
def generate_addresslist(organization):
with open("freedom-addresslist.rsc", 'a') as f:
#read csv, and split on "," the line
asn_ipv4 = csv.reader(open('GeoLite2-ASN-Blocks-IPv4.csv', "r"), delimiter=",")
#loop through csv list
for row in asn_ipv4:
#if current rows 2nd value is equal to input, print that row
if row[2].upper().startswith(organization.upper()):
f.write("add address=" + row[0] + " comment=\"" + row[2] + "-AS" + row[1] + "\" list=freedom")
f.write("\n")
if __name__ == '__main__':
download_db()
parse_organization()