列表

详情


SHELL25. nginx日志分析3-统计访问3次以上的IP

描述

假设nginx的日志我们存储在nowcoder.txt里,格式如下:
192.168.1.20 - - [21/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [21/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [21/Apr/2020:21:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.23 - - [21/Apr/2020:22:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.24 - - [22/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [22/Apr/2020:15:26:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:08:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:09:20:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:10:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:10:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:14:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:15:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:15:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [23/Apr/2020:16:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.24 - - [23/Apr/2020:20:27:49 +0800] "GET /2/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.25 - - [23/Apr/2020:20:27:49 +0800] "GET /3/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.20 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:20:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.22 - - [23/Apr/2020:22:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
192.168.1.21 - - [23/Apr/2020:23:27:49 +0800] "GET /1/index.php HTTP/1.1" 404 490 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0"
现在需要你写脚本统计访问3次以上的IP,你的脚本应该输出
6 192.168.1.22
5 192.168.1.21
4 192.168.1.20


原站题解

Bash 解法, 执行用时: 3ms, 内存消耗: 424KB, 提交时间: 2021-12-14

#!bin/bash
declare -A map
while read line
    do
        tmp=($line)
        ((map["${tmp[0]}"]++))
    done < nowcoder.txt
for i in ${!map[*]}
    do
        [ ${map[${i}]} -le 3 ] && unset map[$i]
    done
function InsertSort(){
	tmp=()
	for ve in ${map[*]}
	    do
	        tmp[${#tmp[*]}]=$ve
	    done
	q=${#tmp[*]}
	for ((i=0;i<$q;i++))
	    do
	        for ((j=$i+1;j<$q;j++))
	            do
	                if [ ${tmp[$i]} -lt ${tmp[$j]} ];then
	                    t=${tmp[$i]}
	                    tmp[$i]=${tmp[$j]}
	                    tmp[$j]=$t
	                fi
	            done
	    done
}
InsertSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 3ms, 内存消耗: 428KB, 提交时间: 2021-12-06

declare -A map
while read line
    do
        tmp=($line)
        ((map["${tmp[0]}"]++))
    done < nowcoder.txt
for i in ${!map[*]}
    do
        [ ${map[${i}]} -le 3 ] && unset map[$i]
    done
function InsertSort(){
	tmp=()
	for ve in ${map[*]}
	    do
	        tmp[${#tmp[*]}]=$ve
	    done
	q=${#tmp[*]}
	for ((i=0;i<$q;i++))
	    do
	        for ((j=$i+1;j<$q;j++))
	            do
	                if [ ${tmp[$i]} -lt ${tmp[$j]} ];then
	                    t=${tmp[$i]}
	                    tmp[$i]=${tmp[$j]}
	                    tmp[$j]=$t
	                fi
	            done
	    done
}
InsertSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 3ms, 内存消耗: 512KB, 提交时间: 2021-12-17

#!bin/bash
declare -A map
while read line
    do
        tmp=($line)
        ((map["${tmp[0]}"]++))
    done < nowcoder.txt
for i in ${!map[*]}
    do
        [ ${map[${i}]} -le 3 ] && unset map[$i]
    done
function InsertSort(){
    tmp=()
    for ve in ${map[*]}
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
    for ((i=0;i<$q;i++))
        do
            for ((j=$i+1;j<$q;j++))
                do
                    if [ ${tmp[$i]} -lt ${tmp[$j]} ];then
                        t=${tmp[$i]}
                        tmp[$i]=${tmp[$j]}
                        tmp[$j]=$t
                    fi
                done
        done
}
InsertSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 4ms, 内存消耗: 420KB, 提交时间: 2021-12-29

declare -A map
while read line
    do
        tmp=($line)
        ((map["${tmp[0]}"]++))
    done < nowcoder.txt
for i in ${!map[*]}
    do
        [ ${map[${i}]} -le 3 ] && unset map[$i]
    done
function InsertSort(){
    tmp=()
    for ve in ${map[*]}
        do
            tmp[${#tmp[*]}]=$ve
        done
    q=${#tmp[*]}
    for ((i=0;i<$q;i++))
        do
            for ((j=$i+1;j<$q;j++))
                do
                    if [ ${tmp[$i]} -lt ${tmp[$j]} ];then
                        t=${tmp[$i]}
                        tmp[$i]=${tmp[$j]}
                        tmp[$j]=$t
                    fi
                done
        done
}
InsertSort
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

Bash 解法, 执行用时: 4ms, 内存消耗: 428KB, 提交时间: 2021-11-26

declare -A map
while read line
    do
        a=($line)
        ((map["${a[0]}"]++))
    done < nowcoder.txt
for i in ${!map[*]}
    do
        [ ${map[${i}]} -le 3 ] && unset map[$i]
    done
tmp=()
for ve in ${map[*]}
    do
        tmp[${#tmp[*]}]=$ve
    done
q=${#tmp[*]}
for ((i=0;i<$q;i++))
    do
        for ((j=$i+1;j<$q;j++))
            do
                if [ ${tmp[$i]} -lt ${tmp[$j]} ];then
                    t=${tmp[$i]}
                    tmp[$i]=${tmp[$j]}
                    tmp[$j]=$t
                fi
            done
    done
for ((i=0; i<$q; i++))
    do
        for ve in ${!map[*]}
            do
                if [ ${tmp[$i]} -eq ${map[$ve]} ];then
                    printf "${map[$ve]} $ve\n"
                fi
            done
    done

上一题