Get IP list from CIDR


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get IP list from CIDR
# 1  
Old 01-09-2007
Get IP list from CIDR

Dear Srs :-)

I'm looking for a shell script, that given a network in CIDR format it lists all IPs, for example:

Quote:
$ script 192.168.0.0/24
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
...
Preferredly a shell script, but a Perl, Python, C, etc.. is also welcome :-)

I have been looking in sipcalc, ipcalc, etc.. options but this feature is not implemented :-(

Regards,
# 2  
Old 01-09-2007
Code:
ip=$( echo $1 | cut -d"/" -f1 )
no=$( echo $1 | cut -d"/" -f2 )
num=1
new_ip=$( echo $ip | cut -d"." -f1-3 )

while [[ $num -ne $no ]]
do
	echo $new_ip".$num"
	(( num = num + 1 ))
done

# 3  
Old 01-09-2007
I'm afraid it's not that easy, anbu23. That works only for class C addresses Smilie
I'll try to write a script and post it, but I'll need some time Smilie
Regards.
# 4  
Old 01-09-2007
Thanks for the fast reply!! But it doesnt works with all CIDR.. for this simple example:

Quote:
./cidr.sh 192.168.0.0/24
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
192.168.0.8
192.168.0.9
192.168.0.10
192.168.0.11
192.168.0.12
192.168.0.13
192.168.0.14
192.168.0.15
192.168.0.16
192.168.0.17
192.168.0.18
192.168.0.19
192.168.0.20
192.168.0.21
192.168.0.22
192.168.0.23
Complete list according to this network must be:

Quote:
192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
(..)
192.168.0.250
192.168.0.251
192.168.0.242
192.168.0.253
192.168.0.254
I have found this module for Perl, but I prefer a shell script:

http://search.cpan.org/~mrsam/Net-CIDR-0.11/CIDR.pm

Regards,
# 5  
Old 01-09-2007
checkout this script

ip=$( echo $1 | cut -d"/" -f1 )
no=$( echo $1 | cut -d"/" -f2 )
k=`expr 32 - $no`
nu=$(echo "2 ^ $k" | bc)
num=1
new_ip=$( echo $ip | cut -d"." -f1-3 )
while [[ $num -ne $nu ]]
do
echo $new_ip".$num"
(( num = num + 1 ))
done

Last edited by Krrishv; 01-09-2007 at 09:29 AM..
# 6  
Old 01-09-2007
Krrishv , this scripts works fine for CIDR from /24 to /32, but for bigger networks it doenst work, example:

Quote:
$
./cidr.sh 192.168.0.0/19
(..)
192.168.0.8184
192.168.0.8185
192.168.0.8186
192.168.0.8187
192.168.0.8188
192.168.0.8189
192.168.0.8190
192.168.0.8191
Thanks anyway, regards :-)
# 7  
Old 01-09-2007
yeah i will get u that script soon.
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