• Diniz Martins

Junos-PythonEZ (PyEZ) | Connection

Junos PyEZ is a microframework for Python that enables you to manage and automate devices running the Junos operating system (Junos OS). Junos PyEZ is designed to provide the capabilities that a user would have on the Junos OS command-line interface (CLI) in an environment built for automation tasks. Junos PyEZ does not require extensive knowledge of Junos OS or the Junos XML APIs.


Junos PyEZ enables you to directly connect to a device using a serial console connection, telnet, or a NETCONF session over SSH. In addition, Junos PyEZ also supports connecting to the device through a telnet or SSH connection to a console server that is connected to the device’s console port. You can use Junos PyEZ to initially configure a new or zeroized device that is not yet configured for remote access by using either a serial console connection when you are directly connected to the device or by using telnet or SSH through a console server that is directly connected to the device.


Here you have the codes to kick off:


First thing first - establishing an SSH Connection for a NETCONF Session into Juniper router:

stenge@juniper# set system services netconf ssh

To install the current release of Junos PyEZ from PyPI, execute the following command into your Linux server:

stenge@server:~$ sudo pip install junos-eznc

Connecting to Device Running Junos OS Using Junos PyEZ:

stenge@server:~$ python2.7
>>> from pprint import pprint
>>> from jnpr.junos import Device
>>> dev = Device (host='192.168.0.1' ,user='usr' ,password='pwd')

Check the connection:

>>> dev.open()
Device(192.168.0.1)

In this case, we are connected on the router by NETCONF over SSH with default port (830).



You can check others outputs:

>>> dev.facts ['version']
'19.1R3.5'

>>> print dev.cli ("show interface terse")
Interface               Admin Link Proto    Local                 
ge-0/0/0                up    up
ge-0/0/0.40             up    up   inet     10.20.30.1/17  
(...)                                 

>>> pprint (dev.facts)
{'2RE': True,
 'HOME': '/var/home/stenge',
  'RE0': {'last_reboot_reason': 'Router rebooted after a normal shutdown.',
           'mastership_state': 'master',
                      'model': 'RE-S-1800x4',
                     'status': 'OK',
                    'up_time': '81 days, 1 hour, 37 minutes, 22 seconds'},
                        'RE1': {'last_reboot_reason': 'Router rebooted after a normal shutdown.',
            'mastership_state': 'backup',
                       'model': 'RE-S-1800x4',
                      'status': 'OK',
                     'up_time': '81 days, 1 hour, 37 minutes, 21 seconds'},
                    'RE_hw_mi': False,
                    (...)

To close session:

>>> dev.close()

Bonus Time:

The following example connect to a host and execute an operational mode command from the shell and save the output to a file.


On your server:

>>> from jnpr.junos import Device
>>> from jnpr.junos.utils.start_shell import StartShell
>>> dev = Device (host='192.168.0.1' ,user='usr' ,password='pwd')
>>> ss = StartShell(dev)
>>> ss.open()
>>> ss.run('cli -c "show configuration | display set | no-more | save /var/tmp/backup.txt"')
>>> ss.close()

On your router:

stenge@MX480> start shell
% cd /var/tmp/
% ls -la | grep backup
-rw-r--r--   1 stenge  wheel     1120693 May 30 23:10 backup.txt


24 views

Recent Posts

See All

Managing Junos using Rest API

The Managing Junos Devices using REST API learning byte demonstrates how to manage Junos devices using REST API. Since most programming languages can communicate with REST API , programmers can easily

JunOS | Tips & Tricks

Sharing some tips and tricks below: Exit from the CLI environment and create a UNIX-level shell. To return to the CLI, type exit from the shell. Find big size file into shell: Deleting whole configura