-----输出如下-------
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语句要怎么写?
8 个解决方案
#1
[nicenight@CSDN ~]# netstat -tln | awk 'BEGIN{ORS=","}; NR>2{sub(".*:", "", $4); print $4}'
#2
厉害,基本实现了我想要的效果,不过没有过滤重复端口。
会输出两个25
5801,427,5901,111,2544,631,25,22,25
#3
命令:
#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
#4
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}'
#5
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}END{print ""}'
#6
netstat -tln | awk 'NR>2{sub(".*:", print $4}' | sort | uniq | tr '\r\n' ','
去重的。
#7
#1 @nicenight 输出结果基本正确,但端口没剔重
#3 @zhangtie8527 输出结果完全正确
#4 @ljc007 输出结果不正确
#5 @ljc007 输出结果不正确
#6 @fdl19881 输出有误,不过| sort | uniq | tr '\r\n' ','结合#1的语法,可以正常剔重,并排序输出。但是最后以为会多个逗号。
综述给分如下
#1 @nicenight 5分
#3 @zhangtie8527 8分
#4 @ljc007 1分
#5 @ljc007 1分
#6 @fdl19881 5分
谢谢大家的帮助!
#3 @zhangtie8527 输出结果完全正确
#4 @ljc007 输出结果不正确
#5 @ljc007 输出结果不正确
#6 @fdl19881 输出有误,不过| sort | uniq | tr '\r\n' ','结合#1的语法,可以正常剔重,并排序输出。但是最后以为会多个逗号。
综述给分如下
#1 @nicenight 5分
#3 @zhangtie8527 8分
#4 @ljc007 1分
#5 @ljc007 1分
#6 @fdl19881 5分
谢谢大家的帮助!
#8
#1
[nicenight@CSDN ~]# netstat -tln | awk 'BEGIN{ORS=","}; NR>2{sub(".*:", "", $4); print $4}'
#2
厉害,基本实现了我想要的效果,不过没有过滤重复端口。
会输出两个25
5801,427,5901,111,2544,631,25,22,25
#3
命令:
#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
#4
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}'
#5
netstat -tln | awk -F '[: ]+' 'NR>2{printf $5","}END{print ""}'
#6
netstat -tln | awk 'NR>2{sub(".*:", print $4}' | sort | uniq | tr '\r\n' ','
去重的。
#7
#1 @nicenight 输出结果基本正确,但端口没剔重
#3 @zhangtie8527 输出结果完全正确
#4 @ljc007 输出结果不正确
#5 @ljc007 输出结果不正确
#6 @fdl19881 输出有误,不过| sort | uniq | tr '\r\n' ','结合#1的语法,可以正常剔重,并排序输出。但是最后以为会多个逗号。
综述给分如下
#1 @nicenight 5分
#3 @zhangtie8527 8分
#4 @ljc007 1分
#5 @ljc007 1分
#6 @fdl19881 5分
谢谢大家的帮助!
#3 @zhangtie8527 输出结果完全正确
#4 @ljc007 输出结果不正确
#5 @ljc007 输出结果不正确
#6 @fdl19881 输出有误,不过| sort | uniq | tr '\r\n' ','结合#1的语法,可以正常剔重,并排序输出。但是最后以为会多个逗号。
综述给分如下
#1 @nicenight 5分
#3 @zhangtie8527 8分
#4 @ljc007 1分
#5 @ljc007 1分
#6 @fdl19881 5分
谢谢大家的帮助!