zoukankan      html  css  js  c++  java
  • 一个bash脚本

    #!/bin/bash

    dir=/tmp
    file=$dir/ip_apnic
    rm -f $file

    #下载文件并输出到 $file
    wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest -O $file
    grep 'apnic|CN|ipv4|' $file| cut -f 4,5 -d '|'| sed 's/|/ /g'| while read ip cnt
    do
    #echo "$ip, $cnt"
    #将 $cnt 送到 bc 程序计算
    mask=$(cat<<EOF |bc |tail -1
    pow=32;
    define log2(x) {
    if (x <= 1)
    return (pow);
    pow--;
    return (log2(x/2));
    }
    log2($cnt)
    EOF
    )
    echo $ip/$mask >> $dir/cn.net
        #过滤出 UNICOM 和 CHINANET 的地址
    net=$(whois $ip |grep netname| cut -d : -f 2| sed 's/ *\(.*\)-.*/\1/')
    case $net in
    UNICOM)
    echo $ip/$mask >> $dir/UNICOM
    ;;
    CHINANET)
    echo $ip/$mask >> $dir/CHINANET
    ;;
    *)
    echo $net >> $dir/OTHER;
    ;;
    esac
    done
    exit 0

    功能大概是从一个统计网站提取 ip 地址,并判断来源,写入到相应文件中。计算掩码的时候效率不是太高,但很有利于学习!从一些实例中可以进步很快。

  • 相关阅读:
    mfc中的_T
    zmq的send
    c++内存相关函数
    如何运行linux shell程序
    Dockfile中的命令如何在.sh中执行
    Linux 错误: $' ': command not found
    实战ZeroMQ的PUSH/PULL推拉模式
    Servlet笔记
    进程控制块(PCB)
    Makefile规则介绍
  • 原文地址:https://www.cnblogs.com/xiangzi888/p/2420339.html
Copyright © 2011-2022 走看看