Skip to content

Instantly share code, notes, and snippets.

@ramit-mitra
Last active November 1, 2023 17:27
Show Gist options
  • Save ramit-mitra/f72b16b34099f85b3423bd32f63930c3 to your computer and use it in GitHub Desktop.
Save ramit-mitra/f72b16b34099f85b3423bd32f63930c3 to your computer and use it in GitHub Desktop.
Block banned IPs using UFW
#!/bin/bash
# Example: curl -sk https://gist.githubusercontent.com/ramit-mitra/f72b16b34099f85b3423bd32f63930c3/raw/ufw_block_banned_ips.sh | bash
url="https://raw.githubusercontent.com/ramit-mitra/blocklist-ipsets/main/rottenIPs.json"
echo "-------------------------"
echo "Reading Banlist IPs..."
blocked_ips=$(curl -s $url | jq -r '.[]')
echo "Banning IPs (using UFW)..."
echo "-------------------------"
for ip in $blocked_ips; do
echo $ip
sudo ufw deny from $ip to any > /dev/null
done
echo "-------------------------"
sudo ufw reload
sudo ufw status
echo "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment