Last active
December 22, 2022 14:39
-
-
Save ramit-mitra/f5e9c4f9adc4a155ebcce2608bc50f39 to your computer and use it in GitHub Desktop.
This script helps block banned IPs using fail2ban in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Example: curl -s https://gist.githubusercontent.com/ramit-mitra/f5e9c4f9adc4a155ebcce2608bc50f39/raw/block_banned_ips.sh | bash | |
url=https://raw.githubusercontent.com/ramit-mitra/blocklist-ipsets/main/rottenIPs.json | |
jail_names=($(fail2ban-client status | grep "Jail list" | sed -E 's/^[^:]+:[ \t]+//' | sed 's/,//g')) | |
blocked_ips=$(curl -s $url | jq -r '.[]') | |
echo "-------------------------" | |
echo "Reading Banlist IPs..." | |
echo "Banning IPs (this may take a while)..." | |
for jail in "${jail_names[@]}"; do | |
echo "Banning IPs in jail_type:: $jail" | |
echo "-------------------------" | |
for ip in $blocked_ips; do | |
echo $ip | |
fail2ban-client set $jail banip $ip > /dev/null | |
done | |
echo "-------------------------" | |
done | |
echo "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment