awk, print all ip's in subnet


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk, print all ip's in subnet
# 1  
Old 06-02-2010
awk, print all ip's in subnet

I've searched for options to list out all ip's in a given range, up to a class B. The best option I found was here on unix.com.
Code:
awk -F"." '{ if(NR==1){ for(i=$4;i<=254;i++) print $1"."$2"."$3"."i >> "ucheck.input" } else \
{ for(i=1;i<=$4;i++) print $1"."$2"."$3"."i >> "ucheck.input" }} ' input_file1

The input file has two entries, the beginning and ending ip. It works ok, except when trying to print out something like a /25. 10.1.1.0/25 will print all 255 ip's instead of 10.1.1.0 to 10.1.1.128.

---------- Post updated 06-02-10 at 10:09 AM ---------- Previous update was 06-01-10 at 05:49 PM ----------

Does any AWK guru's have any suggestions for me? Maybe if this is thought of as a number with four fields and each field can be a number from 0 to 255. I am only concerned with fields 3 and 4, for each 3rd field increase, 4th field will run though 1 to 255.
# 2  
Old 06-02-2010
Why don't you post the input file and the desired output?
# 3  
Old 06-02-2010
Oops, that would have been a good idea, sorry.

Input example, the first line is the starting address and the second line is the ending.
Code:
192.168.53.1
192.168.54.255

Output I would like is the list from beginning to end. The first and second fields can be different on each input but will never increment. Only fields 3 and 4 will increment from 0 to 255.
Code:
192.168.53.1
192.168.53.2
192.168.53.3
192.168.53.4
>>>continue to 255 then start on next subnet>>>
192.168.53.254
192.168.53.255
192.168.54.1
192.168.54.2
192.168.54.3
>>> continue to 255
192.168.54.253
192.168.54.254
192.168.54.255

# 4  
Old 06-02-2010
If you write a pair of functions which can convert ip addresses to-from quad-dotted string and integer representations (akin to inet_aton and inet_ntoa), then the task is just a for loop that runs so long as the current ip is less than or equal to the upper bound of the range. And, if you don't mind printing out the network address (which your example skips, e.g. 192.168.54.0), then you don't even have to bother with masks.

Regards,
Alister

---------- Post updated at 09:05 PM ---------- Previous update was at 08:59 PM ----------

Nevermind. I just noticed ...

Quote:
Originally Posted by numele
Does any AWK guru's have any suggestions for me? Maybe if this is thought of as a number with four fields and each field can be a number from 0 to 255. I am only concerned with fields 3 and 4, for each 3rd field increase, 4th field will run though 1 to 255.
You don't need anything so general as to require inet_aton/inet_ntoa functionality.

I ought to learn to look before I leap. Smilie

---------- Post updated at 09:44 PM ---------- Previous update was at 09:05 PM ----------

Perhaps this AWK may be of use to you:
Code:
awk -F. 'NR==1 {pre=$1"."$2; a=$3} NR==2 {b=$3} END {for(i=a;i<=b;i++) for(j=1;j<=255;j++) print pre"."i"."j}'

Or this sh:
Code:
a=192.168.53.1 b=192.168.54.255
pre=${a%.*.*}
a=${a#*.*.}
a=${a%.*}
b=${b#*.*.}
b=${b%.*}
eval printf \'%s\\n\' $pre.\{$(jot -s, - $a $b 1)\}.\{$(jot -s, 255)\}

If you aren't using a BSD-ish system, then you may not have jot. In that case, should you wish to use the sh solution, check for the availability of seq (and adjust the commands accordingly).

Regards,
Alister
# 5  
Old 06-03-2010
Thank you alister, I made a couple of changes to your awk statement and it works great for me. The changes are for when field 4 is less than 255.
Code:
awk -F. 'NR==1 {pre=$1"."$2; a=$3; d=$4} NR==2 {b=$3; c=$4} END {for(i=a;i<=b;i++) for(j=d;j<=c;j++) print pre"."i"."j}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

AWK print AWK command

awk '{print "awk '{sub(/pdb_00/,"pdb_"$0"_00"); print}' pdb_"$0"_00.namd > tempo"; print "mv tempo pdb_"$0"_00.namd"}' datA2.dat > copy_script2.bash This works when trying to print 'sed etc. etc' but if I switch to using AWK to print a set of AWK commands it doesn't work... e.g. this... (3 Replies)
Discussion started by: chrisjorg
3 Replies

4. IP Networking

2 ip from one subnet my isp

Hi. my english is not so good. sorry. i have some problem. My isp give me second ip from subnet. One network is working, but secong don't. fxp0 - my network dc0 - network isp (that working) re0 - network isp (don't working) i try use ng_one2many, but it's don't working ngctl mkpeer... (0 Replies)
Discussion started by: kil
0 Replies

5. IP Networking

How many hosts per subnet

What would be a realistic number of hosts in a 100BaseT network, before you have to start thinking on subnetting further? Or in another words. How many hosts would you keep in the same broadcast domain? In a 100BaseT network, with subnet 10.20.20.0/24, I could have 254 hosts as part of the same... (3 Replies)
Discussion started by: Aia
3 Replies

6. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

7. IP Networking

How do I figure out the subnet?

Hi, How do I get subnet from this: 10.252.0.138/25 Tnx (2 Replies)
Discussion started by: mehrdad68
2 Replies

8. IP Networking

Migrating existing Subnet to a new subnet and changing ip addresses in UNIX

Hi, My project needs to migrate the existing Subnet (255.255.255.0) to a new subnet and change the ipaddresses (currently C class). How can I do that. I need some information. John (0 Replies)
Discussion started by: johnmarsh
0 Replies

9. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies

10. IP Networking

Subnet mask

Hi, I have about 30 computers for users with subnet mask x.x.x.0, and 25 computers for workers with s.m. x.x.x.128. My server has a s.m. x.x.x.128 so with workers computers I can see my server and all the computers in that s.m., but I can't see the server from the users computers and I need to see... (7 Replies)
Discussion started by: Z0DiaC
7 Replies
Login or Register to Ask a Question