作者:
DarkKiller (System hacked)
2019-09-20 17:33:10※ 引述《angle065 (Fu)》之銘言:
: 大家好,小弟有個問題想請教,因為想直接查詢出access.log不重複的ip
: 查到可以利用這個指令去查
: awk '{tmp[$1]} END {for (i in tmp) print i}' access.log
: 這邊有個比較不理解的地方想請教各位大大
這邊有點取捷徑,如果你看不懂的話可以用:
awk '{tmp[$1] = 1} END {for (i in tmp) print i}' access.log
這樣就會好懂一些。你可以交叉比較:
awk '{tmp[$1]} END {for (i in tmp) print tmp[i]}' access.log
你會發現裡面全部都是 1。
: 其中的 {tmp[$1]} 這個部分,我理解是把每一行的 第一組文字
: 寫入到tmp這個陣列變數中,接著再利用for迴圈去呈現重複的文字
: 也確實是讓我取得所有不重複的IP
: 想請教這個觀念是對的嗎?
: 那想請問tmp[$1],這個是哪一種語言寫入陣列的方式呢?
: 因為我稍微略懂一點PHP、JS,這樣的做法通常是塞給陣列/物件,索引值再用的
: 應該不是寫入陣列
不同家的實做不太一樣,但因為 POSIX 標準的關係,標準內定義的功能必須實做:
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/awk.html
在 GNU awk 的 manual 裡面是這樣寫:
All arrays in AWK are associative, i.e., indexed by string values.
而在 FreeBSD 上的 awk manual 則是這樣說:
Array subscripts may be any string, not necessarily numeric; this allows
for a form of associative memory. Multiple subscripts such as [i,j,k]
are permitted; the constituents are concatenated, separated by the value
of SUBSEP (see the section on variables below).
其他家又會有其他的方式,翻 manpage 或是 google 一下通常都會有。
然後補充一下,你的問題我的習慣是:
cut -d ' ' -f 1 access.log | sort -u
awk '{print $1}' access.log | sort -u
如果只是要看大宗的數量:
awk '{print $1}' access.log | sort | uniq -c | sort -n | tail
然後要看現在是哪個王八蛋在打:
while true; do clear; date; tail -n 10000 access.log | awk '{print $1}' | sort | uniq -c | sort -n | tail; sleep 1; done
指令用的習慣就好,方法還蠻多的...