最近发现/var/log/secure里有大量陌生IP尝试ssh登录到服务器,于是写了下面的小脚本把非法IP列入“黑名单”。
#!/bin/bash
#Author: Andy
#deny illegal ip login
#尝试5次失败就列入黑名单,写入/etc/hosts.deny
cat /var/log/secure|grep -v 192.168|\
awk '/Failed/ {print $(NF-3)}'|sort|uniq -c|\
awk '{if($1>=5)print
$2}'>/usr/local/shfile/black_name.txt
for ip in `cat /usr/local/shfile/black_name.txt`;do
#hosts.deny中如果有该IP则不重复写入
cat /etc/hosts.deny|grep "${ip}" >/dev/null
if [ $? -ne 0 ];then
#hosts.deny中没有的IP被写入
echo
"sshd:${ip}">>/etc/hosts.deny
fi
done
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/27181165/viewspace-775809/
,如需转载,请注明出处,否则将追究法律责任。