This guide explains how to use import-ip-blocklists.sh to download one or more public IP blocklists, extract valid IPv4/CIDR entries, deduplicate results, and use them as a firewall blocklist.
Run with URLs passed directly:
./import-ip-blocklists.sh https://example.org/list1.txt https://example.org/list2.txt
Run using a source file (one URL per line):
./import-ip-blocklists.sh --url-file sources.txt
In both cases, the script:
Write to a custom output file:
./import-ip-blocklists.sh --url-file sources.txt --output my-ip-blocklist.txt
Merge with existing list and deduplicate:
./import-ip-blocklists.sh --url-file sources.txt --merge-existing --output ip-to-ban.txt
Apply directly to firewall (requires root):
sudo ./import-ip-blocklists.sh --url-file sources.txt --apply --set-name antispam_ext_block
Exclude trusted IPs using a whitelist:
./import-ip-blocklists.sh --url-file ip-to-ban-sources.txt --whitelist ip-to-ban-whitelist.txt
Combined whitelist and apply:
sudo ./import-ip-blocklists.sh --url-file ip-to-ban-sources.txt --whitelist ip-to-ban-whitelist.txt --apply
The whitelist file uses the same format as the output: one entry per line, IPv4 or CIDR, with # comment support.
Example ip-to-ban-whitelist.txt:
# Trusted IPs that must never be blocked
203.0.113.5
198.51.100.0/24
The script removes all whitelisted entries from the blocklist before writing the output file. It logs the number of whitelist entries loaded and how many were actually removed.
When you use –apply, the script:
match-set <set-name> src -j DROPCheck ipset content:
sudo ipset list antispam_ext_block
Check INPUT rule:
sudo iptables -S INPUT | grep antispam_ext_block
Example: update daily at 03:15 and apply automatically:
15 3 * * * /bin/bash /absolute/path/import-ip-blocklists.sh --url-file /absolute/path/sources.txt --apply --set-name antispam_ext_block >> /var/log/import-ip-blocklists.log 2>&1
Error: No URLs provided
Error: –apply requires root privileges
Error while downloading one source
Error: No valid IPv4/CIDR entries extracted
Error: Whitelist file not found