netstat命令常见用法

一、介绍

Netstat是控制台命令,是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表、实际的网络连接以及每一个网络接口设备的状态信息。Netstat用于显示与IP、TCP、UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况。

二、netstat常见参数

列名解释:

Proto:显示连接使用的协议。

RefCnt:表示连接到本套接口上的进程号。

Types:显示套接口的类型。

State:显示套接口当前的状态。

Path:表示连接到套接口的其它进程使用的路径名。

三、常见用法

找出程序运行的端口

#netstat  -ap | grep ':80'

   
   
  • 1

查看连接某服务端口最多的的IP地址(前20个)

#netstat -nat | grep "10.1.62.23:443" |awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -20

   
   
  • 1

TCP各种状态列表

#netstat -nat |awk '{print $6}'

   
   
  • 1

在这里插入图片描述
统计连接数数量

#netstat -nat |awk '{print $6}'|sort|uniq -c

   
   
  • 1

在这里插入图片描述
排序

#netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn

#netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’

  • 1
  • 2
  • 3

在这里插入图片描述
直接统计tcp数量监听的数量

#netstat -ant | wc -l

 
 
  • 1

找出程序运行的端口

netstat -ap | grep '程序名'

 
 
  • 1

在这里插入图片描述
显示内核路由信息
使用 -r 选项打印内核路由信息。打印出来的信息与 route 命令输出的信息一样。我们也可以使用 -n 选项禁止域名解析。

$ netstat -rn
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
0.0.0.0         192.168.1.1     0.0.0.0         UG        0 0          0 eth0
192.168.1.0     0.0.0.0         255.255.255.0   U         0 0          0 eth0

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

打印网络接口
netstat 也能打印网络接口信息,-i 选项就是为这个功能而生。

$ netstat -i
Kernel Interface table
Iface   MTU Met   RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
eth0       1500 0     31611      0      0 0         27503      0      0      0 BMRU
lo        65536 0      2913      0      0 0          2913      0      0      0 LRU

 
 
  • 1
  • 2
  • 3
  • 4
  • 5

上面输出的信息比较原始。我们将 -e 选项和 -i 选项搭配使用,可以输出用户友好的信息。

$ netstat -ie
Kernel Interface table
eth0      Link encap:Ethernet  HWaddr 00:16:36:f8:b2:64  
          inet addr:192.168.1.2  Bcast:192.168.1.255  Mask:255.255.255.0
          inet6 addr: fe80::216:36ff:fef8:b264/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:31682 errors:0 dropped:0 overruns:0 frame:0
          TX packets:27573 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:29637117 (29.6 MB)  TX bytes:4590583 (4.5 MB)
          Interrupt:18 Memory:da000000-da020000 

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

显示多播组信息
选项 -g 会输出 IPv4 和 IPv6 的多播组信息。

$ netstat -g
IPv6/IPv4 Group Memberships
Interface       RefCnt Group
--------------- ------ ---------------------
lo              1      all-systems.mcast.net
eth0            1      224.0.0.251
eth0            1      all-systems.mcast.net
lo              1      ip6-allnodes
lo              1      ff01::1
eth0            1      ff02::fb
eth0            1      ff02::1:fff8:b264
eth0            1      ip6-allnodes
eth0            1      ff01::1
wlan0           1      ip6-allnodes
wlan0           1      ff01::1

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

查找具体的连接状态

$ netstat -atnp | grep ESTA

 
 
  • 1

打印 active 状态的连接
active 状态的套接字连接用 “ESTABLISHED” 字段表示,所以我们可以使用 grep 命令获得
active 状态的连接:
在这里插入图片描述
配合 watch 命令监视 active 状态的连接:

$ watch -d -n0 "netstat -atnp | grep ESTA"

 
 
  • 1

在这里插入图片描述
查看TCP连接状态

netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn
netstat -n |awk '/^tcp/ {print $NF}'|sort|uniq -c|sort -rn
netstat -n | awk '/^tcp/ {++S[$NF]};END {for(a in S) print a, S[a]}'
netstat -n | awk '/^tcp/ {++state[$NF]}; END {for(key in state) print key,"\t",state[key]}'
netstat -n | awk '/^tcp/ {++arr[$NF]};END {for(k in arr) print k,"\t",arr[k]}'
netstat -ant | awk '{print $NF}' | grep -v '[a-z]' | sort | uniq -c

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查找较多TIME_WAIT、SYN连接的IP

netstat -n|grep TIME_WAIT|awk '{print $5}'|sort|uniq -c|sort -rn|head -n20

 
 
  • 1

用tcpdump嗅探80端口看看哪个IP访问最频繁

tcpdump -i eth0 -tnn dst port 80 -c 1000 | awk -F"." '{print $1"."$2"."$3"."$4}' | sort | uniq -c | sort -nr |head -n 20

 
 
  • 1

查找请求数请10个IP
这个命令组合常用来查找网站攻 击来源IP,命令如下:

netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n10
netstat -ant |awk '/:80/{split($5,ip,":");++A[ip[1]]}END{for(i in A) print A[i],i}' |sort -rn|head -n10

 
 
  • 1
  • 2

这个命令最经常使用,特别是当网站访问量莫名升高的时候,例如:

[root@test ~]# netstat -anlp|grep 80|grep tcp|awk '{print $5}'|awk -F: '{print $1}'|sort|uniq -c|sort -nr|head -n5
124 27.148.154.216
85 27.148.154.162
82 27.148.154.217
81 27.148.154.159
78 221.235.244.60

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查看有哪些爬虫在抓取我的网站

tcpdump -i eth0 -l -s 0 -w - dst port 80 | strings | grep -i user-agent | grep -i -E 'bot|crawler|slurp|spider'

 
 
  • 1

获取访问次数最多的文件或页面,取前10

[root@test logs]# cat miivey_access.log |awk '{print $11}'|sort|uniq -c|sort -nr|head -n 10
12817067 "http://www.abc.com/"
1279583 "http://www.abc.com/cn/"
387843 "http://www.habcd.com/"

 
 
  • 1
  • 2
  • 3
  • 4

统计http连接状态
首先统计出日志中所有的连接状态码,以及每个状态码的数量:

[root@test logs]# cat miivey_access.log |awk '{print $9}'|sort|uniq -c|sort -rn
457030 200
17035 404

 
 
  • 1
  • 2
  • 3

状态码为200的数量有457030个,状态码为404的数量为17035,也非常多,这么多的404状态,肯定会对网站有影响,那么接下来就看看就有哪些404页面:

[root@test logs]# awk '($9 ~/404/)' miivey_access.log | awk '{print $9,$7}' | sort
404 /zzb.php
404 //zzk.aspx

 
 
  • 1
  • 2
  • 3

参考链接 :

Linux下netstat命令详解 https://blog.csdn.net/dongl890426/article/details/86981901

                                </div><div data-report-view="{&quot;mod&quot;:&quot;1585297308_001&quot;,&quot;dest&quot;:&quot;https://blog.csdn.net/qq_40907977/article/details/107365020&quot;,&quot;extend1&quot;:&quot;pc&quot;,&quot;ab&quot;:&quot;new&quot;}"><div></div></div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-e0530931f4.css" rel="stylesheet">
                            </div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值