AWK +主机查找

目前使用下面的代码获得排名前20的ips按要求sorting:

grep 'GET /' /var/log/nginx/access.log | awk '{ print $1 }' | sort -n | uniq -c | sort -rn | head -20 

输出:

 575 66.249.*.* 570 66.249.*.* 534 207.46.*.* 511 157.55.*.* 493 66.249.*.* 435 207.46.*.* 383 66.249.*.* 378 157.55.*.* 368 66.249.*.* 336 66.249.*.* 334 188.165.*.* 332 174.24.*.* 292 54.209.*.* 251 66.249.*.* 241 66.249.*.* 234 66.249.*.* 226 66.249.*.* 225 89.145.*.* 221 89.145.*.* 209 66.249.*.* 

我想查找每个IP使用“主机

是否有可能完成这一行?

谢谢

你可以做这样的事情:

 awk '/GET / {print $1}' /var/log/nginx/access.log | sort -n | uniq -c | \ sort -rn | head -20 | awk '{print $2}' | while read row; do host $row; done 

我在这里添加了一个换行符,使其更加可重用。

我删除了grep因为你可以用awk直接过滤。

这段代码: awk '{print $2}' | while read row; do host $row; done awk '{print $2}' | while read row; do host $row; done awk '{print $2}' | while read row; do host $row; done会在每行(ip-address)上执行host命令。

编辑

这将保持最初的计数和顺序:

 awk '/GET / {print $1}' /var/log/nginx/access.log | sort -n | uniq -c | \ sort -rn | head -20 | while read row; do z=$( echo $row | awk '{print $2}' ); \ echo "$row $(host $z)"; done 

这不完全是一个漂亮的解决scheme,但它工作。

请注意,一般来说,您应该尝试避免多余的DNS查找,因为这可能会导致DNS服务器的容量问题,并且可能经常会阻止客户端上的caching(所以速度也会变慢)。 使用getent hosts进行查找。

我的博客页面上有一个AWK示例http://distracted-it.blogspot.co.nz/2015/04/please-dont-use-dig-etc-in-reporting.html

样品尼斯和脚本友好。 我们先看一个简单的例子,看看如何在AWK(或GAWK)中使用它。 让我们从一些input开始 – 可能是IP地址和计数的行。 我还包括一个门槛,只是提醒一下,尽量减less查找的次数。

 $ echo -e '1.1.1.1 2\n8.8.8.8 12\n8.8.4.4 25' \ | awk ' BEGIN {threshold = 5} $2 > threshold { "getent hosts " $1 | getline getent_hosts_str; split(getent_hosts_str, getent_hosts_arr, " "); print $1, getent_hosts_arr[2], $3 }' 8.8.8.8 google-public-dns-a.google.com 8.8.4.4 google-public-dns-b.google.com