假设nginx的日志我们存储在nowcoder.txt里,格式如下: 192.168.1.20 - - [21Apr2020:14:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.21 - - [21Apr2020:15:27:49 +0800] "GET 2index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [21Apr2020:21:27:49 +0800] "GET 3index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.23 - - [21Apr2020:22:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.24 - - [22Apr2020:15:27:49 +0800] "GET 2index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.25 - - [22Apr2020:15:26:49 +0800] "GET 3index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.20 - - [23Apr2020:08:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.21 - - [23Apr2020:09:20:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [23Apr2020:10:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [23Apr2020:10:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.20 - - [23Apr2020:14:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.21 - - [23Apr2020:15:27:49 +0800] "GET 2index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [23Apr2020:15:27:49 +0800] "GET 3index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.25 - - [23Apr2020:16:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.24 - - [23Apr2020:20:27:49 +0800] "GET 2index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.25 - - [23Apr2020:20:27:49 +0800] "GET 3index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.20 - - [23Apr2020:20:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.21 - - [23Apr2020:20:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [23Apr2020:20:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.22 - - [23Apr2020:22:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 192.168.1.21 - - [23Apr2020:23:27:49 +0800] "GET 1index.php HTTP1.1" 404 490 "-" "Mozilla5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko20100101 Firefox45.0" 现在需要编写shell脚本统计访问3次以上的IP,你的脚本应该输出: 6 192.168.1.22 5 192.168.1.21 4 192.168.1.20
示例1
输入
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.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"
输出
7 192.168.1.22
5 192.168.1.21
4 192.168.1.20
加载中...