Skip to content

Instantly share code, notes, and snippets.

@jammasterj89
Last active January 4, 2019 11:20
#!/bin/bash
# Script to unblock any IPs that denyhosts and fail2ban blocks.
# Slighty amended version of: https://gist.github.com/jayjanssen/1043053#file-denyhosts-remove-ip-sh-L1
# Script will remove the IP from all of the files inside of /var/lib/denyhosts except for allowed-hosts (the whitelist)
# Then loop through and unblock the IP from IPTABLES
# It will also unban any IPs in the fail2ban sshd jail
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
if [ -z $1 ]; then
echo "Example usage: ./unbanIP.sh 192.168.1.2"
exit 1
fi
#denyhosts
cd /var/lib/denyhosts
for i in `ls | grep -v allowed`; do mv $i $i.old; grep -v $1 $i.old >> $i; done
cp /etc/hosts.deny /tmp/hosts.deny
grep -v $1 /tmp/hosts.deny > /etc/hosts.deny
rm /var/lib/denyhosts/*.old
#fail2ban
fail2ban-client set sshd unbanip $1
#iptables
IPCOUNT=$(iptables -L | grep $1 | wc -l)
for n in `seq 1 $IPCOUNT`; do iptables -D INPUT -s $1 -j DROP; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment