---
title: "一个分析apache日志，统计ip访问，若某一ip访问次数超过规定值则自动发送短息给系统维护人员的shell"
author: "keepgo"
date: "2010-02-23T01:35:05Z"
tags: []
categories: ["操作系统"]
slug: "apache-ip-ip-shell"
draft: false
lastmod: "2010-02-23T01:37:41Z"
id: 600450
url: "https://keepgo.iteye.com/blog/600450"
---

webpay曾出现有人恶意访问的问题，因而写了这个shell。该shell所读取的webpay。log就是apache系统所记录的日志。改shell会分析统计ip访问次数，若超过规定值，就会自动发信息通知系统维护人员。其中，移动短信功能是一个开源的飞信机器人程序，可Google之获取使用。短息发送一般延时不超过一分钟，居然比公司购买的短信平台还快。

 

webpay。log已用awk等命令预处理过了，处理前apache日志格式如下：

123.152.119.141 - - \[28/Oct/2009:00:00:12 +0800] "GET /mLogin.do HTTP/1.1" 200 2666

处理之后格式webpay。log格式如下：

123.152.119.141 2009-10-28 00:00:12 /mLogin.do

 

分析处理webpay。log的shell如下：

 

```
#!/bin/sh
unus=50
declare -i n
declare -i t
t=0
n=0
m=$(cat webpay.log|wc -l)
cat webpay.sort| while read output;
do
  #echo $output
  countip=$(echo $output | awk '{ print $1}')
  ip=$(echo $output | awk '{ print $2 }' )
  t=$t+1
  echo "now read row: $t ; total row is: $m"

#判断ip访问统计数是否超过规定次数，并设置计数器  
if [ $countip -ge $unus ]; then
      n=$n+1
      echo "$n  --$ip --count:$countip greater than 50" 
fi

#当$t -eq $m表示已经读完文件最后一行，则可开始判断是否应该发短信警告，有超过50次访问的ip则发信息警告
if [  $t -eq $m  ]; then
     if [ $n -gt 0  ]; then
      string=("请注意，警告webpay共有$n个ip在规定时间内访问次数超过50次，查看更多此可疑ip信息请查result文件。发送三个人测试，系统发送时间：`date +%Y%m%d-%T`")
      #echo $string
      /opt/oracle/fection/fetion --debug --mobile=13450484855 --pwd=kigo1234 --to=13450484855,13660263413,13631387132 --msg-gb=$string
    else
      echo "no unusually ip found "
    fi
fi

done

```