
Junos automation simplifies complex configurations and reduces potential configuration errors. It saves time by automating operational and configuration tasks. It can also speed up troubleshooting and maximises network uptime.
You don't have to be an experienced programmer to use Junos PyEZ. Non-programmers can quickly execute simple commands in Python interactive mode, and more experienced programmers can opt to create more complex, robust, and reusable programs to perform tasks.
For a comprehensive overview of PyEZ refer to the excellent Juniper website listed below.
https://www.juniper.net/documentation/en_US/junos-pyez/topics/concept/junos-pyez-overview.html
PyEZ use cases
- Software and hardware audits
- Automated data collection of device and protocol state
- Basic automated configuration deployment
In order for Python and PyEZ to connect to a device running Junos, the device must be configured to accept connections. This normally involves configuring netconf over ssh to connect, but as of PyEZ 2.0 telnet and serial console connections can also be used.
When creating Python and PyEZ scripts use the following process:-
- Import libraries
- Create device connection string
- Open a connection to a device
- Perform task by writing a script
- Close the connection
In this example I will work with Python and PyEZ to create a script that modifies the Junos configuration. I will use the relevant methods from the PyEZ library to manage Junos configurations on the targeted device. The configuration snippet will add a new interface and routing-instance.
My environment consists of VMware Workstation running Ubuntu open source software. Juniper wrote a Python library, which is actually a "micro-framework", called PyEZ and it makes it easier for non-programmers to manage a Junos OS router.
If you are considering building your own environment the Juniper Day One: Junos PyEZ Cookbook is an excellent resource to help you get up and running. https://www.juniper.net/uk/en/training/jnbooks/day-one/automation-series/junos-pyez-cookbook/
Let's get started!
I’m using Geany to create my PyEZ script. Geany is a text editor using the GTK+ toolkit with basic features of an integrated development environment. The following script has been created to automate my configuration snippet.
Geany PyEZ script
Let’s take a closer look at what the script is doing.
from jnpr.junos.device import Device
I’m creating the PyEZ script by importing the Device class from the PyEZ package.
from jnpr.junos.utils.config import Config
Create the Config object which makes updates to the shared configuration database.
from pprint import pprint
Add the PrettyPrint library so that the Python script data will look better when printed.
junos_hosts = [ ‘192.168.139.150’ ]
for ip in junos_hosts:
Create a list called junos_hosts to contain a single element: the IP address of the targeted device. Also, start a for loop to iterate over each IP address in the list.
dev = Device(host=ip, user=’lab’, password=’lab123’)
dev.open()
Instantiate a Device object and assign it to a variable called dev. Open the NETCONF over SSH session by calling the open() method.
config = Config(dev)
Assign the Config object to a variable named config.
config.lock()
config.load(path=”PE_Config.conf”, merge=True)
config.pdiff()
config.commit()
config.unlock()
Add the load() method to deploy the configuration file. My PyEZ script and configuration file are located in the same directory, so that a full path to the configuration file is not required. Also, use lock() method to lock the configuration database to prevent anyone else from making changes.
The config.pdiff line of code will print the difference between the current active configuration and the candidate configuration.
The merge argument allows me to perform the load merge CLI command.
The config.commit() commits the configuration
dev.close()
Close the connection.
Next I need to create the PE_Config.conf file which is referenced in the script. As a network engineer this is the easy bit because it's what I understand best - Junos. You can create many different configuration snippets to automate many different items of the configuration.
Router configuration file
We’re all set. Let’s login to the targeted device and check the status of the interface and routing-instance. You can see from the below configuration that the configuration does not exist.
Router pre-checks
Now the fun part. Issue the python command and reference the script. The configuration is applied to the targeted device and at the touch of a button multiple lines of configuration are added to the router.

Python command
Finally let’s re-check the router configuration to see what has happened. You can see from the below output that the configuration push has been successful.
Router post automation checks
The automation process is complete and the router is configured. The configuration snippet and script can be modified and deployed on many different routers.
In my next blog I will be looking at automation techniques using Junos PyEZ, Jinja2 and YAML.
@PaulClarkeJNCIP
Fujitsu Customer Solutions Architect
Juniper Networks Ambassador