Python script for end-system VLAN Distribution for NAC

  • 2
  • Idea
  • Updated 4 years ago
If anyone is using this solution, I am posting a quick script to create the files needed to create the policies.  There is a Bash script in the solution doc but python may be a little more accessible. It is native on Mac OS X.  use this syntax: python <script name> <number of vlans>.  

#!/usr/bin/pythonimport sys

#this program will take the following arguments
# -number of VLAN's to distribute
# This will create one file for each vlan
# the name will begin with the name in arg[2]
# for loop for each file name
# for loop to add each mac range line
vlancount=sys.argv[1]
macspervlan=int(round(256/int(vlancount),0))

#because the last digit of the MAC does not seem to be normally distributed,
#we have to use the entire 6th octet of the MAC

macbase1 = "00:"*5
macbase2 = macbase1+"0"
macbase3 = macbase1+"FF"
octetsix = 0
i=0 # used to count the number of lines written to each file
k=1 # used as a counter to increment file names
while octetsix < 256:
# create and open a file
policyfile = open("vlan-dist-policy-{:0>2d}.txt".format(k),"w")
for i in range(i, macspervlan):
# write a line to the file
if octetsix<256:
policyfile.write(macbase1 + "{:0>2X}".format(octetsix)+"/"+"00:"*5+"FF\n")
octetsix+=1
else:
i=macspervlan
else:
i=0
k+=1
policyfile.close()
else:
print "Successfully created {} MAC distribution policy files.".format(k-1)
print macspervlan



Photo of Jon Linton

Jon Linton

  • 580 Points 500 badge 2x thumb

Posted 4 years ago

  • 2

Be the first to post a reply!