I am trying to find a script that will generate all the ip addresses in particular range.
Example: start: 41.0.0.0 end ip 41.1.1.2
32.32.35.3 to 32.32.36.0
Please help.
Thanks
I am trying to find a script that will generate all the ip addresses in particular range.
Example: start: 41.0.0.0 end ip 41.1.1.2
32.32.35.3 to 32.32.36.0
Please help.
Thanks
There was a recent thread about that subject here: /t/awk-print-all-ips-in-subnet/266853/1
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
works great if the input file is like
192.168.0.1
192.168.3.1
but my input is a bit different: 192.168.0.1 192.168.3.1
just need a little tweak for awk i guess.
Thanks
Hi, i modified the awk to make it more universal. It does not take netmasks into account or anything so it includes broadcast addresses for instance.
awk 'NR==1{split($1,A,".")}
NR==2{split($1,B,".")}
END {for(i=A[1];i<=B[1];i++)
for(j=A[2];j<=((i==B[1])?B[2]:255);j++)
for(k=A[3];k<=((j==B[2])?B[3]:255);k++)
for(l=A[4];l<=((k==B[3])?B[4]:255);l++)
print i"."j"."k"."l}' infile
Place this awk code in a file.
BEGIN { FS="[. ]" }
END {
a1=$1; b1=$2; c1=$3; d1=$4;
a2=$5; b2=$6; c2=$7; d2=$8;
while( d2 >= d1 || c2 > c1 || b2 > b1 || a2 > a1) {
if (d1 > 255) { d1=1; c1++; }
if (c1 > 255) { c1=1; b1++; }
if (b1 > 255) { b1=1; a1++; }
print a1"."b1"."c1"."d1
d1++
}
}
Suppose you name this file ip.awk. Then
$ echo "192.168.0.1 192.168.3.1" | awk -f ip.awk
generates an inclusive list of all IP addresses between 192.168.0.1 and 192.168.3.1, one per line.
super thank you.
Most recent Linux distro's have ipcalc that will perform this very operation.
I don't think (s)ipcalc enumerates IP ranges, does it?
You are right. It does not.
You're correct sipcalc doesn't... still useful though.
I guess I failed to see the original focus of the question from kkkk.
Cheers!