日期:2014-05-16  浏览次数:20647 次

shell脚本获取本机端口号
#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语句要怎么写?

------解决方案--------------------
[nicenight@CSDN ~]# netstat -tln 
------解决方案--------------------
 awk 'BEGIN{ORS=","}; NR>2{sub(".*:", "", $4); print $4}'
  
------解决方案--------------------
命令: