Thursday, January 1, 2015

Controlling Junos with Python & PyEZ, Part 1: Installation

You can manage or automate routers by using python. Having an XML API is the benefit of using a Juniper router as it is easier than other routers to manage it with python. Moreover Juniper wrote a Python library, which is actually a "micro-framework", called PyEZ and it makes easier for non-proggramers to manage a Junos OS router. 


I am taking the exact explanation from the "juniper tech wiki":

Junos PyEZ is designed to provide the same capabilities as a user would have on the Junos OS CLI, but in an environment built for automation tasks. These capabilities include, but are not limited to:

  • Provide "facts" about the device such as software version, serial number, and so on
  • Retrieve the "operational" or "run-state" information (think "show" commands) using Tables/Views
  • Retrieve the configuration using Tables/Views
  • Make unstructured configuration changes with "snippets" and "templates"
  • Make structured configuration changes with modeled abstractions
  • Provide common utilities for tasks such as secure copy of files and software updates

Installation Python, PIP and PyEZ (Windows) 

To use PyEz, you must be using a python version 2.7 or later. I am not getting  into details of installing Python on to Windows as it is as easy as installing an .exe file. You may download the latest build from When installing python select "pip" and "setuptools" to get installed. Also to register the version which you are installing select "" as seen in the capture below:

You may need to reload Windows inorder for python to work. If PIP is not installed and you do not wanted to install python from scratch then you have the option to only install PIP. (PIP is a package itself which install packages.). Download,  save it as a .py file then, run it from the command prompt "python".

Behind the scenes PyEZ uses lxml, ncclient, paramiko, pycrypto, jinja2, PyYAML, netaddr, ecdsa and markupsafe python packages.  For ease of installation, we will use PyPI, which is a Python package repository. But before  you need to check if  Microsoft Visual C++ Compiler for Python 2.7 is installed on your PC and if not install it before executing the command "pip install junos-eznc" command in powershell/cmd.

Configuring Router 

For PyEz to work, ssh and netconf must be enabled on router which means you must be using "domestic" version of Junos. Basic configuration to enable ssh and netconf: 


If everything is right, then you should be able to connect to a router using PyEZ and get some outputs. Here is a basic script which gets version info from a router:

from jnpr.junos import Device

dev = Device(host='',user='admin',password='password')

print dev.facts['version']
print dev.cli("show configuration system services)


Here is the output:

For more you should visit Junipers PyEZ TechWiki page. You may also have a look at Juniper Python TeckWiki page. 

Happy New Year!!


Mason's Children said...

Very cool! I've just started to learn Python, so I'm going to try this out.

Erçin TORUN said...

I'm glad you like the post, hopefully you benefited it.

Unknown said...

for me, it doesn't work until I give port 22 in script as it keep on getting connection timed out error. Netconf and SSH both are enabled.

from jnpr.junos import Device

dev = Device(host='',user='labs',password='labs1labs',port='22')

print dev.facts['version']
print dev.cli("show configuration system services")


Lord Necros said...

hello can someone help me im trying to configure a junos router with this tool but my problem is the way i reach the router in my job i use a centos server to get access to the router and i dont have acces to the server to execute the python is there some way to make a connection to the server and then send the instructions from python.


Post a Comment


Internetworking Hints Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger