Dabbling in the Dark Arts

It seems to me that learning a bit of Python would probably be a really stinkin’ good idea right about now. Don’t get me wrong, I am NOT one of those folks who thinks that if you are a network engineer who does not learn to program you are going to be obsolete by the end of the month… and I don’t think that (from what I can tell) network engineers are going to need super deep programming skills… BUT I do think that network engineers need to evolve a bit and learn some other cool stuff. Worst case, you learn a bit, never have to use that skill, and are no worse for wear.

Anywho, this post doesn’t really have anything to do with routing or Cisco or Juniper and how Python can interact with their respective gear (although hopefully this will lead down that road eventually), but instead is just a blurb about my first little foray into playing with Python.

Many moons ago a friend at an old job gave me a teraterm macro that called on a list of devices and a list of commands. The macro would load up and ask you for some credentials, then go out to poke all the devices and record the output of the commands you asked it to do. This was a great tool, I used it all the time to bulk update stuff like a new NTP server for a bunch of access switches or something along those lines. One big problem with the tool was that teraterm kinda sucked! Also for some reason it never seemed to want to work with SSH which is obviously an issue (hopefully everybody’s devices have telnet disabled!).

So, I was bored over the holidays and saw some stuff about Python and saw some scripts some server guys did to go out and touch servers and do xyz tasks. I figured if server guys can do it, then I can probably do it (like way better too) :)!

From zero Python experience, in two afternoons, here is my creation thus far. I have of course relied HEAVILY on my dear friend the Googles, and the posts of far smarter people than me, but other than checking out some examples this monstrosity is my own little creation. I know that Python supposedly favors flat instead of nested (funny/awesome Zen of Python: http://www.python.org/dev/peps/pep-0020/), but I ended up with a decent amount of nested loops… perhaps from the one semester of C++ I took way back when?

Here she is, anybody who actually knows Python feel free to comment and tell me how wrong I’m doing it 🙂 I’ll keep this link as the ‘master’ script if I make any changes.

Linky: https://www.dropbox.com/s/qvks9e4g9kn24sg/SSH.py

To run it, just cd to the directory and run ‘python [scriptname].py’ make sure that your device list, credentials list, and commands list are in txt files named per the script in the same directory. Those files should just have one entry per line — for credentials, line 1 = username, line 2 = password, and line 3 = enable password.

I’ve only tested it out on a few devices at my house, so play with it and let me know if it works or fails miserably and any other things that would be cool to have in there. That would give me a cool little side project to keep on playing with Python.

Also note that I built this on a mac and have not tested any other platforms. I used the default Python install but did have to install IPy and perhaps another package, can’t recall at the moment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.