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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Expand IP address range 2

Status
Not open for further replies.

rkckjk

IS-IT--Management
Apr 27, 2001
34
US
I have an input file listed below which contains IP addresses with
other information tacked on after each address. Some of the IP address
are set up in ranges with a '-' and some of them have the wildcard
('*') within them. I need to do is read the file and create a new file
that has non-expanded ip addresses expanded within the range specified
from the existing file. Some of the addresses below also have multiple
'-'s. All the '-'s would have to be expanded. I don't care about
expanding the ip address with the wildcards (*), just the ones with
the '-'s in them.

Input File
===========
10.40.6.130:m0ther:*:8:3:120::m00se:
10.40.6.131:m0ther:*:8:3:120::m00se:
10.40.6.140:m0ther:*:8:3:120::m00se:
10.42.1-3.240-243:m0ther:*:8:2:120::m00se:
10.60.1.2-3:m0ther:*:8:3:120::m00se:
10.60.2.*:m0ther:*:8:2:300::m00se:
10.90.*.*:m0ther:*:8:2:240::m00se:
10.100.*.231-235:m0ther:*:8:2:120::m00se:
152.10-12.*.*:m0ther:*:8:3:120::m00se:

The new output file should look like the following:

Output File
===========
10.40.6.130:m0ther:*:8:3:120::m00se:
10.40.6.131:m0ther:*:8:3:120::m00se:
10.40.6.140:m0ther:*:8:3:120::m00se:

10.42.1.240:m0ther:*:8:2:120::m00se:
10.42.1.241:m0ther:*:8:2:120::m00se:
10.42.1.242:m0ther:*:8:2:120::m00se:
10.42.1.243:m0ther:*:8:2:120::m00se:
10.42.2.240:m0ther:*:8:2:120::m00se:
10.42.2.241:m0ther:*:8:2:120::m00se:
10.42.2.242:m0ther:*:8:2:120::m00se:
10.42.2.243:m0ther:*:8:2:120::m00se:
10.42.3.240:m0ther:*:8:2:120::m00se:
10.42.3.241:m0ther:*:8:2:120::m00se:
10.42.3.242:m0ther:*:8:2:120::m00se:
10.42.3.243:m0ther:*:8:2:120::m00se:

10.60.1.2:m0ther:*:8:3:120::m00se:
10.60.1.3:m0ther:*:8:3:120::m00se:

10.60.2.*:m0ther:*:8:2:300::m00se:
10.90.*.*:m0ther:*:8:2:240::m00se:

10.100.*.231:m0ther:*:8:2:120::m00se:
10.100.*.232:m0ther:*:8:2:120::m00se:
10.100.*.233:m0ther:*:8:2:120::m00se:
10.100.*.234:m0ther:*:8:2:120::m00se:
10.100.*.235:m0ther:*:8:2:120::m00se:

152.10.*.*:m0ther:*:8:3:120::m00se:
152.11.*.*:m0ther:*:8:3:120::m00se:
152.12.*.*:m0ther:*:8:3:120::m00se:

I separated the output file here in this email with a blank line to
make it easier to view the expanded records, the blank line shouldn't
be in the final output file. I think this can be done in AWK, I'm just
not sure how to accomplish it.

Thanks
Rich K.
 
This may do what you want.

BEGIN { FS=OFS=":"}
{
split($1,a,".")
$1=""

n1=split(a[1],a1,"-")
n2=split(a[2],a2,"-")
n3=split(a[3],a3,"-")
n4=split(a[4],a4,"-")

for (j1=a1[1];j1<=a1[n1];j1++) {
b1 = j1 &quot;.&quot;
for (j2=a2[1];j2<=a2[n2];j2++) {
b2 = b1 j2 &quot;.&quot;
for (j3=a3[1];j3<=a3[n3];j3++) {
b3 = b2 j3 &quot;.&quot;
for (j4=a4[1];j4<=a4[n4];j4++) {
print b3 j4 $0
}
}
}
}
}


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Thanks alot CaKiwi, the AWK script works great. I have another question maybe you could help me with. I now have an IP address file that I now want to compare against expanded IP address file like the one below:

Sample of the expanded IP address file
======================================
172.16.*.*:m0ther:*:8:2:300::m00se:
172.16.160.102:m0ther:*:8:2:300::m00se:
172.30.220.119:public:*:8:2:60::private:
10.5.6.*:m0ther:*:8:2:86400::m00se:
10.20.1.*:public:*:8:2:300::private:
10.20.5.*:public:*:8:2:300::private:
10.40.9.*:public:*:8:2:300::private:
10.60.4.*:m0ther:*:8:3:120::m00se:
192.6.*.250:public:*:8:2:120::private:
192.168.134.*:m0ther:*:8:2:86400::m00se:
208.152.32.17:public:*:8:2:120::private:
208.152.32.18:public:*:8:2:120::private:
*.*.*.*:public:*:8:2:86400::private:

Sample of the Input File
========================
10.20.1.161
10.40.9.152
10.60.1.8
172.30.110.27
172.30.220.36
172.16.76.132
172.16.76.133
10.60.4.34
10.60.4.36
192.6.35.250
208.152.32.17


Every record from the input file should be read and matched against the expanded IP address file to see if there is an exact ip address match or if it matches with a wild card '*' IP address match. Example output after comparison:

Output of Comparison
====================
10.60.1.8 not found
172.30.110.27 not found
172.30.220.36 not found

As you can see the last record of the expanded list is:
*.*.*.*:public:*:8:2:86400::private:

There are four '*' wildcards in this record, so all the ip addresses should match-up with this line, so we can ignore this comparison check. I hope you can help me out.
Thanks
Rich K.

 
(Thanks, marsd)

rkckjk

This is not very elegant, but seems to works.

BEGIN {
FS=&quot;:&quot;
while ((getline < fn) > 0) {
n++
a[n] = $0
}
}
{
split($1,b1,&quot;.&quot;)
if (b1[1]==&quot;*&quot; && b1[2]==&quot;*&quot; && b1[3]==&quot;*&quot; && b1[4]==&quot;*&quot;) next
for (j=1;j<=n;j++) {
split(a[j],b2,&quot;.&quot;)
if ((b1[1]==&quot;*&quot;||b1[1]==b2[1]) && (b1[2]==&quot;*&quot;||b1[2]==b2[2]) && (b1[3]==&quot;*&quot;||b1[3]==b2[3]) && (b1[4]==&quot;*&quot;||b1[4]==b2[4])) flg[j]=1
}
}
END {
for (j=1;j<=n;j++) {
if (!flg[j]) print a[j] &quot; not found&quot;
}
}


CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Sorry, I should have told you how to run it

awk -v fn=&quot;file2&quot; -f scipt.awk file1

CaKiwi

&quot;I love mankind, it's people I can't stand&quot; - Linus Van Pelt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top