Home >Computer Tutorials >Computer Knowledge >Linux-Monitor IP frequent login server script

Linux-Monitor IP frequent login server script

WBOY
WBOYforward
2024-02-19 13:45:55633browse

这个脚本旨在跟踪IP地址的登录失败次数,当某个IP的失败次数超过限定值时,将禁止该IP进行登录尝试。

通过iptables防火墙阻止连接,当一个IP尝试登录次数超过5次时,iptables会阻止来自该IP的所有连接。

#!/bin/bash

function secrity(){
# 设置要监控的登录失败次数,超过该次数则会被阻止
MAX_ATTEMPTS=5

# 获取所有登录失败的IP并计数
IP_COUNT=$(lastb | awk '{print $3}' | sort | uniq -c | awk '$1 >= '$MAX_ATTEMPTS' {print $2}')


# 遍历所有登录失败次数超过阈值的IP并将其阻止
for IP in ${IP_COUNT}
do
# 检查IP是否已经在iptables策略中
if ! iptables -C INPUT -s $IP -j DROP &> /dev/null; then
echo "`date +"%F %H:%M:%S"`Blocking $IP ..."
iptables -A INPUT -s $IP -j DROP
else
echo "$IP is already blocked." > /dev/null 2>&1
fi
done
}

效果展示:

Linux-Monitor IP frequent login server script

The above is the detailed content of Linux-Monitor IP frequent login server script. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:mryunwei.com. If there is any infringement, please contact admin@php.cn delete