Hello
After I created a script in Netmiko that connected to a number of devices to create an uptime report, I thought it would be nice to do a similar thing using Unicon. It was a much easier job.
here’s the python code that does the magic:
from unicon import Connection from jinja2 import Environment, FileSystemLoader import yaml from pyats.topology import loader import sys def executeshowongroup(command,testbedfile,devicelist): realdevicelist = devicelist.split('%') print(realdevicelist) testbed = loader.load(testbedfile) x = 0 for x in range(x,len(realdevicelist)): devicename = realdevicelist[x] c = testbed.devices[devicename] c.connect() c.execute(command) if name == "main": command = sys.argv[1] testbedfile = sys.argv[2] devicelist = sys.argv[3] executeshowongroup(command,testbedfile,devicelist)
and here’s the testbed.yml:
devices: myproxy: os: linux type: linux credentials: default: username: cisco password: cisco connections: cli: protocol: ssh ip: 1.1.1.1 port: 722 router1: os: ios type: router connections: defaults: class: unicon.Unicon cli: command: connect router1 proxy: myproxy router2: os: ios type: router connections: defaults: class: unicon.Unicon cli: command: connect router2 proxy: myproxy
Now I run the script with:
python3 unicongroupscript.py ‚show ip int brief’ newtestbed.yml router1%router2
It’s important to notice what is NOT in this script: gone are the time.sleep() commands, so i don’t need to worry if my proxy is particularly slow on any given day. I get the output much, much faster than in the case of netmiko. And the whole logic is done in python, unlike in Ansible where I’m artificially limited to the yaml syntax.
This is really, really cool stuff. One thing that is missing is the built-in group functionality in the testbed file, so scripts need to be run on individual hosts (or like in my case: with groups of hosts where delimiter between each host is %). I’ll try to talk to Cisco about this.