Bash
1 前言
一个问题,一篇文章,一出故事。
笔者最近接到需要收集RHDS服务活跃的IP地址的工作任务,于是整理本文。
2 最佳实践
2.1 收集的环境
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
没有评论