Junos PyEZ in Interactive Mode

PyEZ Screenshot

Watching Jeremy Schulman demonstrate his Junos PyEZ module is fascinating, because he does pretty much everything in Python’s interactive mode. I’m more used to writing a script, trying to run it, editing it again, running it again, and so forth. For more complex scripts or repetitive tasks clearly my way is better(!), but for quick checks the interactive mode is super helpful, and it’s great for testing command syntax and data structures to put into a script. It could also, perhaps, become a CLI alternative for some people, and a quick way to pull together information on a device. That might sound absurd until you watch Jeremy working on the Python command line and realize that he can pull out information faster than you probably can on the Junos CLI.

So let’s assume you’d like to try interactive mode as well. There are a bunch of commands you probably want to run every time you do this, so how do you avoid having to type that stuff every time? I’ll be testing in Ubuntu as usual, but this should work on most linux-based systems as it’s pretty basic stuff.

PyEZ Connect Script

This script contains a number of commands to be run before Python hands over control to you in interactive mode. We want to import some standard modules (including the basic Junos PyEZ Device object) and connect to a device. We’ll feed the device name in via a wrapper, and we’re using a hard coded username of ‘script’. This text file is called “connect.py”:

We’re importing a few modules, configuring the username to use (script), requesting the password interactively (this I stole from Jeremy’s guides), configure the device object then try to open it. In this case I’m trying to deal with failure politely as well, although in turn I’m also suppressing any helpful exception information (so you wouldn’t know why the connection fails if it were to fail), but that’s a compromise on my part. At least you’ll know for sure it did fail!

Wrapper Script

The connect.py script is expecting to be given the hostname on the command line, but rather than have to invoke Python manually each time, I’ve created a shell script called “pyez” that calls python in interactive mode and tells it to run the commands in connect.py before handing off to the user. It looks something like this:

This sits in the same directory as connect.py and has the execute bit set (chmod +x) so it can be run.

Trying It Out

Hopefully then if I call the wrapper with a hostname (note that there’s no sanity checking going on), it should ask me for a password, then try to connect. Let’s see a successful connection first:

And now failing due to a bad password:

Now What?

The >>> , by the way, is the interactive Python prompt. But what can we do with it? Let’s start by just listing some basic information about the chassis:

Hmm. Lots of info there, but in a very unfriendly format, isn’t it? Our connect.py script imported pretty-print (pprint) as “pp” though, so making that result look nicer is simple – just wrap it in a call to pretty print.

That looks a little better, doesn’t it? Extracting individual elements is as easy as referencing the name as an element of ‘facts’:

We can also load modules on demand (or if we do this a lot, we’d want to push the loading into connect.py):

Notice that helpfully you are told how many items were loaded into the table as it was populated (12 in this case).

We can peek at the data a few different ways perhaps:

Or:

Or maybe:

At this point, it really becomes a challenge of “how many ways can you do this in Python?”

And There It Is

I’ll come back to some more interactive stuff in the future, but suffice it to say that it is a handy way to start playing with the Junos PyEZ modules and device connectivity.

2 Comments on Junos PyEZ in Interactive Mode

  1. This is a great place for network guys. My questions is kind of a little off, but let see if anyone can help me with this. I am a newbie to python, I have been working with juniper with quit a while (long enough to understand junos/juniper devices), the problem i m facing is how to load the pyez module with python on windows machine. I tried to install by directing to the module folder and then “python setup.py install” in the cmd, the installation starts but ends with an error:

    error: Setup script exited with error: Unable to find vcvarsall.bat

Leave a Reply

Your email address will not be published.


*


 

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