首页 > 系统服务 > 详细

shell编程题(二十八)

时间:2020-02-05 13:32:35      阅读:71      评论:0      收藏:0      [点我收藏+]

题目:

  查找请求数前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

 

shell编程题(二十八)

原文:https://www.cnblogs.com/wanghao-boke/p/12263286.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!