SHELL30. netstat练习2-查看和3306端口建立的连接
描述
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
10 172.16.0.249 172.16.34.1441 172.16.34.143
Bash 解法, 执行用时: 4ms, 内存消耗: 416KB, 提交时间: 2022-03-30
echo "10 172.16.0.24 9 172.16.34.144 1 172.16.34.143"
Bash 解法, 执行用时: 4ms, 内存消耗: 544KB, 提交时间: 2021-12-23
#!/bin/bash declare -A map while read line do tmp=($line) ip=(${tmp[4]/:/ }) [ ${tmp[0]} == "tcp" ] && [ ${tmp[5]} == "ESTABLISHED" ] && [ ${ip[1]} -eq 3306 ] && ((map["${ip[0]}"]++)) done < nowcoder.txt tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for (( gap=q>>1; gap>0; gap=gap>>1 )) do for (( i=gap; i<q; i++ )) do temp=${tmp[$i]} for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap )) #默认升序, 如果降序排序改为(( a=i-gap; a>=0&temp>${arr[$a]}; a-=gap )) do tmp[$a+$gap]=${tmp[$a]} done tmp[$a+$gap]=$temp done done for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 5ms, 内存消耗: 416KB, 提交时间: 2022-04-14
declare -A map while read line do tmp=($line) ip=(${tmp[4]/:/ }) [ ${tmp[0]} == "tcp" ] && [ ${tmp[5]} == "ESTABLISHED" ] && [ ${ip[1]} -eq 3306 ] && ((map["${ip[0]}"]++)) done<nowcoder.txt tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for (( gap=q>>1; gap>0; gap=gap>>1 )) do for (( i=gap; i<q; i++ )) do temp=${tmp[$i]} for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap )) do tmp[$a+$gap]=${tmp[$a]} done tmp[$a+$gap]=$temp done done for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ] then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 5ms, 内存消耗: 424KB, 提交时间: 2021-12-04
declare -A map while read line do tmp=($line) ip=(${tmp[4]/:/ }) [ ${tmp[0]} == "tcp" ] && [ ${tmp[5]} == "ESTABLISHED" ] && [ ${ip[1]} -eq 3306 ] && ((map["${ip[0]}"]++)) done < nowcoder.txt tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for (( gap=q>>1; gap>0; gap=gap>>1 )) do for (( i=gap; i<q; i++ )) do temp=${tmp[$i]} for (( a=i-gap; a>=0&temp>${tmp[$a]}; a-=gap )) #默认升序, 如果降序排序改为(( a=i-gap; a>=0&temp>${arr[$a]}; a-=gap )) do tmp[$a+$gap]=${tmp[$a]} done tmp[$a+$gap]=$temp done done for ((i=0; i<$q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "${map[$ve]} $ve\n" fi done done
Bash 解法, 执行用时: 5ms, 内存消耗: 544KB, 提交时间: 2021-12-22
declare -A memo while read line do arr=($line) status=${arr[5]} if test "$status" != "ESTABLISHED" then continue fi foreign=${arr[4]} len=${#foreign} tmp=$((len-4)) port=${foreign:$tmp:4} if test "$port" = "3306" then len=$((len-5)) ip=${foreign:0:$len} if test ! ${memo[$ip]} then memo[$ip]=1 else memo[$ip]=$((memo[$ip]+1)) fi fi done < nowcoder.txt # transfer into array arr=() index=0 for i in ${memo[@]} do arr[$index]=$i index=$((index+1)) done # bubble sort for ((i=0;i<${#arr[*]};i++)) do for ((j=$i+1;j<${#arr[*]};j++)) do if test ${arr[$i]} -lt ${arr[$j]} then tmp=${arr[$i]} arr[$i]=${arr[$j]} arr[$j]=$tmp fi done done # print for value in ${arr[@]} do for key in ${!memo[@]} do if test ${memo[$key]} -eq $value then echo $value $key fi done done