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

Linux-Monitor IP frequent login server script

WBOY
Release: 2024-02-19 13:45:55
forward
648 people have browsed it

这个脚本旨在跟踪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
}
Copy after login

效果展示:

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!

Related labels:
source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template