Warning: Undefined array key "rcommentid" in /home/clabr/public_html/movingpackets/wp-content/plugins/wp-recaptcha/recaptcha.php on line 348

Warning: Undefined array key "rchash" in /home/clabr/public_html/movingpackets/wp-content/plugins/wp-recaptcha/recaptcha.php on line 349
Junos PyEZ in Interactive Mode - MovingPackets.net

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”:

import sys
from jnpr.junos import Device
from getpass import getpass
from pprint import pprint as pp

user = "script"

print "\nPlease provide password for user '" + user + "' on device '" + sys.argv[1] + "'."
passwd = getpass()

dev = Device(sys.argv[1],user=user,password=passwd)
  print "Connected to " + sys.argv[1] + " as dev"
  print "Connection failed. :-("
  print "\n** Type exit() to quit **"

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:


python -i connect.py $1

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:

john@ubuntu:~$ ./pyez srx1

Please provide password for user 'script' on device 'srx1'.
Connected to srx1 as dev

And now failing due to a bad password:

john@ubuntu:~$ ./pyez srx1

Please provide password for user 'script' on device 'srx1'.
Connection failed. :-(

** Type exit() to quit **

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:

>>> dev.facts
{'hostname': None, 'ifd_style': 'CLASSIC', 'version_info': junos.version_info(major=(10, 3), type=R, minor=2, build=11), '2RE': False, 'serialnumber': 'AB9110AE0730', 'fqdn': None, 'switch_style': 'VLAN', 'version': '10.3R2.11', 'model': 'DELL J-SRX100H', 'RE0': {'status': 'OK', 'last_reboot_reason': '0x1:power cycle/failure ', 'model': 'RE-SRX100H', 'up_time': '19 days, 19 hours, 42 minutes, 41 seconds'}, 'personality': 'SRX_BRANCH'}

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.

>>> pp (dev.facts)
{'2RE': False,
 'RE0': {'last_reboot_reason': '0x1:power cycle/failure ',
         'model': 'RE-SRX100H',
         'status': 'OK',
         'up_time': '19 days, 19 hours, 42 minutes, 41 seconds'},
         'fqdn': None,
 'hostname': None,
 'ifd_style': 'CLASSIC',
 'model': 'DELL J-SRX100H',
 'personality': 'SRX_BRANCH',
 'serialnumber': 'AB9110AE0730',
 'switch_style': 'VLAN',
 'version': '10.3R2.11',
 'version_info': junos.version_info(major=(10, 3), type=R, minor=2, build=11)}

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

>>> dev.facts['model']

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

Connected to srx1 as dev
>>> from jnpr.junos.op.ethport import EthPortTable
>>> eths = EthPortTable(dev)
>>> eths.get()
EthPortTable:srx1: 12 items

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:

>>> list(eths)
[EthPortView:fe-0/0/0, EthPortView:fe-0/0/1, EthPortView:fe-0/0/2, EthPortView:fe-0/0/3, EthPortView:fe-0/0/4, EthPortView:fe-0/0/5, EthPortView:fe-0/0/6, EthPortView:fe-0/0/7, EthPortView:ae0, EthPortView:ae1, EthPortView:ae2, EthPortView:ae3]


>>> for key in eths: print key.name

Or maybe:

>>> pp ( eths.keys() )

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.


Warning: Undefined array key "rerror" in /home/clabr/public_html/movingpackets/wp-content/plugins/wp-recaptcha/recaptcha.php on line 291

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