如何收集活跃的RHDS客户端IP地址?

Bash

1 前言

一个问题,一篇文章,一出故事。
笔者最近接到需要收集RHDS服务活跃的IP地址的工作任务,于是整理本文。

2 最佳实践

2.1 收集的环境

如何调整RHDS的访问日志的级别?

2.2 配置收集脚本

2.2.1 创建收集脚本

vim ~/scripts/getAccessIP.sh

加入如下配置,

#!/bin/bash

activeFile="/var/log/dirsrv/activeIPs.log"
excludeFile="/var/log/dirsrv/excludeIPs.log"
accessFile="/var/log/dirsrv/slapd-389ds/access"

nosslIPs=$(grep "connection from" "$accessFile" | grep -v "SSL" | awk -F' ' '{print $8}' | sort -u)
sslIPs=$(grep "connection from" "$accessFile" | grep "SSL" | awk -F' ' '{print $9}' | sort -u)
combinedIPs=$(echo -e "$nosslIPs\n$sslIPs" | sort -u)

if [ -f "$excludeFile" ]; then
        excludedIPs=$(cat "$excludeFile")
        filteredIPs=$(echo "$combinedIPs" | grep -v -F -x -f > "$activeFile"

if [ ! -f "$excludeFile" ]; then
        exit
fi

if [ ! -f "$activeFile" ]; then
        exit
fi

while IFS= read -r ip; do
        if grep -q "^$ip$" "$activeFile"; then
                sed -i "/^$ip$/d" "$activeFile"
        fi
done < "$excludeFile"
cat "$activeFile" | grep -v '^$' | sort -n -t . -k1,1 -k2,2 -k3,3 -k4,4 -o "$activeFile"

2.2.2 创建脚本定时任务

crontab -e

加入如下配置,

0 */1 * * * bash ~/scripts/getAccessIP.sh
没有评论

发表回复

Bash
如何根据条件输出MySQL表Insert行?

1 前言 一个问题,一篇文章,一出故事。 笔者执行NextCloud的维护任务,需要从数据表中删除用 …

Bash
如何函数代替Base Shell的命令别名?

1 前言 一个问题,一篇文章,一出故事。 之前的章节我们使用命令别名来缩写输入的命令,相见如下, 如 …

Bash
如何监视服务并自动通知?

1 前言 一个问题,一篇文章,一出故事。 最近笔者希望通过脚本实现监视服务并在服务异常是使用两种方式 …