【刷題】麪筋-linux-如何查找出現頻率最高的100個ip地址

命令:

  • cat + 文件名 + 管道 + 各類限制條件

簡單示例:有以下文件test.txt

1  134.102.173.43

2  134.102.173.43

3  134.102.171.42

4  134.102.170.9
  • 要統計出現次數最多的IP能夠利用如下shell腳本:php

    • cat test.txt | awk '{print $2}' | sort | uniq -c | sort -n -r | head -n 1
  • 參數含義:html

    • (針對首行不是IP地址信息的狀況)tail -n +3 :去掉上面用紅色標明的兩行。web

    • awk '{ print $5}':取數據的低5域(第5列),本例中是第2列,所以將5寫爲2。shell

    • (多加的限制,可忽略)cut -d : -f 1 :取藍色部分前面的IP部分。服務器

    • sort:對IP部分進行排序。less

    • uniq -c:打印每一重複行出現的次數,並去掉重複行ui

    • sort -n -r:按照重複行出現的次序倒序排列。url

    • head -n 5:取排在前5位的IP,本例中是找"最多",所以5能夠寫爲1..net

當前WEB服務器中聯接次數最多的ip地址

  • 命令:netstat -ntu | tail -n +3|awk '{ print $5}' | cut -d : -f 1 | sort | uniq -c| sort -n -r | head -n 5unix

  • 參數解析:

    • tail -n +3 :去掉上面用紅色標明的兩行。

    • awk '{ print $5}':取數據的低5域(第5列),上面藍色標明。

    • cut -d : -f 1 :取藍色部分前面的IP部分。

    • sort:對IP部分進行排序。

    • uniq -c:打印每一重複行出現的次數。(並去掉重複行)

    • sort -n -r:按照重複行出現的次序倒序排列。

    • head -n 5:取排在前5位的IP

  • 其餘示例:netstat -ntu |awk '{print $5}' |sort | uniq -c| sort -nr

經過查看日誌access_log文件進行統計

  • 查看日誌中訪問次數最多的前10個IP

    • cat access_log |cut -d ' ' -f 1 | sort |uniq -c | sort -nr | awk '{print $0 }' | head -n 10 | less
  • 查看日誌中出現100次以上的IP

    • cat access_log |cut -d ' ' -f 1 | sort |uniq -c | awk '{if ($1 > 100) print $0}'|sort -nr | less
  • 查看最近訪問量最高的文件

    • cat access_log | tail -10000 | awk '{print $7}' | sort | uniq -c | sort -nr | less
  • 查看日誌中訪問超過100次的頁面

    • cat access_log | cut -d ' ' -f 7 | sort |uniq -c | awk '{if ($1 > 100) print $0}' | less
  • 統計某url,一天的訪問次數

    • cat access_log | grep '12/Aug/2009' | grep '/images/index/e1.gif' | wc | awk '{print $1}'
  • 前五天的訪問次數最多的網頁

    • cat access_log | awk '{print $7}' | uniq -c | sort -n -r | head -20
  • 從日誌裏查看該ip在幹嗎

    • cat access_log | grep 218.66.36.119 | awk '{print $1"\t"$7}' | sort | uniq -c | sort -nr | less
  • 列出傳輸時間超過 30 秒的文件

    • cat access_log | awk '($NF > 30){print $7}' | sort -n | uniq -c | sort -nr | head -20
  • 列出最最耗時的頁面(超過60秒的)

    • cat access_log | awk '($NF > 60 && $7~/.php/){print $7}' | sort -n | uniq -c | sort -nr | head -100

參考連接

END

相關文章
相關標籤/搜索