Wednesday, November 12, 2014

Generating Bulk Router Configurations Using Python

As a network engineer, sometimes i need to generate bulk configurations, especially for testing purposes. Imagine you are trying to create 300 interfaces with different ip address, it will be a burden if you are writing it down by hand. Below are sample scripts that you could use as a sample to create bulk configurations.

Creating Bulk Interface Configuration with  Sequantial IP Addresses
#!/usr/local/bin/python2.7
a=0
while a<10:
    a=a+1
    #reassign value b to zero thus keep last octet between 1-255
    b=0             
    while b<255: #loop for last octet 
        b=b+4    #in every loop increase ip address by four, which corresponds to /30
        print "interface Port-channel %s" % a
        print "ip address 100.100.100.%s" % b

As seen in the script, there are two while loops. In the first loop the criteria is to keep a variable smaller than 10, which corresponds to port-channel number.

In the second loop (which is inside the first loop), the criteria is to keep b variable smaller than 255, which is the last octet value of the IP address. 


Creating Bulk Policy Configurations For Different Bandwidth Limits
a=0
x=1000000
while a<10:
  a=a+1
  cir=a*x
  bc=cir/40
  print "policy-map %sM-internet" % a
  print "  class class-default"
  print "     police cir %s bc %s conform-action transmit exceed-action drop" %(cir, bc)
  print "  set dscp default"
In python, while loops are generally not suggested if you are not really intending to do something non-lasting, instead you should use for-loops. Here is an example to create bulk configurations using for loops.
Generating Bulk BGP Configurations for IOS-XR
#!/usr/local/bin/python2.7
for x in range ( 1,20 ):
    print "!"
    print ("router bgp 65100")
    print "vrf customer-%d" %x
    print "rd 65100:%d" %(x)
    print "address-family ipv4 unicast"
    print "redistribute connected"
    print "redistribute static"
    print "neighbor 192.66.%d.2" %x
    print "remote-as 10"
    print "address-family ipv4 unicast"
    print "route-policy accept-all in"
    print " route-policy accept-all out"
    print "soft-reconfiguration inbound always"
    print "!"
    print "interface GigabitEthernet0/1/0/0.%d" %(x+10)
You may compile these scripts and see the result either in your pc, or may be on a website like http://www.compileonline.com/execute_python_online.php .

0 comments:

Post a Comment

 

Internetworking Hints Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger