网卡信息和简单日志分析

1、写一个脚本getinterface.sh,脚本可以接受参数(i,I,a),完成以下任务:   (1)使用以下形式:getinterface.sh [-i interface|-I IP|-a]   (2)当用户使用-i选项时,显示其指定网卡的IP地址;   (3)当用户使用-I选项时,显示其后面的IP地址所属的网络接口;(如 192.168.199.183:eth0)   (4)当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)

 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地址访问的次数;

 

1、

#!/bin/bash

#test success in centos6  

#orangleliu

 

helptext="unknow options You can use: getinterface.sh [-i interface|-I IP|-a]"

 

while getopts "i:I:a" arg

do

    case $arg in

        i) #get arg value

            echo "$OPTARG IP:"`ifconfig $OPTARG|grep -E "inet "|cut -d: -f2|cut -d" " -f1`

            ;;

        I)

            echo "$OPTARG Interfacei:" `ifconfig |grep -B 1 $OPTARG|head -1|cut -d" " -f1`

            ;;

        a)

            echo -e "All interface except lo is: \n"

            ifconfig -a | grep -A 1 '^[^[:space:]]\{1,\}'  | grep  -v  -E  '(\<lo\>|\b127.0.0.1\b)'

            ;;

        *)  #unknow arg

            echo $helptext

            exit 1

            ;;

        esac

done


2、

#!/bin/bash

#file:analyzelog.sh  author:orangleliu

 

helptext="options is missing Please use: analyzelog.sh <-i IP|-d DATE|-t TYPE|-a> filename"

 

#get last parmater value, that is filename

filename=${BASH_ARGV[0]}

 

#file charge

if [ ! "$filename" == "" ];then

    echo "analyze file is $filename"

elif [ ! -e $filename ];then

    echo "file not existed"

    exit 1

fi

 

#funciton

access_i(){

    if echo "$1" | egrep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' &> /dev/null; then

        num=$(cat access.log|awk '{print $1}'|grep "$1"|wc -l)

        echo "IP $1 access count is $num"

    else

        echo "$1 is invalid ip"

        exit 1

    fi

}

 

access_d(){

    res=$(cat access.log|grep "$1"|cut -d" " -f1|sort |uniq -c |awk '{print $2,$1}')

    echo "accessIP  count"

    echo -e -n  "$res"

}

 

access_t(){

    num=$(cat access.log|awk '{print $7}'|grep -E "\.$1"|wc -l)

    echo ".$1 access count is $num"

}

 

access_a(){

    res=$(cat access.log|cut -d" " -f1|sort|uniq -c |awk '{print $2,$1}')

    echo "every ip access count:"

    echo "$res"

}

 

error_i(){

    if echo "$1" | egrep -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' &> /dev/null; then

        num=$(cat error.log|awk -F"[,:]" '{print $7}'|grep $1|wc -l)

        echo "IP $1 error count is $num"

    else

        echo "$1 is invalid ip"

        exit 1

    fi

}

 

error_d(){

    if date -d "$1" > /dev/null 2>&1 ; then

        res=$(cat error.log|awk -F"[,:]" '{print $1,$7}'|grep "$1"|sort|uniq -c|awk '{print $4,$1}')

        echo "errorIP  count"

        echo -e -n "$res"

    else

        echo "$1 is invalid date formate"

        exit 1

    fi

}

 

error_t(){

    num=$(cat error.log|awk -F"[\"]" '{split($4,g," ");print g[1]}'|grep "GET"|wc -l)

    echo "GET error requests count is $num"

}

 

error_a(){

    res=$(cat error.log|awk -F"[,:]" '{print $7}'|sort|uniq -c|awk '{print $2,$1}')

    echo "every ip error count:"

    echo  "$res"

}

 

#main

if [ "$filename" == "access.log" ];then

    case "$1" in

    -i)

        access_i $2

        ;;

    -d)

        access_d $2

        ;;

    -t)

        access_t $2

        ;;

    -a)

        access_a

        ;;

    *)

        echo $helptext

        ;;

    esac

 

elif [ "$filename" == "error.log" ];then

    case "$1" in

    -i)

        error_i $2

        ;;

    -d)

        error_d $2

        ;;

    -t)

        error_t

        ;;

    -a)

        error_a

        ;;

    *)

        echo $helptext

        ;;

    esac

 

else

    echo "file not existed"

fi


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值