Monday, August 27, 2012

Repetitive Configuration Generation

There are many times when I need to generate repetitive IP address and the corresponding BGP neighbor via point to point IPs. Due to IP management scheme, it typically goes something like:

1. We beak down the block we have into /24's.
2. The first /28 of the /24 we reserve for loopback and other IPs.
3. Then use the next /28 for p2p IPs.

Obviously 'any' scripting language can figure this out, but here is 7 lines of Python that can help:

Say our blocks are 10.0.8.0/24 and 10.0.10.0/24, need to skip 10.0.9.0/24 for whatever reason.

Script:

import sys
# The range command syntex (start, stop, increment)
for net in range(8, 11, 2):
    #starting point for my last octet
    i = 16
    while i < 32: #sets the condition for upper limit
        sys.stdout.write("My IP: 10.0." + str(net) + "." + str(i) + "\t")
        sys.stdout.write("BGP Neighor: 10.0." + str(net) + "." + str(i+1) + "\n")
        i += 2

Output:

>>>
My IP: 10.0.8.16 BGP Neighor: 10.0.8.17
My IP: 10.0.8.18 BGP Neighor: 10.0.8.19
My IP: 10.0.8.20 BGP Neighor: 10.0.8.21
My IP: 10.0.8.22 BGP Neighor: 10.0.8.23
My IP: 10.0.8.24 BGP Neighor: 10.0.8.25
My IP: 10.0.8.26 BGP Neighor: 10.0.8.27
My IP: 10.0.8.28 BGP Neighor: 10.0.8.29
My IP: 10.0.8.30 BGP Neighor: 10.0.8.31
My IP: 10.0.10.16 BGP Neighor: 10.0.10.17
My IP: 10.0.10.18 BGP Neighor: 10.0.10.19
My IP: 10.0.10.20 BGP Neighor: 10.0.10.21
My IP: 10.0.10.22 BGP Neighor: 10.0.10.23
My IP: 10.0.10.24 BGP Neighor: 10.0.10.25
My IP: 10.0.10.26 BGP Neighor: 10.0.10.27
My IP: 10.0.10.28 BGP Neighor: 10.0.10.29
My IP: 10.0.10.30 BGP Neighor: 10.0.10.31
>>>

1 comment:

  1. I found a lot of interesting information here. A really good post man, very thankful and hopeful that you will write many more posts like this one.
    I found a lot of interesting information here. A really good post man, very thankful and hopeful that you will write many more posts like this one.

    ReplyDelete