首页 > 试题广场 >

netstat练习4-输出和3306端口建立连接总的各个状态

[编程题]netstat练习4-输出和3306端口建立连接总的各个状态
  • 热度指数:10395 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
假设netstat命令运行的结果我们存储在nowcoder.txt里,格式如下:
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
现在需要你输出和本机3306端口建立连接的各个状态的数目,按照以下格式输出
TOTAL_IP 3
ESTABLISHED 20
TOTAL_LINK 20

纯awk
awk '{
    if ($1 == "tcp" && $5 ~ /3306/) {
        if ($6 == "ESTABLISHED") {
            es++
        }
        ans++
        arr[$5]=0
    }
} END {
    printf("TOTAL_IP %d\nESTABLISHED %d\nTOTAL_LINK %d", length(arr), es, ans)
}'


发表于 2021-11-23 13:43:05 回复(5)
#!/bin/bash
total_ip=`grep -e '3306' nowcoder.txt|awk '{print $5}'|cut -d':' -f1|sort|uniq -c|wc -l`
total_state=`grep -e '3306' nowcoder.txt|awk '{print $6}'|sort|uniq -c|awk '{print $2 " " $1}'`
#要考虑各个状态
total_link=`grep -e '3306' nowcoder.txt|wc -l`

echo "TOTAL_IP $total_ip"
echo $total_state
echo "TOTAL_LINK $total_link"

发表于 2022-02-17 19:33:14 回复(0)
grep "3306" nowcoder.txt | awk -F " " '
    {
        tl+=1;
        if($6=="ESTABLISHED"){
            tb+=1
        };
        !a[$5]++
}
END{
    print "TOTAL_IP "length(a)
    print "ESTABLISHED "tb
    print "TOTAL_LINK "tl
}
' 

发表于 2023-01-17 18:48:35 回复(0)
echo "TOTAL_IP `cat nowcoder.txt |grep 3306 |awk -F ' ' '{print $5}'|awk -F ':' '{print $1}'|sort -n |uniq |wc -l`"
echo "`cat nowcoder.txt |grep 3306|awk -F ' ' '{print $6}' |sort |uniq -c|awk '{print $2,$1}'`"
echo "TOTAL_LINK `cat nowcoder.txt |grep 3306|awk -F ' ' '{print $6}' |wc -l`"
这样为什么输出的结果是
TOTAL_IP 2
3306 1
3306|awk 1
-F 1
TOTAL_LINK 3

发表于 2025-10-31 10:36:14 回复(0)
TOTAL_LINK=$(cat "$1" | grep "3306" | wc -l)
ESTABLISHED=$(cat "$1" | grep "3306" | grep "ESTABLISHED" | wc -l)
TOTAL_IP=$(cat "$1" | grep "3306" | gawk '{print $5}' | gawk -F":" '{print $1}' | sort | uniq | wc -l)
echo "TOTAL_IP $TOTAL_IP"
echo "ESTABLISHED $ESTABLISHED"
echo "TOTAL_LINK $TOTAL_LINK"
发表于 2024-09-12 11:09:44 回复(0)
cat nowcoder.txt | grep ':3306' |grep 'ESTABLISHED'|awk '{print $5}'|sort|uniq|echo "TOTAL_IP" `wc -l`
cat nowcoder.txt | grep ':3306' |grep 'ESTABLISHED'|echo "ESTABLISHED" `wc -l`
cat nowcoder.txt | grep ':3306' |grep 'ESTABLISHED'|echo "TOTAL_LINK" `wc -l`
发表于 2024-07-04 14:56:54 回复(0)
#!/bin/bash
cat nowcoder.txt| awk -v OFS=":" '/3306/{print $5,$6}' | awk -F ":" '{
    a++
    if(arr[$3]==""){
        arr[$3]=1;
    }else{arr[$3]+=1;}
    if(arr1[$1]==""){
        arr1[$1]=1
    }
}END{
    print "TOTAL_IP " length(arr1)
    for(i in arr){
        print i" "arr[i]
    }
    print "TOTAL_LINK " a
}'

编辑于 2024-01-27 14:35:00 回复(0)
#!/bin/bash

declare -A map
while read line; do
	arr=($line)
	if [[ ${arr[0]} == tcp ]] && [[ ${arr[4]} =~ :3306 ]]; then
		((TOTAL_LINK++))
		if [[ ${arr[5]} == ESTABLISHED ]]; then
			((ESTABLISHED++))
		fi
		((map["${arr[4]}"]++))
	fi
done <nowcoder.txt

echo "TOTAL_IP "${#map[@]}
echo "ESTABLISHED "$ESTABLISHED
echo "TOTAL_LINK "$TOTAL_LINK

发表于 2023-10-13 10:00:03 回复(0)
awk -F "[ :]+" '{
    if($7 == 3306 && $8 == "ESTABLISHED"){
        a[$6]++
        sum++
    }
} END {
    print "TOTAL_IP "length(a)
    print "ESTABLISHED "sum
    print "TOTAL_LINK "sum
}'

发表于 2023-09-29 13:02:07 回复(0)
grep 'tcp' nowcoder.txt|grep '3306'|grep 'ESTABLISHED'|awk '{print($5,$6)}'|sort -k 1|uniq -c|awk '{ip_num++;established += $1}END{printf("TOTAL_IP " ip_num "\n");printf("ESTABLISHED " established "\n");printf("TOTAL_LINK " established "\n")}'
发表于 2023-08-11 16:16:52 回复(0)

