Shell+Linux命令实现日志分析

一、列出当天访问次数最多的IP
命令:
复制代码代码如下:
cut -d- -f 1 /usr/local/apache2/logs/access_log |uniq -c | sort -rn | head -20

原理:
复制代码代码如下:

       cut
       -d, --delimiter=DELIM
              use DELIM instead of TAB for field delimiter
              表示用-分割,然后-f 1 
       -f, --fields=LIST
              select only these fields;  also print any line that contains  no
              delimiter character, unless the -s option is specified
           表示打印第一部分,就是ip
 uniq 是将重复行去掉, -c表示前面前面加上数目,
       sort -rn 就是按照数字从大到小排序,
       head -20取前面20行
       
最后打印的结果大概是这样:
复制代码代码如下:
   217 192.114.71.13
   116 124.90.132.65
   108 192.114.71.13
   102 194.19.140.96
   101 217.70.34.173
   100 219.235.240.36
以下是其他一些分析日志的shell用法:
1、查看当天有多少个IP访问:
复制代码代码如下:
awk '{print $1}' log_file|sort|uniq|wc -l
2、查看某一个页面被访问的次数;
复制代码代码如下:
grep "/index.php" log_file | wc -l
3、查看每一个IP访问了多少个页面:
复制代码代码如下:
awk '{++S[$1]} END {for (a in S) print a,S[a]}' log_file
4、将每个IP访问的页面数进行从小到大排序:
复制代码代码如下:
awk '{++S[$1]} END {for (a in S) print S[a],a}' log_file | sort -n
5、查看某一个IP访问了哪些页面:
复制代码代码如下:
grep ^111.111.111.111 log_file| awk '{print $1,$7}'
6、去掉搜索引擎统计当天的页面:
复制代码代码如下:
awk '{print $12,$1}' log_file | grep ^\"Mozilla | awk '{print $2}' |sort | uniq | wc -l
7、查看2009年6月21日14时这一个小时内有多少IP访问:
复制代码代码如下:
awk '{print $4,$1}' log_file | grep 21/Jun/2009:14 | awk '{print $2}'| sort | uniq | wc -l


练习

[root@localhost logs]# cat access_log-20150708 
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET / HTTP/1.1" 403 4897 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/bootstrap.min.css HTTP/1.1" 200 19341 "http://192.168.140.199/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /images/apache_pb.gif HTTP/1.1" 200 2326 "http://192.168.140.199/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/open-sans.css HTTP/1.1" 200 5081 "http://192.168.140.199/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /images/poweredby.png HTTP/1.1" 200 3956 "http://192.168.140.199/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.140.199/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://192.168.140.199/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.140.199/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.140.199/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
192.168.140.62 - - [29/Jun/2015:12:07:28 -0400] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.140.199/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36"
::1 - - [02/Jul/2015:12:15:53 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [02/Jul/2015:12:15:53 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [02/Jul/2015:12:15:53 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [02/Jul/2015:12:15:53 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [02/Jul/2015:12:15:53 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [08/Jul/2015:06:34:01 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [08/Jul/2015:06:34:01 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [08/Jul/2015:06:34:01 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [08/Jul/2015:06:34:01 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"
::1 - - [08/Jul/2015:06:34:01 -0400] "OPTIONS * HTTP/1.0" 200 - "-" "Apache/2.4.6 (CentOS) (internal dummy connection)"

[root@localhost logs]# cut -d - -f 1 access_log-20150708 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
192.168.140.62 
::1 
::1 
::1 
::1 
::1 
::1 
::1 
::1 
::1 
::1 
[root@localhost logs]# cut -d - -f 1 access_log-20150708 |uniq -c
     10 192.168.140.62 
     10 ::1 
[root@localhost logs]# cut -d - -f 1 access_log-20150708 |uniq -c |sort -rn 
     10 192.168.140.62 
     10 ::1 
[root@localhost logs]# cut -d - -f 1 access_log-20150708 |uniq -c |sort -rn |head -10
     10 192.168.140.62 
     10 ::1


转载于:https://my.oschina.net/yonghan/blog/476075

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值