[CentOS] ip address from range script

John Doe jdmls at yahoo.com
Thu Jul 29 16:31:50 UTC 2010


From: Jozsi Vadkan <jozsi.avadkan at gmail.com>

> TEST-A.txt: list of ip address ranges [AS/isp's in a country]
> TEST-B.txt:  list of ip addresses
> I just need to know, if an ip in the TEST-B.txt is  in a range of
> TEST-A.txt
> cat  "TEST-A.txt"
> 63.31.63.0/24;9007;44536
> 64.65.0.0/19;9000;8263
> 62.64.14.0/21;9001;6852
> cat "TEST-B.txt"
> 63.31.63.2
> 64.66.5.4
> 63.31.63.66
> 62.64.14.231
> output: : 
> 63.31.63.0/24;9007;44536
>      63.31.63.2
>      63.31.63.66
> 62.64.14.0/21;9001;6852
>      62.64.14.231
> -> so is an ip address [in TEST-B.txt] is from my  country [TEST-A.txt]
> or  not?

I have to go home soon, so here's a quick and dirty shell script that should 
work (uses ipcalc):

#!/bin/bash
cat TEST-A.txt | while read LINE
  do
    RANGE=`echo $LINE | cut -d';' -f1`
    echo `ipcalc -n $RANGE | grep "^HostM" | awk ' { print $2 } ' | 
          awk -F. -v r=$LINE ' { print r" "$1*16777216+$2*65536+$3*256+$4; } '` 
|
      while read INTRANGE
        do
          set $INTRANGE
          echo $1
          cat TEST-B.txt | while read ANIP
            do
              INTIP=`echo $ANIP | 
                     awk -F. ' { print $1*16777216+$2*65536+$3*256+$4; } '`
              if [ $INTIP -ge $2 -a $INTIP -le $4 ]; then
                echo "  "$ANIP
              fi
            done
        done
  done

=>

63.31.63.0/24;9007;44536
  63.31.63.2
  63.31.63.66
64.65.0.0/19;9000;8263
62.64.14.0/21;9001;6852
  62.64.14.231


JD


      



More information about the CentOS mailing list