BASH第七课第二题

来源:互联网 发布:云计算与电子政务 编辑:程序博客网 时间:2024/06/10 18:46
2、写一个脚本analyzelog.sh,完成日志分析:(使用函数)(日志文件在课件中)
说明:此脚本可以接受选项(i,d,t,a),使用格式:analyzelog.sh <-i IP|-d DATE|-t TYPE|-a> 日志文件名 :
先判断是访问日志文件还是错误日志文件

访问日志文件如下:
   (1)当用户使用选项-i时,统计出访问日志文件中指定IP地址的访问次数(通常每一行为一次);
   (2)当用户使用选项-d时,统计出访问日志文件中指定日期(某一天,如:04/May/2015)内每个IP地址访问的次数;如:
    192.168.0.1:33
    192.168.0.195:17
    ...
   (3)当用户使用选项-t时,统计出访问日志文件中以后缀后指定类型的文件(如.png表示png格式的图片)被访问的次数;
   (4)当用户使用选项-a时,统计出访问日志文件中每个IP地址访问的次数;


错误日志文件日下:
   (1)当用户使用选项-i时,统计出错误日志文件中指定IP地址的访问次数(通常每一行为一次);
   (2)当用户使用选项-d时,统计出错误日志文件中指定日期(某一天,如:2015/05/04)内每个IP地址访问的次数;如:
    192.168.0.1:33
    192.168.0.195:17
    ...
   (3)当用户使用选项-t时,统计出错误日志文件中GET获取失败的次数(就是一行错误信息中包含GET);

   (4)当用户使用选项-a时,统计出错误日志文件中每个IP地址访问的次数;

#!/bin/bash#exec 2>>/dev/nullDIR=/root/tkp/file/command=$1[ -z $command ]&&command="-h"[ -z $FILE ]&&FILE="null"function helptext(){echo "analyzelog.sh <-i IP|-d DATE|-t TYPE|-a|-h> filename"echo "filename is access.log or error.log"echo "-------------access.log-----------------------"echo "-i IP (show how many times of the IP)"echo "-d DATE (format example 04/May/2015,list different IPs and times of their appearances)"echo "-a list all IPS and their IPs"echo "-t type show how many times the suffix appears"echo "-------------error.log------------------------"echo "-i IP (show how many times of the IP)"echo "-d DATE (format example 2015/05/04,list different IPs and times of their appearances)"echo "-a list all interfaces and their IPs"echo "-t type show how many times the wrong request  appears"echo "-h show the help text"}function legalip(){ip=$1flag=0echo $ip | grep "[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}"  &>/dev/nullif [ $? -ne 0 ];then        flag=1else        list=`echo $ip | awk -F"." '{print $1,$2,$3,$4}'`        for i in $list;        do                if [ $i -lt 1 ]||[ $i -gt 255 ];thenflag=1                fi        donefiecho $flag}function access(){OPTION=$1PARA=$2FILE_PATH=$DIR$3if [ $OPTION == "-i" ];thencount=`cat $FILE_PATH | grep ^$PARA | wc -l`echo $PARA appear $count timeselif [ $OPTION == "-d" ];thencat $FILE_PATH | grep $PARA |  awk -F" " '{count[$1]++}END{for(ip in count){printf("%s %d\n",ip,count[ip]);}}'elif [ $OPTION == "-t" ];thencount=`cat $FILE_PATH | grep $PARA | wc -l`echo "$PARA appears $count times"fi} function error(){OPTION=$1PARA=$2FILE_PATH=$DIR$3if [ $OPTION == "-i" ];thencount=`cat $FILE_PATH | grep "client: $PARA" | wc -l`echo $PARA appear $count timeselif [ $OPTION == "-d" ];thencat $FILE_PATH | grep $PARA | awk -F"," '{print $2}' | awk -F": " '{count[$2]++}END{for(ip in count){printf("%s %d\n",ip,count[ip]);}}'elif [ $OPTION == "-t" ];thencount=`cat $FILE_PATH | grep $PARA | wc -l`echo "$PARA appears $count times"         fi}if [ $# -lt 1 -o $# -gt 3 ];thenecho "the number of the parameters  is wrong "helptextelif [ $command == "-h" ];thenhelptextelif [ $command == "-i" ];thenif [ $# -le 2 ];then"the filename or IP cannot be null!"elif [ $# -eq 3 ];thenIP=$2                FILE=$3flag=$(legalip $IP)if [ $flag -ne 0 ];thenecho "error format ip(1-255.1-255.1-255.1-255)"elseif [ $FILE == "access.log" ];thenaccess $command $IP $FILE elif [ $FILE == "error.log" ];thenerror $command $IP $FILEelseecho "filename $FILE is wrong"helptextfififielif [ $command == "-d" ];thenif [ $# -le 2 ];thenecho "the filename or DATE cannot be null!"elif [ $# -eq 3 ];thendate=$2file=$3if [ $file == "access.log" ];thenaccess $command $date $fileelif [ $file == "error.log" ];thenerror $command $date $fileelseecho "filename $FILE is wrong"helptextfifielif [ $command == "-t" ];thenif [ $# -le 2 ];thenecho "the filename or TYPE cannot be null!"elif [ $# -eq 3 ];thentype=$2file=$3if [ $file == "access.log" ];thenaccess $command $type $fileelif [ $file == "error.log" ];thenerror $command $type $fileelseecho "filename $FILE is wrong"helptextfifielif [ $command == "-a" ];thenif [ $# -eq 1 ];thenecho "the filename cannot be null!"elif [ $# -eq 2 ];thenFILE=$2if [ $FILE == "access.log" ];thencat $DIR$FILE |  awk -F" " '{count[$1]++}END{for(ip in count){printf("%s %d\n",ip,count[ip]);}}'elif [ $FILE == "error.log" ];thencat $DIR$FILE |awk -F"," '{print $2}' | awk -F": " '{count[$2]++}END{for(ip in count){printf("%s %d\n",ip,count[ip]);}}'elseecho "filename $FILE is wrong"helptextfifielseecho "error command"helptextfi


0 0
原创粉丝点击