Skip to content

Instantly share code, notes, and snippets.

@ramit-mitra
Last active December 22, 2022 14:39
Show Gist options
  • Save ramit-mitra/f5e9c4f9adc4a155ebcce2608bc50f39 to your computer and use it in GitHub Desktop.
Save ramit-mitra/f5e9c4f9adc4a155ebcce2608bc50f39 to your computer and use it in GitHub Desktop.
This script helps block banned IPs using fail2ban in Linux
#!/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