shell编程题(二十八)

题目:

  查找请求数前20个IP(常用于查找攻来源)

答案:

#! /bin/bash
echo "The numbers of IP address"
echo "the first way:"

netstat -anlp | grep 80 | grep tcp | awk '{print $5}' | awk -F: '{print $1}' | uniq -c | sort -nr | head -n20


echo "the second way:"


netstat -ant | awk '/:80/{split($5,ip,":");++A[ip[1]]} END {for (i in A) print A[i],i}' | sort -rn | head -n20

运行结果:

解析:

split的效果

netstat -ant | awk '{print $5}'

netstat -ant | awk '/:80/{split($5,ip,":");print ip[1]}'

综合可知

{split($5,ip,":")

意思是将第五个字符串按照 ":" 进行切割,存放在名称为 ip的数组中。

ip[1]放的是ip, ip[2]放的是端口号 80

猜你喜欢

转载自www.cnblogs.com/wanghao-boke/p/12263286.html