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

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:
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.
Junos XML API
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:
1 2 3 4 |
sbehrens@vTetris> show version Hostname: vTetris Model: firefly-perimeter JUNOS Software Release [12.1X47-D20.7] |
PyEZ will execute the following XML-RPC to the device for requesting the information about the software version:
1 2 3 4 5 6 7 8 9 10 |
sbehrens@vTetris> show version | display xml rpc <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos"> <rpc> <get-software-information> </get-software-information> </rpc> <cli> <banner></banner> </cli> </rpc-reply> |
And the device will answer with the following XML output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
sbehrens@vTetris> show version | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos"> <software-information> <host-name>vDoom</host-name> <product-model>firefly-perimeter</product-model> <product-name>firefly-perimeter</product-name> <package-information> <name>junos</name> <comment>JUNOS Software Release [12.1X47-D20.7]</comment> </package-information> </software-information> <cli> <banner></banner> </cli> </rpc-reply> |
And if you want to get the „show version“ information with PyEZ the „<get–software–information>“ 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:
1 |
dev.cli("show version") |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from jnpr.junos import Device from lxml import etree # Set device information with IP-address, login user and passwort dev = Device(host="192.168.4.51", user="sbehrens", password="secretPW") # Connect to the device dev.open() # Get the software version from device software_version = dev.rpc.get_software_information(normalize=True) # Print response of device print (etree.tostring(software_version)) # Close the connection dev.close() |
In my example the output of the Python code above is the following:
1 |
<software-information><host-name>vTetris</host-name><product-model>firefly-perimeter</product-model><product-name>firefly-perimeter</product-name><package-information><name>junos</name><comment>JUNOS Software Release [12.1X47-D20.7]</comment></package-information></software-information> |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from jnpr.junos import Device # Set device information with IP-address, login user and passwort dev = Device(host="192.168.4.51", user="sbehrens", password="secretPW") # Connect to the device dev.open() # Print all the information included in facts print(dev.facts) # Close the connection dev.close() |
And you will see something similar to the output below:
1 |
{'fqdn': 'vTetris', 'domain': None, '2RE': False, 'vc_capable': False, 'personality': 'SRX_BRANCH', 'hostname': 'vTetris', 'HOME': '/cf/var/home/sbehrens', 'srx_cluster': False, 'ifd_style': 'CLASSIC', 'serialnumber': '123456', 'version': '12.1X47-D20.7', 'switch_style': 'NONE', 'model': 'FIREFLY-PERIMETER', 'virtual': True, 'RE0': {'up_time': '10 hours, 21 minutes, 6 seconds', 'model': 'FIREFLY-PERIMETER RE', 'last_reboot_reason': 'Router rebooted after a normal shutdown.', 'status': 'Testing'}, 'version_info': junos.version_info(major=(12, 1), type=X, minor=(47, 'D', 20), build=7)} |
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):
1 2 |
# Set device information with IP-address, login user and passwort dev = Device(host="192.168.4.51", user="sbehrens", password="secretPW", 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:
- jnpr.junos.op.fpc (FpcHwTable, FpcInfoTable)
- jnpr.junos.op.routes (RouteTable)
If I run a similar RouteTable-example like mentioned above:
1 2 3 4 5 6 7 |
tbl = RouteTable(dev).get() print(tbl) print("------------------------------------") for item in tbl: print("ROUTE: ", item) print("proto: ", item.protocol) print("via: ", item.via) |
I directly get the following output of all 10 routes of my vSRX:
1 2 3 4 5 6 7 8 9 10 |
RouteTable:192.168.4.51: 10 items ------------------------------------ ROUTE: RouteTableView:10.0.0.1/32 proto: Direct via: lo0.0 ROUTE: RouteTableView:10.10.10.0/30 proto: Direct via: ge-0/0/3.0 ... shortened! |
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.
Easy ncclient for Python… PyEZ. 🙂
Nice post!
Hey Steve, thanks 🙂
…ncclient…is really too obvious 🙂
very interesting~
Very use full article.
After a long time, I have found understandable article.
Kindly share some other stuff on Juniper automation.
Thank you.