Skip to content

Instantly share code, notes, and snippets.

@ArtiomL
Last active December 16, 2015 19:19
Show Gist options
  • Save ArtiomL/5484454 to your computer and use it in GitHub Desktop.
Save ArtiomL/5484454 to your computer and use it in GitHub Desktop.
csgen - Cisco SVI Sequence Configuration Generator
#!/usr/bin/python
# csgen - Cisco SVI Sequence Configuration Generator
# (CC0) No Rights Reserved
# Artiom Lichtenstein
# v1.2, 29/04/2013
import sys
def fun_PRINT_INSTRUCTIONS():
print "\nUsage: ./csgen {First SVI Number} {Base IP Address/CIDR} {IP Address Octet Number to Rotate} {Last SVI Number} [Additional Config Lines devided by '+']"
print "Examples:"
print "./csgen 100 192.168.100.254/24 3 113"
print "./csgen 13 10.13.1.254/16 2 17"
print "./csgen 13 10.13.1.254/16 2 17 'no ip unreachables'"
print "./csgen 13 10.13.1.254/16 2 17 'no ip unreachables+ip router ospf 1 area 0+no shut'\n"
if (len(sys.argv) < 5) or (int(sys.argv[1])>int(sys.argv[4])) or (int(sys.argv[3])>4):
fun_PRINT_INSTRUCTIONS() # Print usage instructions when there are not enough arguments or arguments are invalid
sys.exit(1)
int_FIRST_SVI = int(sys.argv[1])
str_IP_AND_MASK = sys.argv[2]
int_ACTIVE_OCTET = int(sys.argv[3])-1
int_LAST_SVI = int(sys.argv[4])
lst_IP_AND_MASK = str_IP_AND_MASK.split("/") # IP Address and Mask separation
lst_OCTETS = lst_IP_AND_MASK[0].split(".") # IP Octets separation
str_MASK = lst_IP_AND_MASK[1]
str_BINARY_MASK = ""
for i in range (0,32): # Create a binary representation of the CIDR Mask
if i < int(str_MASK):
str_BINARY_MASK = str_BINARY_MASK + "1"
else:
str_BINARY_MASK = str_BINARY_MASK + "0"
if (i==7) or (i==15) or (i==23):
str_BINARY_MASK = str_BINARY_MASK + "."
lst_MASK_OCTETS = str_BINARY_MASK.split(".") # Separate the Mask Octets
for i in range (int_FIRST_SVI, int_LAST_SVI+1):
print "\ninterface Vlan" + str(i)
print " ip address " + lst_OCTETS[0] + "." + lst_OCTETS[1] + "." + lst_OCTETS[2] + "." + lst_OCTETS[3] + " " + str(int(lst_MASK_OCTETS[0],2)) + "." + str(int(lst_MASK_OCTETS[1],2)) + "." + str(int(lst_MASK_OCTETS[2],2)) + "." + str(int(lst_MASK_OCTETS[3],2))
lst_OCTETS[int_ACTIVE_OCTET] = str(int(lst_OCTETS[int_ACTIVE_OCTET])+1)
if ( (int_ACTIVE_OCTET==3) and (int(lst_OCTETS[int_ACTIVE_OCTET])==255) or (int_ACTIVE_OCTET!=3) and (int(lst_OCTETS[int_ACTIVE_OCTET])==256) ):
sys.exit("\nNo more addresses available for the Rotating IP Octet!\n")
if len(sys.argv)==6:
lst_STATIC_LINES = sys.argv[5].split("+")
for j in range (0,len(lst_STATIC_LINES)):
print " " + lst_STATIC_LINES[j]
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment