Skip to content

Instantly share code, notes, and snippets.

@MarkZhangTW
Last active October 27, 2019 17:34
Show Gist options
  • Save MarkZhangTW/94426dfde4e0ca4c651968ea4b5a0b7f to your computer and use it in GitHub Desktop.
Save MarkZhangTW/94426dfde4e0ca4c651968ea4b5a0b7f to your computer and use it in GitHub Desktop.
A bash script for iptables dropping IPs which logged in failed more than 9 times.
#!/usr/bin/env bash
lastb -if /var/log/btmp.1 | awk '{print$3}' | sort | uniq -cd > lastb.log.1
lastb -i | awk '{print$3}' | sort | uniq -cd > lastb.log
cat lastb.log.1 lastb.log | sort -n | awk '$1>9{print$2}' | sort -n > try9s.ip
echo '===== IPs failed more than 9 times ====='
cat try9s.ip
while read ip; do
iptables --check INPUT --source $ip --jump DROP 2>/dev/null ||\
iptables --append INPUT --source $ip --jump DROP
done < try9s.ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment