awk 统计netstat 连接信息
[root@rsync_server ~]# netstat -an | grep "^tcp"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.16.66:22 192.168.16.99:59638 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 :::873 :::* LISTEN
tcp 0 0 :::3306 :::* LISTEN
------统计tcp连接的状态信息------
[root@rsync_server ~]# netstat -an | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a,S[a]}'
ESTABLISHED 1
LISTEN 6
------查看包含了tcp 和udp连接的信息------
[root@rsync_server ~]# netstat -an | grep -E "^tcp|^udp"
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 52 192.168.16.66:22 192.168.16.99:59638 ESTABLISHED
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
tcp 0 0 :::873 :::* LISTEN
tcp 0 0 :::3306 :::* LISTEN
udp 0 0 0.0.0.0:631 0.0.0.0:*
------统计包含了tcp 和udp连接的连接状态数------
[root@rsync_server ~]# netstat -an | awk '/^tcp|^udp/ {++S[$NF]} END {for (a in S) print a,S[a]}'
ESTABLISHED 1
0.0.0.0:* 1
LISTEN 6
[root@rsync_server ~]# netstat -an | awk '/^tcp|^udp/ {++S[$5]} END {for (a in S) print a,S[a]}'
192.168.16.99:59638 1
0.0.0.0:* 3
:::* 4
------过滤出tcp连接的IP地址------
[root@rsync_server ~]# netstat -an | grep "^tcp" | grep -v "::" | awk -F "[ :]+" '{print $6}'
0.0.0.0
0.0.0.0
192.168.16.99
------对过滤出来的连接IP地址进行统计------
[root@rsync_server ~]# netstat -an | grep "^tcp" | grep -v "::" | awk -F "[ :]+" '{++S[$6]} END {for (a in S) print a,S[a]}'
192.168.16.99 1
0.0.0.0 2
------按照数字排序,对第二个域进行排序-----
[root@rsync_server ~]# netstat -an | grep "^tcp" | grep -v "::" | awk -F "[ :]+" '{++S[$6]} END {for (a in S) print a,S[a]}' | sort -rn -k 2
0.0.0.0 2
192.168.16.99 1
------将统计数放在第一个域,直接sort -rn进行排序------
[root@rsync_server ~]# netstat -an | grep "^tcp" | grep -v "::" | awk -F "[ :]++" '{++S[$6]} END {for (a in S) print S[a],a}' | sort -rn
2 0.0.0.0
1 192.168.16.99
------过滤出IP地址后,排序,然后去重、统计------
[root@rsync_server ~]# netstat -an | awk -F "[ :]++" '/^tcp/ {print $6}' | sort | uniq -c
1 *
2 0.0.0.0
1 192.168.16.99
3 LISTEN
[root@rsync_server ~]# netstat -an | awk -F "[ :]++" '/^tcp/ {print $6}' | grep -v "LISTEN" | sort | uniq -c
1 *
2 0.0.0.0
1 192.168.16.99