Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Beginner's question: How to generate a list of IP addresses?

Status
Not open for further replies.

hanke72

Technical User
Mar 10, 2005
2
IE
Hi,
I just started to learn awk and thought this would be easy. I hoped I could find examples which would do similar things and modify those examples for my purpose, but I can't get it to work.

I'd like to generate all IP addresses of a Class B subnet, e.g.
10.0.0.0/16

10.0.0.0
10.0.0.1
...
10.0.0.255
10.0.1.0
...
10.0.255.255


I thought I could combine something like this "BEGIN {for ( i=0; i<256; i++)" with a text file (ip.txt) that contains the first 3 bytes. The first part was easy, so the text file would look like:

10.0.0.
10.0.1.
...
10.0.255

But I can't get each line ($0) printed 256 times together with a number incremented by one until it reaches 255.

Can you help me? Is the approach ok or would you use a different way?

Kind Regards,

P.
 
Something like this ?
awk '{for(i=0;i<256;++i)printf "%s%d\n",$0,i}' ip.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Wow, I would have never figured that out, I guess :)

Thank you so very much. It's almost 1:30 am and I was still trying and I didn't expect such a quick answer.

Thanks a million !!

Best Regards,

Pascal
 
If you want to dispense with the text file and generate the last 2 bytes:

Code:
# Generate all IP addresses, given the first 2 bytes.
BEGIN { prefix = "10.0"
  for (i=0; i<256; i++)
    for (j=0; j<256; j++)
      printf "%s.%d.%d\n", prefix, i, j
}

For an introduction to Awk, see faq271-5564.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top