如何收集活跃的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
如何Telnet自动登录路由器执行命令?

1 前言 一个问题,一篇文章,一出故事。 最近在笔者需要一个脚本自动从路由读取一些信息,因此整理本章 …

Bash
如何防止Base Shell脚本重复执行?

1 前言 一个问题,一篇文章,一出故事。 笔者最近发现脚本因为重复执行而损耗服务器性能,因此解决此问 …

Bash
如何实现Base Shell的数值百分比计算?

1 前言 一个问题,一篇文章,一出故事。 最近笔者需要通过Base Shell实现一个计算Quota …