Get IP list from CIDR


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get IP list from CIDR
# 8  
Old 01-09-2007
Hi again.
I hope this script do the trick:
Code:
#!/bin/bash

function addzeros {
   NUM=$1
   ZS=$2
   NBITS=$(echo "$NUM" | grep -o "[0-9]" | wc -l)
   if (( $NBITS < $ZS )); then
      ZEROS2ADD=$(expr $ZS - $NBITS)
      while (( $ZEROS2ADD << 0 )); do
         NUM="0$NUM"
         ZEROS2ADD=$(expr $ZEROS2ADD - 1)
      done
      echo "$NUM"
   else
      echo $NUM
   fi
}

function bin2dec {
   echo "obase=10; ibase=2; $1" | bc
}
function dec2bin {
   echo "obase=2; $1" | bc
}

function ipbin2ip {
   cont=1
   for l in $(echo $1 | grep -o [0,1]); do
      VIPTMP[$cont]=$l
      cont=$(expr $cont + 1)
   done
   ONE=`bin2dec $(echo ${VIPTMP[@]:1:8} | tr -d [:space:])`
   TWO=`bin2dec $(echo ${VIPTMP[@]:9:8} | tr -d [:space:])`
   THREE=`bin2dec $(echo ${VIPTMP[@]:17:8} | tr -d [:space:])`
   FOUR=`bin2dec $(echo ${VIPTMP[@]:25:8} | tr -d [:space:])`
   echo ${ONE}.${TWO}.${THREE}.${FOUR}
}

IPCIDR=$1
IP=$(echo $IPCIDR | cut -d"/" -f1)
BITS=$(echo $IPCIDR | cut -d"/" -f2)

FST=$(echo $IP | cut -d"." -f1)
FSTBIN=`addzeros $(echo "obase=2; $FST" | bc) 8`
SND=$(echo $IP | cut -d"." -f2)
SNDBIN=`addzeros $(echo "obase=2; $SND" | bc) 8`
TRD=$(echo $IP | cut -d"." -f3)
TRDBIN=`addzeros $(echo "obase=2; $TRD" | bc) 8`
FOH=$(echo $IP | cut -d"." -f4)
FOHBIN=`addzeros $(echo "obase=2; $FOH" | bc) 8`

VIP=0.0.0.0
IPBIN="$FSTBIN$SNDBIN$TRDBIN$FOHBIN"

i=1
for l in $(echo $IPBIN | grep -o [0,1]); do
   VIPBIN[$i]="$l"
   i=$(expr $i + 1)
done

BITSHOST=$(expr 32 - $BITS)
i=$BITSHOST
while (( $i > 0 )); do
   MAXHOSTBIN=1$MAXHOSTBIN
   i=$(expr $i - 1)
done
MAXHOST=$(bin2dec $MAXHOSTBIN)

n=$(echo ${VIPBIN[@]:1:$BITS} | tr -d [:space:])
c=$MAXHOST
while (( $c > 0 )); do
   h=$c
   EUREKA=${n}$(addzeros `dec2bin $h` $BITSHOST)
   ipbin2ip "$EUREKA"
   c=$(expr $c - 1)
done

As you can guess, it's been written for bash/Linux.

Code:
$ ./cidr.sh 192.168.0.0/19
192.168.31.255
192.168.31.254
192.168.31.253
192.168.31.252
192.168.31.251
192.168.31.250
192.168.31.249
192.168.31.248
192.168.31.247
(...)

Though, I'm pretty sure there are several other (and much better and efficient) ways to do it! Smilie
Regards.
# 9  
Old 01-10-2007
great work grial. this is perfect.
# 10  
Old 01-11-2007
Here is a revision of that will run without GNU grep. It also runs a little bit faster but it is still slow.

Code:
#!/usr/local/bin/bash
shopt -s extglob

oct2bin=("000" "001" "010" "011" "100" "101" "110" "111")

