zoukankan      html  css  js  c++  java
  • Shell脚本问题详解

    例1:找出当前系统中端口大于1024的程序!


    使用netstat -tuln查询出的结果如下,需要输出红色字体的行:

    [root@localhost ~]# netstat -tuln
    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address               Foreign Address             State      
    tcp        0      0 0.0.0.0:49506               0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      
    tcp        0      0 127.0.0.1:6379              0.0.0.0:*                   LISTEN      

    tcp        0      0 0.0.0.0:111                 0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      
    tcp        0      0 0.0.0.0:22                  0.0.0.0:*                   LISTEN      
    tcp        0      0 127.0.0.1:631               0.0.0.0:*                   LISTEN      
    tcp        0      0 127.0.0.1:88                0.0.0.0:*                   LISTEN      
    tcp        0      0 127.0.0.1:25                0.0.0.0:*                   LISTEN      
    tcp        0      0 :::111                      :::*                        LISTEN      
    tcp        0      0 :::49331                    :::*                        LISTEN      
    tcp        0      0 :::22                       :::*                        LISTEN      
    tcp        0      0 ::1:631                     :::*                        LISTEN      
    udp        0      0 0.0.0.0:37727               0.0.0.0:*                              
    udp        0      0 0.0.0.0:751                 0.0.0.0:*                               
    udp        0      0 0.0.0.0:111                 0.0.0.0:*                               
    udp        0      0 0.0.0.0:631                 0.0.0.0:*                               
    udp        0      0 0.0.0.0:68                  0.0.0.0:*                               
    udp        0      0 127.0.0.1:852               0.0.0.0:*                               
    udp        0      0 :::33901                    :::*                                   
    udp        0      0 :::751                      :::*                                    
    udp        0      0 :::111                      :::*    


    【解法1】

    awk  '{match($0,/.*:([0-9]+)/,a)}a[1]>1024{print a[1]}'  1.txt


    说明:将netstat -tuln的输出结果保存为1.txt.

              使用match来匹配内容,$0表示1.txt的内容。

                /.*:([0-9]+)/为正则表达式,匹配需要的内容。匹配规则为:前面为任意多个字符+冒号+数字+任意多个字符。并把匹配到的内容自动赋值到数组a中,a的            下标             从1开始。如果a[1]的值大于1024,那么打印a[1]的值。

              

    【解法2】

    netstat -tuln|awk   -F:   '+$2>1024'

    说明:


    【解法3】

    netstat -tuln|awk   '+gensub(".*:","",1,$4)>1024'

    说明:gensub的语法为gensub("匹配到的字符串",'替换后的字符串',起始的位置),gensub(".*:","",1,$4)就是将.*匹配到的内容替换成空,然后显示第4列大于                 1024的数据。


    【解法4】

    ss -tu sport ge :1024

    说明:ss命令的用法,-tu表示tcp和udp协议.





  • 相关阅读:
    168. Excel Sheet Column Title
    171. Excel Sheet Column Number
    264. Ugly Number II java solutions
    152. Maximum Product Subarray java solutions
    309. Best Time to Buy and Sell Stock with Cooldown java solutions
    120. Triangle java solutions
    300. Longest Increasing Subsequence java solutions
    63. Unique Paths II java solutions
    221. Maximal Square java solutions
    279. Perfect Squares java solutions
  • 原文地址:https://www.cnblogs.com/xialiaoliao0911/p/7524034.html
Copyright © 2011-2022 走看看