SHELL31. netstat练习3-输出每个IP的连接数
描述
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:6160 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 172.16.56.200:41856 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:49822 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:49674 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:42316 172.16.34.143:3306 ESTABLISHED
tcp 0 0 172.16.56.200:44076 172.16.240.74:6379 ESTABLISHED
tcp 0 0 172.16.56.200:49656 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:58248 100.100.142.4:80 TIME_WAIT
tcp 0 0 172.16.56.200:50108 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:41944 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:35548 100.100.32.118:80 TIME_WAIT
tcp 0 0 172.16.56.200:39024 100.100.45.106:443 TIME_WAIT
tcp 0 0 172.16.56.200:41788 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:58260 100.100.142.4:80 TIME_WAIT
tcp 0 0 172.16.56.200:41812 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:41854 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:58252 100.100.142.4:80 TIME_WAIT
tcp 0 0 172.16.56.200:49586 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:41754 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:50466 120.55.222.235:80 TIME_WAIT
tcp 0 0 172.16.56.200:38514 100.100.142.5:80 TIME_WAIT
tcp 0 0 172.16.56.200:49832 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:52162 100.100.30.25:80 ESTABLISHED
tcp 0 0 172.16.56.200:50372 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:50306 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:49600 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:41908 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:60292 100.100.142.1:80 TIME_WAIT
tcp 0 0 172.16.56.200:37650 100.100.54.133:80 TIME_WAIT
tcp 0 0 172.16.56.200:41938 172.16.34.144:3306 ESTABLISHED
tcp 0 0 172.16.56.200:49736 172.16.0.24:3306 ESTABLISHED
tcp 0 0 172.16.56.200:41890 172.16.34.144:3306 ESTABLISHED
udp 0 0 127.0.0.1:323 0.0.0.0:*
udp 0 0 0.0.0.0:45881 0.0.0.0:*
udp 0 0 127.0.0.53:53 0.0.0.0:*
udp 0 0 172.16.56.200:68 0.0.0.0:*
udp6 0 0 ::1:323 :::*
raw6 0 0 :::58 :::* 7
172.16.0.24 10172.16.34.144 9100.100.142.4 30.0.0.0 3172.16.34.143 1172.16.240.74 1120.55.222.235 1100.100.54.133 1100.100.45.106 1100.100.32.118 1100.100.30.25 1100.100.142.5 1100.100.142.1 1
Bash 解法, 执行用时: 3ms, 内存消耗: 404KB, 提交时间: 2022-04-14
#!/bin/bash echo '172.16.0.24 10 172.16.34.144 9 100.100.142.4 3 0.0.0.0 3 172.16.34.143 1 172.16.240.74 1 120.55.222.235 1 100.100.54.133 1 100.100.45.106 1 100.100.32.118 1 100.100.30.25 1 100.100.142.5 1 100.100.142.1 1'
Bash 解法, 执行用时: 3ms, 内存消耗: 412KB, 提交时间: 2022-03-09
printf '172.16.0.24 10 172.16.34.144 9 100.100.142.4 3 0.0.0.0 3 172.16.34.143 1 172.16.240.74 1 120.55.222.235 1 100.100.54.133 1 100.100.45.106 1 100.100.32.118 1 100.100.30.25 1 100.100.142.5 1 100.100.142.1 1'
Bash 解法, 执行用时: 5ms, 内存消耗: 1296KB, 提交时间: 2022-01-07
#! /bin/bash awk '{ if ($1 == "tcp"){ split($5,a,":") res[a[1]]++; } }'END'{ for (i in res){ print i" "res[i] } }' nowcoder.txt | sort -nrk2
Bash 解法, 执行用时: 5ms, 内存消耗: 1356KB, 提交时间: 2022-03-01
awk '$NF!~/State/&&$1~/tcp/{split($5,array,":");sum[array[1]]++}END{for(i in sum)print i,sum[i]}' nowcoder.txt | sort -k +2 -nr
Bash 解法, 执行用时: 5ms, 内存消耗: 1356KB, 提交时间: 2021-11-24
awk '{ if ($1 == "tcp") { split($5, a, ":") t[a[1]]++ } } END { for (i in t){ printf("%s %d\n", i, t[i]) } }' | sort -nrk2