Question

This is a difficult one to extrapolate what I need, so bear with me. Let's say we have this subnet 10.4.x.0/28 which iterates itself across multiple subnets that look the same, i.e

10.4.0.0/28
10.4.0.16/28
10.4.0.32/28  etc...

and we have a single address in these subnets that we need to extract that will always be +12 of the current subnet. i.e

10.4.0.0/28  ->  10.4.0.12
10.4.0.16/28  -> 10.4.0.28
10.4.0.32/28  ->  10.4.0.44

How would I go about writing a shell script to loop through all of the subnets from

10.4.0.0/28 to 10.4.255.240 to get the single address in each subnet?

Was it helpful?

Solution

Hard to tell what format you want exactly, but ...

for I in $(seq 12 16 256); do echo 10.4.{0..255}.$I; done

Gives 16 lines with 4096 values like this...

10.4.0.12 10.4.1.12 10.4.2.12 10.4.3.12 10.4.4.12 10.4.5.12 10.4.6.12 10.4.7.12 10.4.8.12 10.4.9.12 10.4.10.12 10.4.11.12 10.4.12.12 10.4.13.12 10.4.14.12 10.4.15.12 10.4.16.12 10.4.17.12 ...
10.4.0.28 10.4.1.28 10.4.2.28 10.4.3.28 10.4.4.28 10.4.5.28 10.4.6.28 10.4.7.28 10.4.8.28 10.4.9.28 10.4.10.28 10.4.11.28 10.4.12.28 10.4.13.28 10.4.14.28 10.4.15.28 10.4.16.28 10.4.17.28 ...
[...]
10.4.235.252 10.4.236.252 10.4.237.252 10.4.238.252 10.4.239.252 10.4.240.252 10.4.241.252 10.4.242.252 10.4.243.252 10.4.244.252 10.4.245.252 10.4.246.252 10.4.247.252 10.4.248.252 10.4.249.252 10.4.250.252 10.4.251.252 10.4.252.252 10.4.253.252 10.4.254.252 10.4.255.252

If you want to change the top-level addresses on the fly, you could make a function...

iterateIP(){
     for I in $(seq 12 16 256); do echo $1.{0..255}.$I; done 
}

And call like this:

iterateIP 10.4
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top