Skip to content

Instantly share code, notes, and snippets.

@pllopis
Created February 3, 2012 15:34
Show Gist options
  • Select an option

  • Save pllopis/1730761 to your computer and use it in GitHub Desktop.

Select an option

Save pllopis/1730761 to your computer and use it in GitHub Desktop.
Unblock host from denyhosts
#!/bin/bash
# Description: Remove host from denyhosts files
# This script can be run from any directory
# It requires to be run as root
#
# Pablo Llopis 2011
CONFIG='/etc/denyhosts.conf'
if [ $# -ne 1 ];
then
echo "Usage: $0 <host>"
exit
fi
host=$1
# get denyhosts work dir location
prefix=$(cat ${CONFIG} | grep -v "^\s*#" | grep WORK_DIR | awk '{print $3}')
# get files which contain host to be removed
match="${host}:"
offending_files=$(grep -H "${match}" ${prefix}/* | awk -F ':' '{print $1}')
# remove lines containing matching host pattern
for f in ${offending_files}
do
tmpf="/tmp/$(basename $f)"
grep -v "${match}" ${f} > ${tmpf}
mv ${tmpf} ${f}
done
#remove from /etc/hosts.deny
#try to avoid race condition where unprivileged users
#would overwrite hosts.deny just before final move to /etc
touch /tmp/hosts.deny
chown root /tmp/hosts.deny
chmod 600 /tmp/hosts.deny
grep -v "sshd: ${host}\$" /etc/hosts.deny > /tmp/hosts.deny
mv /tmp/hosts.deny /etc/hosts.deny
chmod 644 /etc/hosts.deny
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment