SHELL29. netstat练习1-查看各个状态的连接数
描述
Proto Recv-Q Send-Q Local Address Foreign Address Statetcp 0 0 0.0.0.0:6160 0.0.0.0:* LISTENtcp 0 0 127.0.0.53:53 0.0.0.0:* LISTENtcp 0 0 0.0.0.0:22 0.0.0.0:* LISTENtcp 0 0 172.16.56.200:41856 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49822 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49674 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:42316 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:44076 172.16.240.74:6379 ESTABLISHEDtcp 0 0 172.16.56.200:49656 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:58248 100.100.142.4:80 TIME_WAITtcp 0 0 172.16.56.200:50108 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:41944 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:35548 100.100.32.118:80 TIME_WAITtcp 0 0 172.16.56.200:39024 100.100.45.106:443 TIME_WAITtcp 0 0 172.16.56.200:41788 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:58260 100.100.142.4:80 TIME_WAITtcp 0 0 172.16.56.200:41812 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:41854 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:58252 100.100.142.4:80 TIME_WAITtcp 0 0 172.16.56.200:49586 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:41754 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:50466 120.55.222.235:80 TIME_WAITtcp 0 0 172.16.56.200:38514 100.100.142.5:80 TIME_WAITtcp 0 0 172.16.56.200:49832 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:52162 100.100.30.25:80 ESTABLISHEDtcp 0 0 172.16.56.200:50372 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:50306 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49600 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:41908 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:60292 100.100.142.1:80 TIME_WAITtcp 0 0 172.16.56.200:37650 100.100.54.133:80 TIME_WAITtcp 0 0 172.16.56.200:41938 172.16.34.144:3306 ESTABLISHEDtcp 0 0 172.16.56.200:49736 172.16.0.24:3306 ESTABLISHEDtcp 0 0 172.16.56.200:41890 172.16.34.144:3306 ESTABLISHEDudp 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
ESTABLISHED 22TIME_WAIT 9LISTEN 3
Bash 解法, 执行用时: 4ms, 内存消耗: 420KB, 提交时间: 2021-11-26
declare -A map while read line do tmp=($line) [ ${tmp[0]} == "tcp" ] && ((map["${tmp[5]}"]++)) done < nowcoder.txt tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ];then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done for ((i=0; i<q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "$ve ${map[$ve]}\n" fi done done
Bash 解法, 执行用时: 4ms, 内存消耗: 424KB, 提交时间: 2022-04-14
#!/bin/bash #!/bin/awk declare -A map while read line do tmp=($line) [ ${tmp[0]} == "tcp" ] && ((map["${tmp[5]}"]++)) done<nowcoder.txt tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ] then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done for ((i=0; i<q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ] then printf "$ve ${map[$ve]}\n" fi done done
Bash 解法, 执行用时: 4ms, 内存消耗: 532KB, 提交时间: 2022-01-20
#!/bin/bash declare -A map while read -a line do if [ "x${line[5]}" = "xESTABLISHED" ] then ((map[${line[5]}]++)) elif [ "x${line[5]}" = "xTIME_WAIT" ] then ((map[${line[5]}]++)) elif [ "x${line[5]}" = "xLISTEN" ] then ((map[${line[5]}]++)) else ((map[other]++)) fi done < nowcoder.txt echo "ESTABLISHED ${map[ESTABLISHED]}" echo "TIME_WAIT ${map[TIME_WAIT]}" echo "LISTEN ${map[LISTEN]}"
Bash 解法, 执行用时: 4ms, 内存消耗: 548KB, 提交时间: 2022-01-13
#! /usr/bin/env bash ######################################################## # # ######################################################## function solution_1() { grep "tcp" nowcoder.txt | awk '{print $6}' | sort | uniq -c | awk '{print $2,$1}' | sort -nr -k2 } ######################################################## # # ######################################################## function solution_2() { awk '{ if($1 = /tcp/) a[$6]++ } END { for(k in a) { print k,a[k] } }' nowcoder.txt | sort -nrk2 } ######################################################## # # ######################################################## function solution_3() { local condition_index=${1} local target_index=${2} declare -A map local array="" while read line; do array=(${line}) [[ ${array[${condition_index}]} = "tcp" ]] && ((map["${array[${target_index}]}"]++)) done < nowcoder.txt local tmp=(${map[@]}) local size=${#tmp[@]} local int=0 for (( i = 0; i < ${size}; i++ )); do for (( j = $i + 1; j < ${size}; j++ )); do [[ ${tmp[$i]} -ge ${tmp[$j]} ]] && continue int=${tmp[$i]} tmp[i]=${tmp[$j]} tmp[$j]=$int done done for (( i = 0; i < size; i++ )); do for va in ${!map[*]}; do [[ ${tmp[$i]} = ${map[$va]} ]] && printf "$va ${map[$va]}\n" done done } solution_3 0 5
Bash 解法, 执行用时: 4ms, 内存消耗: 548KB, 提交时间: 2021-12-21
declare -A map while read line do tmp=($line) [ ${tmp[0]} == "tcp" ] && ((map["${tmp[5]}"]++)) done < nowcoder.txt function InsertSort(){ tmp=() for ve in ${map[*]} do tmp[${#tmp[*]}]=$ve done q=${#tmp[*]} for ((i=0;i<$q;i++)) do for ((j=$i+1;j<$q;j++)) do if [ ${tmp[$i]} -lt ${tmp[$j]} ];then t=${tmp[$i]} tmp[$i]=${tmp[$j]} tmp[$j]=$t fi done done } InsertSort for ((i=0; i<q; i++)) do for ve in ${!map[*]} do if [ ${tmp[$i]} -eq ${map[$ve]} ];then printf "$ve ${map[$ve]}\n" fi done done