#!/bin/bash
#2019-01-20 19:15
#DenyHosts 2.6 auto remove deny ip
#by xucong
################################
clear
date
DENY_DIR="/usr/share/denyhosts/data"
DENY_IP=$1
#############################################
#判断输入的参数只能是1个
if [ $# -ne 1 ];then
echo -e "\033[31m IP address format error....... \033[0m"
echo -e "\033[32m-------------------------------------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.1.1.1|192.168.1.1.100|help}\033[0m"
exit 0
fi
#判断IP 格式是否正确。
if echo $DENY_IP |egrep -q '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' ; then
A=`echo $DENY_IP | awk -F. '{print $1}'`
B=`echo $DENY_IP | awk -F. '{print $2}'`
C=`echo $DENY_IP | awk -F. '{print $3}'`
D=`echo $DENY_IP | awk -F. '{print $4}'`
#判断第一段IP的第一位是否为零 。
if [ `echo $A|cut -b 1` -eq 0 ];then
echo -e "\033[31m IP address error,The first digit cannot be zero. \033[0m"
exit
fi
#判断第二段IP的第一位是否为零 。
if [ `echo $B|cut -b 1` -eq 0 ];then
echo -e "\033[31m IP address error,The first digit cannot be zero. \033[0m"
exit
fi
#判断第三段IP的第一位是否为零 。
if [ `echo $C|cut -b 1` -eq 0 ];then
echo -e "\033[31m IP address error,The first digit cannot be zero. \033[0m"
exit
fi
#判断第四段IP的第一位是否为零 。
if [ `echo $D|cut -b 1` -eq 0 ];then
echo -e "\033[31m IP address error,The first digit cannot be zero. \033[0m"
exit
fi
#判断每段IP的值是否大于256
for n in $A $B $C $D; do
if [ $n -gt 255 ]; then
echo -e "\033[31m The IP value should be less than 256.\033[0m "
exit
fi
done
#输入的IP格式不为[0-9].[0-9].[0-9].[0-9] 这样的格式,均是错误IP。
else
echo -e "\033[31m IP address format error....... \033[0m"
echo -e "\033[32m-------------------------------------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.1.1.1|192.168.1.1.100|help}\033[0m"
exit
fi
if [ $1 == "help" ] ;then
echo -e "\033m[32m-------------------------------------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.1.1.1|192.168.1.1.100|help}\033[0m"
exit 1
fi
###############################################
for i in `ls $DENY_DIR/`
do
sed -i "/$DENY_IP/d" $DENY_DIR/$i
done
sed -i "/$DENY_IP/d" /etc/hosts.deny
echo "$DENY_IP" >>$DENY_DIR/allowed-hosts
echo -e "\033[32m----------------------\033[0m"
sleep 1
echo -e "\033[32m The Deny IP removed Success.\033[0m"