Sometimes you just need a quick and dirty python command to do something accurately to assist in the creation / population of templates which saves manual effort and checking. Now I do use python extensively and often write fairly complex scripts for certain tasks but sometimes you just need a quick command to perform a specific task so you can get something else done ASAP. I’ll include these useful code snippets on this page as I use them as they can be quite helpful and saves me figuring them out as I need them.
Create switch ports in order to populate excel spreadsheets
This is quite a straightforward example to create strings to populate cable map excel spreadsheets, effectively A – B mappings for device installs.
>>> l = [ x for x in range(49)]
>>> l.pop(0)
>>> l1 = l[0:48:2]
>>> l2 = l[1:48:2]
>>> for x, y in zip(l1, l2):
... print('Eth1/0/' + str(x))
... print('Eth1/0/' + str(y))
... print('Eth1/0/' + str(x))
... print('Eth1/0/' + str(y), '\n')
The code is quite straightforward, create a list reperesenting the 48 ports of the switch, then create 2 new lists using slicing to only get odd or even ports. Final stage is using a for loop to print out the results as required in a block of four ports. Of course this last stage can of course be changed to include a different string if required plus different ordering as required.