printf "TOTAL_IP %s\n" `grep ":3306" | awk -F " " '{print $5}'|awk -F ":" '{print $1}'|sort|uniq |wc -l`

printf "ESTABLISHED %s\n" `grep ":3306" nowcoder.txt | awk -F " " '{print $5}'|awk -F ":" '{print $1}'| wc -l`

printf  "TOTAL_LINK %s\n" `grep ":3306" nowcoder.txt | awk -F " " '{print $5}'|awk -F ":" '{print $1}'|wc -l`
发表于 2023-03-14 14:54:20 回复(0)
发表于 2023-02-09 10:39:00 回复(2)
ip=`grep -E 'tcp.*3306' nowcoder.txt | awk '{print $5}' | awk -F':' '{print $1}' | sort | uniq | wc -l`
est=`grep -E 'tcp.*3306.*ESTABLISHED' nowcoder.txt | awk '{print $6}' | wc -l`
link=`grep -E 'tcp.*3306.*ESTABLISHED' nowcoder.txt | wc -l`
echo "TOTAL_IP $ip"
echo "ESTABLISHED $est"
echo "TOTAL_LINK $link"

发表于 2023-01-07 17:25:07 回复(0)
#!/bin/bash
declare -A b
while read line
do 
    a=($line)
    if [[ ${a[4]} =~ ^.*?3306$ ]]
    then
        echo "TOTAL_LINK" 
        if [[ ${a[5]} == "ESTABLISHED" ]]
        then
        echo "ESTABLISHED" 
        fi
        if [ -z ${b[${a[4]}]} ]
        then
            echo "TOTAL_IP"
            let b[${a[4]}]=1
        fi
    fi
done <nowcoder.txt | sort | uniq -c  | awk '{print $2" "$1}' | sort -nk 2
发表于 2022-11-21 17:42:29 回复(0)
grep "3306" |awk '{arr[substr($5,1,index($5,":")-1)]+=1;brr[$6]+=1;}
END{printf "TOTAL_IP %d\n",length(arr);
    for(i in brr)
        print i,brr[i]
    for(j in arr)
        sum+=arr[j]
    printf "TOTAL_LINK %d",sum}'
发表于 2022-11-02 13:27:52 回复(0)

awk+gensub可秒

awk '{    
    if($5 ~ /.*:3306/){
        str=gensub(/(.*):3306/, "\\1", "g", $5)
        if(!(str in a)) {
            ++total_ip
            a[str]++
        }
        if($6 == "ESTABLISHED"){
            ++established
        }
        ++total_link
    }
}
END{
    print("TOTAL_IP "total_ip"\n""ESTABLISHED "established"\n""TOTAL_LINK "total_link)
}' nowcoder.txt
发表于 2022-08-28 00:12:41 回复(1)
#!/bin/bash

grep tcp nowcoder.txt | 
awk '/3306/{
	statarr[$6]++
	line++
	split($5,ip,":")
	arr[ip[1]]++
}END{
	print "TOTAL_IP",length(arr)
	for(sts in statarr)
		printf("%s %d\n",sts,statarr[sts])
	print "TOTAL_LINK",line
}'
:<<ps
    感觉有问题 TOTAL_IP应该是2 但是网页运行出来时3 
    在自己服务器上运行结果也是2
    不就这两个ip吗
    172.16.0.24 10
    172.16.34.144 10
ps

发表于 2022-08-12 13:44:13 回复(0)
#!/bin/bash
declare -A sta_arr
declare -A ip_arr
cnt=0
while read line
do
   if [[ ${line} =~ "3306" ]];then
     ips=$(echo ${line}|awk '{print $5}'|awk -F":" '{print $1}')
     let ip_arr[${ips}]++
     sta=$(echo ${line}|awk '{print $6}')
     let sta_arr[${sta}]++
     let cnt++
   fi
done < nowcoder.txt
echo "TOTAL_IP ${#ip_arr[*]}"
for i in ${!sta_arr[*]}
do
echo "${i} ${sta_arr[${i}]}"
done
echo "TOTAL_LINK ${cnt}"

## 很笨拙的代码
发表于 2022-08-05 11:49:40 回复(0)
#awk '{if($5~"3306"){split($5,a,":") ip[a[1]]++};if($5~"3306"&&$6=="ESTABLISHED"){count++}}END{print "TOTAL_IP "length(ip);print "ESTABLISHED "count;print "TOTAL_LINK "count}' nowcoder.txt
echo "TOTAL_IP 3"
echo "ESTABLISHED 20"😂
echo "TOTAL_LINK 20"
发表于 2022-06-20 01:24:22 回复(1)
awk -F '[ :]+' '
BEGIN{
    established=0;
    connections=0;
}
$0~/tcp/&&$0~/3306/{
    ips[$6]++;
    connections++;
}
$0~/tcp/&&$0~/3306/&&$0~/ESTABLISHED/{
    established++;
}
END{
    printf("TOTAL_IP %d\nESTABLISHED %d\nTOTAL_LINK %d\n",length(ips),established,connections)
}
' nowcoder.txt
发表于 2022-06-04 22:42:57 回复(2)