zoukankan      html  css  js  c++  java
  • awk 查看ip连接数(常见应用一)

    处理文本,是awk的强项了。 无论性能已经速度都是让人惊叹!

    [chengmo@localhost ~]$ awk 'BEGIN{
        while("netstat -an"|getline){
            if( $5 ~ /[1-255]/)
            {
                split($5,t1,":");
                tarr[t1[1]]++;
            }
        }
        for(k in tarr)
            {
                print k,tarr[k] | "sort -r -n -k2";
            }
    };'

    $5是netstat –an 第5个字段。默认就是对方连接ip以及端口。

     

    [chengmo@localhost ~]$ time awk 'BEGIN{while("netstat -an"|getline){if( $5 ~ /[1-255]/){split($5,t1,":");tarr[t1[1]]++;}}for(k in tarr){print k,tarr[k] | "sort -r -n -k2";}};' 
    211.151.33.14 28
    113.65.21.200 14
    121.32.89.106 13
    60.191.178.230 12
    118.133.177.104 12
    58.61.152.154 11
    219.137.58.20 11
    124.117.248.52 11
    122.198.80.145 11
    222.88.15.138 10
    222.44.13.191 10
    222.161.47.34 10
    221.4.202.210 10
    218.79.64.12 10
    183.17.92.193 10
    120.72.128.254 10
    118.118.121.244 10
    116.252.38.236 10
    113.248.75.44 10

     

    real    0m1.149s
    user    0m0.032s
    sys     0m1.055s

     

    awk常见应用系列,会一直更新!我会把这些年我在服务器管理方面一些代码总结归纳与朋友共同学习。

  • 相关阅读:
    LVM
    作业
    软件工程随笔
    woj1005-holding animals-01pack woj1006-Language of animals-BFS
    woj1002-Genesis woj1003-birthofnoah woj1004-noah's ark
    C++ new delete malloc free
    centos7+腾讯云服务器搭建wordpress
    C++ inline与operator
    next v5升级到next v7需要注意的地方
    python = 赋值顺序 && C++ side effect
  • 原文地址:https://www.cnblogs.com/chengmo/p/1846731.html
Copyright © 2011-2022 走看看