commit c17e75b2c9f89a2de89b3e0018f80c5441629925 Author: Adrian Woodley Date: Fri Oct 8 10:10:12 2021 +0800 Initial commit diff --git a/geo-firewall.py b/geo-firewall.py new file mode 100755 index 0000000..00de7ba --- /dev/null +++ b/geo-firewall.py @@ -0,0 +1,39 @@ +#!/usr/bin/python3 +import netaddr +import csv +import sys +import os + +# inFilePath = 'GeoIPCountryWhois.csv' +# inFilePath = 'au.csv' +inFilePath = 'firewall.txt' + +os.system('sudo ipset create TEST2 hash:net -exist') +os.system('sudo ipset flush TEST2') + +iplist = [] + +with open(inFilePath, 'r') as inFile: + # fieldnames = ['StartIP', 'EndIP', 'DecIPStart', 'DecIPEnd', 'CountryCode', "Country"] + # fieldnames = 'StartIP', 'EndIP','Number','Date'] + # csvReader = csv.DictReader(inFile, fieldnames=fieldnames) + + # for row in csvReader: + # if row['CountryCode'] == 'AU': + # iplist.extend(netaddr.iprange_to_cidrs(row['StartIP'], row['EndIP'])) + + for row in inFile: + print(row) + if not row.startswith("#"): + iplist.extend(netaddr.IPNetwork(row)) + +print("########################################") + + +summary_subnets = netaddr.cidr_merge(iplist) +for subnet in summary_subnets: + print(subnet) + os.system('sudo ipset add TEST2 ' + str(subnet)) + +os.system('sudo ipset swap TEST TEST2') +os.system('sudo ipset destroy TEST2') \ No newline at end of file