preface(见面礼):
仅扫tcp端口:
netstat -tnlp|egrep -i "$1"
udp+tcp
netstat -tunlp|egrep -i "$1"
<2,go始终>
(服务器端口扫描,数据保存到shell array)
1 #!/bin/bash
2 portarray=(`netstat -tnlp|egrep -i "$1"|awk {'print $4'}|awk -F':' '{if ($NF~/^[0-9]*$/) print $NF}'|sort |uniq 2>/dev/null`)
3 length=${#portarray[@]}
4 printf "{\n"
5 printf '\t'"\"data\":["
6 for ((i=0;i<$length;i++))
7 do
8 printf '\n\t\t{'
9 printf "\"{#TCP_PORT}\":\"${portarray[$i]}\"}"
10 if [ $i -lt $[$length-1] ];then
11 printf ','
12 fi
13 done
14 printf "\n\t]\n"
15 printf "}\n"
想了解对应的端口 的pid 及进程名的见上面的相关检索命令,ruiy在此不述;