当前位置: 技术问答>linux和unix
shell脚本获取本机端口号
来源: 互联网 发布时间:2017-03-29
本文导语: #netstat -tln -----输出如下------- Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:5801 0.0.0.0:* ...
#netstat -tln
-----输出如下-------
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:5801 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:427 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:2544 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
-------------------------
想得到的输出结果如下:
5801,427,5901,111,2544,631,25,22
请问shell语句要怎么写?
-----输出如下-------
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:5801 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:427 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:2544 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
-------------------------
想得到的输出结果如下:
5801,427,5901,111,2544,631,25,22
请问shell语句要怎么写?
|
命令:
#netstat -tln | awk '{if($1=="tcp"){num=split($4,A,":");B[A[num]]++;if(B[A[num]]==1)str=str""A[num]","}}END{print substr(str,1,length(str)-1)}'
#cat test.txt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:5801 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:427 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:2544 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
-------------------------
#awk '{if($1=="tcp"){num=split($4,A,":");B[A[num]]++;if(B[A[num]]==1)str=str""A[num]","}}END{print substr(str,1,length(str)-1)}' test.txt
5801,427,5901,111,2544,631,25,22
#netstat -tln | awk '{if($1=="tcp"){num=split($4,A,":");B[A[num]]++;if(B[A[num]]==1)str=str""A[num]","}}END{print substr(str,1,length(str)-1)}'
#cat test.txt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:5801 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:427 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:5901 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:2544 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::22 :::* LISTEN
tcp 0 0 ::1:25 :::* LISTEN
-------------------------
#awk '{if($1=="tcp"){num=split($4,A,":");B[A[num]]++;if(B[A[num]]==1)str=str""A[num]","}}END{print substr(str,1,length(str)-1)}' test.txt
5801,427,5901,111,2544,631,25,22
|
[nicenight@CSDN ~]# netstat -tln | awk 'BEGIN{ORS=","}; NR>2{sub(".*:", "", $4); print $4}'
|
netstat -tln | awk 'NR>2{sub(".*:", print $4}' | sort | uniq | tr 'rn' ','
去重的。
|
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}'
|
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}END{print ""}'