shell分析日志

post by rocdk890 / 2013-5-13 18:09 Monday linux技术

1,查看apache进程:
ps aux | grep httpd | grep -v grep | wc -l

2,查看80端口的tcp连接:
netstat -tan | grep "ESTABLISHED" | grep ":80" | wc -l

3,通过日志查看当天ip连接数,过滤重复:
cat access_log | grep "20/Oct/2008" | awk '{print $2}' | sort | uniq -c | sort -nr

4,当天ip连接数最高的ip都在干些什么(原来是蜘蛛):
cat access_log | grep "20/Oct/2008:00" | grep "122.102.7.212" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

5,当天访问页面排前10的url:
cat access_log | grep "20/Oct/2008:00" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

6,用tcpdump嗅探80端口的访问看看谁最高
tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr

接着从日志里查看该ip在干嘛:
cat access_log | grep 122.102.7.212| awk '{print $1"\t"$8}' | sort | uniq -c | sort -nr | less

7,查看某一时间段的ip连接数:

grep "2006:0[7-8]" www20060723.log | awk '{print $2}' | sort | uniq -c| sort -nr | wc -l

  

==============================
nginx

log_format main '[$time_local] $remote_addr $status $request_time $body_bytes_sent "$request" "$http_referer"';
access_log      /data0/logs/access.log  main;

 

 

格式如下:
[21/Mar/2011:11:52:15 +0800] 58.60.188.61 200 0.265 28 "POST /event/time HTTP/1.1" "http://host/loupan/207846/feature"


通过日志查看当天ip连接数,过滤重复
cat access.log | grep "20/Mar/2011" | awk '{print $3}' | sort | uniq -c | sort -nr
38 112.97.192.16
     20 117.136.31.145
     19 112.97.192.31
      3 61.156.31.20
      2 209.213.40.6
      1 222.76.85.28

 

当天访问页面排前10的url:
cat access.log | grep "20/Mar/2011" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10

 

找出访问次数最多的10个IP
awk '{print $3}' access.log |sort |uniq -c|sort -nr|head
  10680 10.0.21.17
   1702 10.0.20.167
    823 10.0.20.51
    504 10.0.20.255
    215 58.60.188.61
    192 183.17.161.216
     38 112.97.192.16
     20 117.136.31.145
     19 112.97.192.31
      6 113.106.88.10

 

找出某天访问次数最多的10个IP

cat /tmp/access.log | grep "20/Mar/2011" |awk '{print $3}'|sort |uniq -c|sort -nr|head
     38 112.97.192.16
     20 117.136.31.145
     19 112.97.192.31
      3 61.156.31.20
      2 209.213.40.6
      1 222.76.85.28

 

当天ip连接数最高的ip都在干些什么:
cat access.log | grep "10.0.21.17" | awk '{print $8}' | sort | uniq -c | sort -nr | head -n 10
224 /test/themes/default/img/logo_index.gif
    224 /test/themes/default/img/bg_index_head.jpg
    224 /test/themes/default/img/bg_index.gif
    219 /test/vc.php
    219 /
    213 /misc/js/global.js
    211 /misc/jsext/popup.ext.js
    211 /misc/js/common.js
    210 /sladmin/home
    197 /misc/js/flib.js

 

找出访问次数最多的几个分钟
 awk '{print $1}' access.log | grep "20/Mar/2011" |cut -c 14-18|sort|uniq -c|sort -nr|head
     24 16:49
     19 16:17
     16 16:51
     11 16:48
      4 16:50
      3 16:52
      1 20:09
      1 20:05
      1 20:03
      1 19:55
ps:http://junlee.blog.51cto.com/228061/270071

夜空- 本站版权
1、本站所有主题由该文章作者发表,该文章作者与夜空享有文章相关版权
2、其他单位或个人使用、转载或引用本文时必须同时征得该文章作者和夜空的同意
3、本帖部分内容转载自其它媒体,但并不代表本站赞同其观点和对其真实性负责
4、如本帖侵犯到任何版权问题,请立即告知本站,本站将及时予与删除并致以最深的歉意
5、原文链接:blog.slogra.com/post-366.html

标签: 命令 shell 分析 日志 脚本 log