题目

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

文件样例

access.log

192.168.199.178 - - [04/May/2015:11:09:11 -0400] "GET / HTTP/1.1" 200 3698 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
192.168.199.178 - - [04/May/2015:11:09:12 -0400] "GET / HTTP/1.1" 200 3698 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
192.168.199.178 - - [04/May/2015:11:09:12 -0400] "GET / HTTP/1.1" 200 3698 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
192.168.199.178 - - [04/May/2015:11:09:13 -0400] "GET / HTTP/1.1" 200 3698 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.15.3 zlib/1.2.3 libidn/1.18 libssh2/1.4.2" "-"
192.168.199.244 - - [04/May/2015:11:09:26 -0400] "GET /favicon.ico HTTP/1.1" 404 3652 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36" "-"
192.168.199.244 - - [04/May/2015:11:09:30 -0400] "GET /bb HTTP/1.1" 404 3652 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.199.244 - - [04/May/2015:11:09:30 -0400] "GET /favicon.ico HTTP/1.1" 404 3652 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.199.244 - - [04/May/2015:11:09:34 -0400] "GET /cc HTTP/1.1" 404 3652 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.199.244 - - [04/May/2015:11:09:34 -0400] "GET /favicon.ico HTTP/1.1" 404 3652 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko" "-"
192.168.199.244 - - [04/May/2015:11:09:44 -0400] "-" 400 0 "-" "-" "-"

error.log

015/05/04 11:06:12 [error] 2145#0: *7 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:07:12 [error] 2145#0: *10 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.180, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:07:12 [error] 2145#0: *10 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.180, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:07:20 [error] 2145#0: *14 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.155, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:07:20 [error] 2145#0: *15 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.155, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:09:26 [error] 2145#0: *47 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:09:30 [error] 2145#0: *49 open() "/usr/share/nginx/html/bb" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /bb HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:09:30 [error] 2145#0: *49 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:09:34 [error] 2145#0: *50 open() "/usr/share/nginx/html/cc" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /cc HTTP/1.1", host: "192.168.199.183"
2015/05/04 11:09:34 [error] 2145#0: *50 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.199.244, server: _, request: "GET /favicon.ico HTTP/1.1", host: "192.168.199.183"

作答

第一题

#!/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

第二题

注意的几个地方

* 正则判断ip合法性

* 判断时间格式合法

* 函数的格式

#!/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

【shell脚本练习】网卡信息和简单日志分析的更多相关文章

  1. 利用shell脚本实现nginx 的logs日志分割

    Nginx 是一个非常轻量的 Web 服务器,体积小.性能高.速度快等诸多优点.但不足的是也存在缺点,比如其产生的访问日志文件一直就是一个,不会自动地进行切割,如果访问量很大的话,将 导致日志文件容量 ...

  2. shell脚本编程之“最简单的死循环”【转】

    转自:http://blog.chinaunix.net/uid-23046336-id-3475462.html 在linux下编程的程序猿都知道shell脚本,就算你不怎么熟悉,也应该听过的吧!那 ...

  3. 【Shell 编程基础第一部分】第一个Shell脚本HelloShell及一些简单的Shell基础书写与概念;

    http://blog.csdn.net/xiaominghimi/article/details/7603000 本站文章均为李华明Himi原创,转载务必在明显处注明:转载自[黑米GameDev街区 ...

  4. 将shell脚本运行情况写入Rsyslog日志server

    在运维工作中,免不了编写一些脚本交由计划任务(cron)去定时运行完毕一些日常工作,实现运维工作自己主动化.比方在我的日常工作中备份数据是一项重要的工作,须要定时将数据备份到备份服器和一些其它的备份介 ...

  5. shell常用命令及正则辅助日志分析统计

    https://www.cnblogs.com/wj033/p/3451618.html 正则日志分析统计 3 grep 'onerror'  v3-0621.log | egrep  -v '(\d ...

  6. Linux下Shell脚本运行程序不输出日志到终端

    使用: 脚本路径/脚本名 >/dev/>& 说明: 可以简单的理解/dev/null是Linux下的回收站 >默认是把标准输出重定向 2>&1是把出错输出也定向 ...

  7. shell脚本在后台运行以及日志重定向输出

    后台运行命令 在命令行后加上 &,表示进程到后台中执行,如:cmd & 日志输出重定向 如:cmd > out.log & Linux默认定义两个变量:1和2: 1 表示 ...

  8. shell脚本 监控网卡信息

    一.简介 源码地址 日期:2018/6/22 介绍:显示实时输入输出流量 效果图: 二.使用 适用:centos6+ 语言:英文 注意:无 下载 wget https://raw.githubuser ...

  9. shell脚本异步日志分析-接口耗时、可用率

    背景:现有日志接入日志报表大盘,为了避免作业高峰期间(双十一),系统也要观测系统整体情况,因此提出了观测近五分钟,接口成功率以及耗时等工具(默认统计最近五分钟,并进行结果汇总统计) 使用说明 前提:p ...

随机推荐

  1. 移动端meta标签缓存设置

    1.<meta charset="utf-8"> 2.<meta content="width=device-width, initial-scale= ...

  2. 【python标准库模块二】random模块学习

    random模块是用来生成随机数的模块 导入random模块 import random 生成一个0~1的随机数,浮点数 #随机生成一个0~1的随机数 print(random.random()) 生 ...

  3. WSGI及gunicorn指北(一)

    作为一个Python Web 开发工程师,pyg0每天都喜滋滋的写着基于各种web框架的业务代码. 突然有一天,技术老大过来跟pyg0说,嘿,我们要新上线一个服务,你来帮我部署一下吧.不用太复杂.用g ...

  4. Android开发艺术探索第五章——理解RemoteViews

    Android开发艺术探索第五章--理解RemoteViews 这门课的重心在于RemoteViews,RemoteViews可以理解为一种远程的View,其实他和远程的Service是一样的,Rem ...

  5. linux系统性能监控--I/O利用率

    尽管整体的处理器速度. 内存大小以及 I/O执行速度在不断提高,但 I/O操作的吞吐率和延迟性能仍然要比等价的内存访问操作低多个数量级.另外,由于许多工作负荷都拥有重要的I/O组件,I/O处理很容易成 ...

  6. django+uwsgi+nginx+postgresql备忘

    安装pg创建数据库xxx设置用户密码111111 apt-get install postgresql su - postgres psql create database xxx; alter us ...

  7. IT男的别样人生,爱折腾,竟然辞职跑丽江去了

    深圳待了4年,在深圳腾讯总部任职,北漂了5年多,任某知名团购公司CTO,有了孩子以后才知道自己想要什么 2015年4月,我和老婆还有6个月的儿子丽江游, 却在旅行的第四天, 买下了位于束河古镇正门的高 ...

  8. 对 /dev/shm 认识

    一./dev/shm理论 /dev/shm/是linux下一个非常有用的目录,因为这个目录不在硬盘上,而是在内存里.因此在linux下,就不需要大费周折去建 ramdisk,直接使用/dev/shm/ ...

  9. springMVC源码分析--异常处理机制HandlerExceptionResolver执行原理(二)

    上一篇博客springMVC源码分析--异常处理机制HandlerExceptionResolver简单示例(一)中我们简单地实现了一个异常处理实例,接下来我们要介绍一下HandlerExceptio ...

  10. React Native之ScrollView控件详解

    概述 ScrollView在Android和ios原生开发中都比较常见,是一个 滚动视图控件.在RN开发中,系统也给我们提供了这么一个控件.不过在RN开发中 ,使用ScrollView必须有一个确定的 ...