Quick and dirty way to create a bunch of loopbacks with IP addresses

This can be handy when doing some sort of BGP lab. There are ways to pull various source data with real prefixes into BGP, but sometimes you just want to create a bunch of local addresses.

Daniel helped me out with this initially and I’ve simple added a couple of things.

This is the script on my linux box:

#!/usr/bin/perl
for ($ip=1; $ip<256; $ip++)
{
print "interface loopback$ip\n";
print "ip address 1.2.3.$ip 255.255.255.255\n";
print "ip address 2.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 3.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 4.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 5.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 6.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 7.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 8.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 9.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 10.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 11.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 12.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 13.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 14.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 15.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 16.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 17.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 18.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 19.2.3.$ip 255.255.255.255 secondary\n";
print "ip address 20.2.3.$ip 255.255.255.255 secondary\n";
}

Running the script spits out a ton of IPs:

./IP_Generate.pl
interface loopback1
ip address 1.2.3.1 255.255.255.255
ip address 2.2.3.1 255.255.255.255 secondary
ip address 3.2.3.1 255.255.255.255 secondary
ip address 4.2.3.1 255.255.255.255 secondary
ip address 5.2.3.1 255.255.255.255 secondary
ip address 6.2.3.1 255.255.255.255 secondary
ip address 7.2.3.1 255.255.255.255 secondary
ip address 8.2.3.1 255.255.255.255 secondary
ip address 9.2.3.1 255.255.255.255 secondary
ip address 10.2.3.1 255.255.255.255 secondary
ip address 11.2.3.1 255.255.255.255 secondary
ip address 12.2.3.1 255.255.255.255 secondary
ip address 13.2.3.1 255.255.255.255 secondary
ip address 14.2.3.1 255.255.255.255 secondary
ip address 15.2.3.1 255.255.255.255 secondary
ip address 16.2.3.1 255.255.255.255 secondary
ip address 17.2.3.1 255.255.255.255 secondary
ip address 18.2.3.1 255.255.255.255 secondary
ip address 19.2.3.1 255.255.255.255 secondary
ip address 20.2.3.1 255.255.255.255 secondary
interface loopback2
ip address 1.2.3.2 255.255.255.255
ip address 2.2.3.2 255.255.255.255 secondary
ip address 3.2.3.2 255.255.255.255 secondary
ip address 4.2.3.2 255.255.255.255 secondary
ip address 5.2.3.2 255.255.255.255 secondary
ip address 6.2.3.2 255.255.255.255 secondary
ip address 7.2.3.2 255.255.255.255 secondary
ip address 8.2.3.2 255.255.255.255 secondary
ip address 9.2.3.2 255.255.255.255 secondary
ip address 10.2.3.2 255.255.255.255 secondary
ip address 11.2.3.2 255.255.255.255 secondary
ip address 12.2.3.2 255.255.255.255 secondary
ip address 13.2.3.2 255.255.255.255 secondary
ip address 14.2.3.2 255.255.255.255 secondary
ip address 15.2.3.2 255.255.255.255 secondary
ip address 16.2.3.2 255.255.255.255 secondary
ip address 17.2.3.2 255.255.255.255 secondary
ip address 18.2.3.2 255.255.255.255 secondary
ip address 19.2.3.2 255.255.255.255 secondary
ip address 20.2.3.2 255.255.255.255 secondary
interface loopback3
ip address 1.2.3.3 255.255.255.255
ip address 2.2.3.3 255.255.255.255 secondary
ip address 3.2.3.3 255.255.255.255 secondary
ip address 4.2.3.3 255.255.255.255 secondary
ip address 5.2.3.3 255.255.255.255 secondary
ip address 6.2.3.3 255.255.255.255 secondary
ip address 7.2.3.3 255.255.255.255 secondary
ip address 8.2.3.3 255.255.255.255 secondary
ip address 9.2.3.3 255.255.255.255 secondary
ip address 10.2.3.3 255.255.255.255 secondary
ip address 11.2.3.3 255.255.255.255 secondary
ip address 12.2.3.3 255.255.255.255 secondary
ip address 13.2.3.3 255.255.255.255 secondary
ip address 14.2.3.3 255.255.255.255 secondary
ip address 15.2.3.3 255.255.255.255 secondary
ip address 16.2.3.3 255.255.255.255 secondary
ip address 17.2.3.3 255.255.255.255 secondary
ip address 18.2.3.3 255.255.255.255 secondary
ip address 19.2.3.3 255.255.255.255 secondary
ip address 20.2.3.3 255.255.255.255 secondary
etc
etc
etc
...

You can redirect that to a text file to import or whatever. Adjust as needed

© 2009-2018 Darren O'Connor All Rights Reserved -- Copyright notice by Blog Copyright