I’m working hard on learning Python, and while learning my new job at dCloud I was able to inspect some other scripts to perform some network automation created by Dustin over at packetsmurf.org. Pulling it apart I noticed a library called netmiko which was developed by Kirk Byers. Netmiko is an expansion on Paramiko which is used to create SSH connections and push commands to network devices.

I wanted to start messing around with Netmiko a little, but the truth is that my basic Python is still a little weak to be any good with it. Still, I stepped through the setup on my Ubuntu box and started using it anyway.

 

 

The first step was to set up a Python 3 virtual environment to import the netmiko library into. Technically this isn’t required, but it’s a lot cleaner to do libraries within a virtual envoronment in case there is not a global need for the library. It also helps differentiate Python2 and Python3 if you have both installed on your dev machine (which I do).

First step: Set up a virtual environment.

 

The next step was to use pip to install the netmiko library within the virtual environment.

PIP3 makes installing libraries a breeze.

 

Once netmiko was installed, I decided to try and use it with EVE-NG as I have no physical gear to reach out and touch. First I wrote a script to connect to R10 in my lab, which had a minimal config (hostname and crypto key, SSH activated, vty allowed ssh, no login on vty required) but I could not get the script to work. I kept getting tracebacks on the script having to do with making the connection. I decided to leave Python alone and troubleshoot the basic connectivity.

It turns out, EVE only does port forwarding for telnet, not SSH. I could not SSH to any of my devices in my EVE lab for that reason. The workaround was to add a MgmtCloud0 to the lab, which allows a device to be configured with an IP on the same subnet as EVE’s management subnet. This exposes the device connectivity without having to do port forwarding. Credit for that goes to one of EVE’s support staff and friend of mine, Mike Doe.

 

 

With this setup I could use Python and netmiko to send commands to the router. I actually didn’t like having to do all the extra SSH setup for the lab and so I instead found that netmiko has a telnet option for ios. I modified my script, wiped the router (only adding an IP for management) and was able to push configuration to the router from Python to bootstrap it.

 

This Python script took an embarrassing amount of trial and error to get the netmiko arguments and commands working.

 

 

The output of the Python script. I checked EVE and sure enough, R10 was configured.

 

Obviously, the configuration and script I pushed was very minimal, but this was more a POC than anything useful. More of a stepping stone on the path than a destination, but it was a lot of fun. When I get better with automation I hope to look back at this stumbling first step as a good start.

 

Categories: LinuxPython