function squishtogether {
        result=""
        while (($#)) ; do
                result="${result}${1}"
                shift
        done
        echo $result
}

function splitapart {
        string=$1
        result=""
        while ((${#string})) ; do
                rest=${string#?}
                chr=${string%$rest}
                if [[ -z $result ]] ; then
                        result=$chr
                else
                        result="${result} ${chr}"
                fi
                string=$rest
        done
        echo $result
}

function addzeros {
# will also remove zeros if needed
        ZS=$2
        NUM=$1
        NUM=${NUM##+(0)}
        NUM=$(printf "%0${ZS}d\n" $NUM)
        echo $NUM
}

function bin2dec {
        string="2#${1}"
        ((idec=$string))
        echo $idec
}

function dec2bin {
        result=""
        for i in $(splitapart $(printf "%3o" $1)) ; do
                result="${result}${oct2bin[i]}"
        done
        echo $result
}

function ipbin2ip {
   cont=1
   for l in $(splitapart $1); do
      VIPTMP[$cont]=$l
      ((cont=cont+1))
   done
   ONE=`bin2dec $(squishtogether ${VIPTMP[@]:1:8})`
   TWO=`bin2dec $(squishtogether ${VIPTMP[@]:9:8})`
   THREE=`bin2dec $(squishtogether ${VIPTMP[@]:17:8})`
   FOUR=`bin2dec $(squishtogether ${VIPTMP[@]:25:8})`
   echo ${ONE}.${TWO}.${THREE}.${FOUR}
}
IPCIDR=$1
IP=${IPCIDR%/*}
BITS=${IPCIDR#*/}

FST=${IP%%.*}
IP=${IP#*.}
FSTBIN=`addzeros $(dec2bin "$FST") 8`
SND=${IP%%.*}
IP=${IP#*.}
SNDBIN=`addzeros $(dec2bin "$SND") 8`
TRD=${IP%%.*}
FOH=${IP#*.}
TRDBIN=`addzeros $(dec2bin "$TRD") 8`
FOHBIN=`addzeros $(dec2bin "$FOH") 8`

VIP=0.0.0.0
IPBIN="$FSTBIN$SNDBIN$TRDBIN$FOHBIN"

i=1
for l in $(splitapart $IPBIN); do
   VIPBIN[$i]="$l"
   ((i=i+1))
done

BITSHOST=$(expr 32 - $BITS)
i=$BITSHOST
while (( $i > 0 )); do
   MAXHOSTBIN=1$MAXHOSTBIN
   ((i=i-1))
done
MAXHOST=$(bin2dec $MAXHOSTBIN)

n=$(squishtogether ${VIPBIN[@]:1:$BITS})
c=$MAXHOST
while (( $c > 0 )); do
   h=$c
   EUREKA=${n}$(addzeros `dec2bin $h` $BITSHOST)
   ipbin2ip "$EUREKA"
   c=$(expr $c - 1)
done

# 11  
Old 12-10-2007
I have found a excellent utility to get a list of IPs from CIDR:

Quote:
# prips
usage: prips [options] <start end | CIDR block>
-c print range in CIDR notation
-d <x> set the delimeter 'x' where 0 =< x =< 255
-f <x> set the format of addresses (hex, dec, or dot)
-i <x> set the increment to 'x'
-e <x.x.x,x.x> e.g. -e ..4. will not print 192.168.4.[0-255]
An output example:

Quote:
# prips 192.168.0.0/29
192.168.0.0
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
Regards,
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert ip ranges to CIDR netblock

2 scripts to convert IP ranges to CIDR notation using awk, gawk or mawk. The scripts are much faster than using ipcalc and will return the same results. The first script is reliably compatible with awk, gawk and mawk but is over 3 times as slow as the second script which is reliably compatible with... (38 Replies)
Discussion started by: azdps
38 Replies

2. Shell Programming and Scripting

Convert ip ranges to CIDR netblocks

Hi, Recently I had to convert a 280K lines of ip ranges to the CIDR notation and generate a file to be used by ipset (netfilter) for ip filtering. Input file: 000.000.000.000 - 000.255.255.255 , 000 , invalid ip 001.000.064.000 - 001.000.127.255 , 000 , XXXXX 001.000.245.123 -... (10 Replies)
Discussion started by: ripat
10 Replies

3. Shell Programming and Scripting

How to change ip addressing format from CIDR notation to netmask and vice versa?

Hi all, I would appreciate if someone could share how to convert CIDR notation to netmask and vice versa. The value below is just an example. it could be different numbers/ip addresses. Initial Output, let say file1.txt Final Output, let say file2.txt (3 Replies)
Discussion started by: type8code0
3 Replies

4. UNIX for Dummies Questions & Answers

Creating a column based list from a string list

I have a string containing fields separated by space Example set sr="Fred Ted Joe Peter Paul Jean Chris Tim Tex" and want to display it in a column format, for example to a maximum of a window of 100 characters And hopefully display some thing like Fred Ted Joe ... (3 Replies)
Discussion started by: kristinu
3 Replies

5. Shell Programming and Scripting

Take a list if strings from a file and search them in a list of files and report them

I have a file 1.txt with the below contents. -----cat 1.txt----- 1234 5678 1256 1234 1247 ------------------- I have 3 more files in a folder -----ls -lrt------- A1.txt A2.txt A3.txt ------------------- The contents of those three files are similar format with different data values... (8 Replies)
Discussion started by: realspirituals
8 Replies

6. Programming

How to parse IP range in CIDR format in C

Hello everybody, I'm coding a network program and i need it to "understand" ip ranges, but i don't know how to make to parse an IP CIDR range, let's say "172.16.10.0/24" to work with the specified IP range. I've found a program which does it, but i don't understand the code. Here is the... (3 Replies)
Discussion started by: semash!
3 Replies

7. Shell Programming and Scripting

Splitting a list @list by space delimiter so i can access it by using $list[0 ..1..2]

EDIT : This is for perl @data2 = grep(/$data/, @list_now); This gives me @data2 as Printing data2 11 testzone1 running /zones/testzone1 ***-*****-****-*****-***** native shared But I really cant access data2 by its individual elements. $data2 is the entire list, while $data,2,3...... (1 Reply)
Discussion started by: shriyer
1 Replies

8. UNIX for Dummies Questions & Answers

counting a list of string in a list of txt files

Hi there! I have 150 txt files named chunk1, chunk2, ........., chunk150. I have a second file called string.txt with more than 1000 unique strings, house, dog, cat ... I want to know which command I should use to count how many times each string appears in the 150 files. I have tried... (4 Replies)
Discussion started by: Pep Puigvert
4 Replies

9. UNIX for Dummies Questions & Answers

send email from address list and subject list

Hello, Here is my problem. there are two files. first.txt <<< contains email address ====== abc@mail.com abd@mail.com abe@mail.com second.txt <<< contains webpage links ======== http//www.test.com/abc/index.html http://www.test.com/abd/index.html http://www.test.com/abe/index.html... (2 Replies)
Discussion started by: paulds
2 Replies
Login or Register to Ask a Question

Featured Tech Videos