Getting started with Junos PyEZ

I am fascinated about Python for networking because it is easy to get your automation stuff done and it’s fun working with it because you can quickly expand your projects to any requirements. Like config generation or data processing. But if you then also can communicate with devices through Junos PyEZ it is incredible because this gives you the possibility to do even more.

At the beginning of this post, I would like to explain what Junos PyEZ is and where you can get all the information. Later on, I will show you some examples how to use PyEZ with its XML-RPC API and what the EASY-part of PyEZ is.

What is Junos PyEZ?

Junos PyEZ is known as the „Python library for Junos automation.“ If you have a look at the official Git repository or the Python package, you will see that it shows up as „py-junos-eznc.“ I do not have found any official information about the naming, but I would guess that it means „python – junos – easy network control.“ (hm too obvious? 😀) But, that does not matter because most of the people will call it only PyEZ.

Junos Automation stack

When I was at the Juniper Tech Summit in Berlin earlier this year, a few sessions have been about automation, and I have seen useful presentation slides showing the Junos Automation portfolio and stack. I think it is important to understand the different access levels and possibilities of automation with Junos and how to classify PyEZ:

Junos Automation Stack including PyEZ
Just took the image from the DENOG7 presentation from Peter Sievers.
As you can see in the picture above PyEZ is placed in the second row as a framework, next to RubyEZ. So you have to code some lines in Python to use the PyEZ framework for doing stuff. But you could also use e.g. Ansible or SaltStack if you do not want to code Python by your own. In my opinion, it comes with more flexibility, but the higher coding effort cause you have to start from scratch. In the end, it depends on your projects requirements of course. If you want to go with Ansible, you have to check out Damien Garros outstanding EVPN Sample project. But let us stay focused with PyEZ for now.

Where to find information?

Some web links where you can find valuable information are the following:

  • Junos PyEZ Developer Guide
    • Juniper’s official TechLibrary with some examples, which are very helpful to get started with PyEZ.
  • Junos PyEZ’s documentation
    • My favorite information source while coding. Here you can find all the information about the API like what parameters you can use or which methods and properties exists.
  • PyEZ at Github
    • I like GitHub, and this is the official repository of the PyEZ project. If you want to stay up-to-date you should have an eye on it – and leave a star of course 😉
  • J-NET community forum
    • You should check the automation forum of the J-NET community. There are some good How-To tutorials and further information about PyEZ. The structure of the forum is a bit confusing, but the content is valuable.

If you are facing a problem you should have a look at StackOverflow, Google Groups or GitHub Issues:

If you are searching for examples, you should have a look at Nitin Kumar’s repository PyEZ examples at GitHub.

Is your development environment ready?

Before you can start with coding, you should check if you have setup all the required tools. I use PyCharm as IDE for my Python projects cause it has a lot of helpful features and you have the possibility to debug. Unfortunately, PyCharm has a problem with the import of the tables/views of PyEZ, which I could not fix yet. I also use Atom text editor for quick changes when I do not need a full IDE.

I won’t show how to install python or the requirements on your computer because this is out of the scope of my blog post. A little side note: Please use virtualenv from the beginning cause it will help you to separate all your python projects cleanly.

How does the communication with Junos devices work?

With Junos PyEZ all the communication with the Juniper devices works via NETCONF protocol. Since PyEZ Release 2.0.0 you can also connect to your device via Telnet/Serial. So as we are using NETCONF, you first have to enable „set system services netconf ssh“ within your device configuration. For the beginning we will use our Laptop to code a Python script for connection to a Junos device and getting some information:

PyEZ: Connection through NETCONF

In my example, the Junos device will be a vSRX which I have also running on my computer. If you are working with new devices with factory settings, there is no configuration and no NETCONF set. You could then use the py-junos-netconify project first for getting a basic configuration onto the device. I have not tried the new feature of connecting via Telnet yet.


But let us get back to the communication between Junos device and your python program using PyEZ. If you send the Junos device an information request through PyEZ, the Junos device will execute an XML-RPC.  So PyEZ takes advantage of the Junos XML API – I have just found out that Juniper has an online XML API Browser. For example, let’s take a simple „show version“ command:

PyEZ will execute the following XML-RPC to the device for requesting the information about the software version:

And the device will answer with the following XML output:

And if you want to get the „show version“ information with PyEZ the „<getsoftwareinformation>“ matches to the method „get_software_information().“ At this point I have to mention that you can also send CLI commands directly via the CLI-method as you can see below but  you should use this only for debugging:

If you do so you will get a „Warning: CLI command is for debug use only!“ on your console – „Please do not make this your standard practice!“ 😉

Connect to Junos device and request information

Below you will find some basic lines connecting to a Junos device and requesting the software-version data:

In my example the output of the Python code above is the following:

As you can see, we have successfully invoked an XML-RPC to get the software information of the device.

But it is not that difficult; it’s EASY

In addition to all the XML-RPC stuff PyEZ also comes with simplicity. So you do not even have to call every XML-RPC to get your information cause PyEZ already does. Let’s take the „gather_facts“ feature of PyEZ: If you connect to a Junos device through PyEZ it will ask the device for some basic information like software version 😉 , serial number, model and so on. PyEZ puts all this information into a Python dict called facts:

And you will see something similar to the output below:

So you can easily get the software version without having to execute any additional command, by grabbing the information out of the Python dict „facts.“ If you do not want that PyEZ catches all these facts you can also disable it via the device object constructor (gather_facts=False):

Here comes the power of SIMPLICITY

There are a few examples showing what you can do with PyEZ without invoking any XML-RPC by your own, without complicated coding stuff and no need for parsing XML. In my opinion, this is the greatest strength of PyEZ: It gives you a simple abstraction out of the box to quickly reach your goal. Please find below some further examples:

If I run a similar RouteTable-example like mentioned above:

I directly get the following output of all 10 routes of my vSRX:

You can check on your own what tables and views already exist within the official PyEZ repository. In addition to this there are some useful tutorials in the J-NET community showing what you can do with tables/views:

There are also some further examples of the PyEZ utils like SCP for copying a file from the Junos device to another server. Well, that’s it for now. My little introduction to the world of PyEZ. Thank you for reading.

4 comments on “Getting started with Junos PyEZ”

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Diese Website verwendet Akismet, um Spam zu reduzieren. Erfahre mehr darüber, wie deine Kommentardaten verarbeitet werden.

© 2017 made with ♡ by Stephan Linnerz | impress