Skip to content

Instantly share code, notes, and snippets.

@jerry74
Created March 28, 2015 09:37
Show Gist options
  • Save jerry74/6ab9b7060eca5f85a77c to your computer and use it in GitHub Desktop.
Save jerry74/6ab9b7060eca5f85a77c to your computer and use it in GitHub Desktop.

Revisions

  1. jerry74 created this gist Mar 28, 2015.
    54 changes: 54 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    #!/bin/bash
    #wget -O- 'http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest' | awk -F\| '/CN\|ipv4/ { printf("%s/%d\n", $4, 32-log($5)/log(2)) }' > /etc/chnroute.txt
    netcalc(){

    local IFS='.' ip i
    local -a oct msk

    read -ra oct <<<"$1"
    read -ra msk <<<"$2"

    for i in ${!oct[@]}; do
    ip+=( "$(( oct[i] & msk[i] ))" )
    done

    NET="${ip[*]}"
    }

    cidr2mask ()
    {
    # Number of args to shift, 255..255, first non-255 byte, zeroes
    set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
    [ $1 -gt 1 ] && shift $1 || shift
    MASK="${1-0}.${2-0}.${3-0}.${4-0}"
    }

    cidrsplit ()
    {
    local IFS='/'
    local -a ipcidr

    read -ra ipcidr <<<"$1"

    IP=${ipcidr[0]}
    CIDR=${ipcidr[1]}
    }


    while read line
    do
    cidrsplit $line
    cidr2mask $CIDR
    netcalc $IP $MASK
    echo no-route = $IP/$MASK
    #echo MASK: $MASK
    #echo NETWORK: $NET

    done < chnroute.txt