Monday, July 18, 2016

My Third Step towards Automation: How to map Junos Commands to PyEZ RPC


In JUNOS, it’s very easy to find any RPC equivalent of the existing command by running “show route | display xml rpc”. This helps to find what is equivalent RPC command of show route as per below output.

get-route-information

It’s very difficult to find every time on cli what is equivalent of RPC of any Junos command. So we can leverage PyEZ to find the same and in the upcoming posts, you can check how it can be leverage to pass the different values directly in the RPC calls. In PyEZ we can call display_xml_rpc library to get the information of any RPC equivalent Junos Command.

from jnpr.junos import Device
from jnpr.junos.exception import ConnectError
dev = Device(host='192.168.1.1', user='lab', passwd=’lab')
try:
dev.open()
except ConnectError as err:
print "Cannot connect to device: {0}".format(err)
sys.exit(1)
print dev.display_xml_rpc('show route',format='text')
print dev.display_xml_rpc('show interface',format='text')
print dev.display_xml_rpc('show ospf neighbor',format='text')
dev.close()
The output of above file is given below:-
C:\Python27>python.exe testing\connect_device_rpc.py
RPC of show route is:
get-route-information

RPC of show interface is:
get-interface-information

RPC of show ospf neighbor is:
get-ospf-neighbor-information

Click Here To Read Rest Of The Post...

Tuesday, July 12, 2016

My Second Step towards Automation: Install the PyEZ Library and Connect Your Remote Device


Junos PyEZ micro framework has been built and tested tested with Python 2.6 and 2.7. So before moving ahead you must install Python 2.6 or 2.7.

Use:- “pip install junos-eznc” on your ubuntu or linux laptop to install PyEZ library or simply clone it from GIT hub “pip install git+https://github.com/Juniper/py-junos-eznc.git”

. You can install GIT on windows and start playing with it also. This library can be easily installed on your working laptop and you can connect any of your physical or virtual juniper device. Verify Your Installation
In your root directory make sure you have correct folders under python directory as below listed:-
1. C:\Python27\Lib\site-packages\junos_eznc
2. C:\Python27\Lib\site-packages\jnpr\junos

You can work with python in interactive mode or simple by creating a python files and run it from the python shell directly. Interactive shell is real time and generates the output immediately. First PyEZ Program from intractive shell to capture the facts of the device.
1. from jnpr.junos import Device
2. dev = Device(host='192.168.1.1', user='lab', passwd='lab')
Above command is Creating the device instance and provide the hostname, any parameters required for authentication, and any optional parameters.
3. Dev.open()
Above Command is Connecting to the device by calling the open() method.
4. print dev.facts
Above command is Printing the device facts, which are automatically retrieved when the connection and NETCONF session are established.
5. dev.close()
Above command is used to close the NETCONF session

If you are looking for to extract the particular information from the facts. In that case you can change the dev.facts to dev.facts['serialnumber']. By executing this command you will get the serial number of chassis.

Click Here To Read Rest Of The Post...

Sunday, July 10, 2016

My First Step towards Automation: Controlling Junos with Python & PyEZ


You can manage or automate routers by using python. But writing long codes in python is tedious task when it comes for non-programmers. But leveraging PyEZ, Python micro framework can help dev-ops engineers to automate the routers on the fly. Having an XML API is the benefit of using a Juniper router as it is easier than other routers to manage it with python PyEZ library. Moreover Juniper wrote a Python library, which is actually a "micro-framework", called PyEZ and it makes easier for non-programmers like me to manage a Junos OS router. Juniper devices comes with SLAX which can be leverage for on box automation. But PyEZ comes with OFF box solution and for onbox JET framework is required. PyEZ micro frame used NETCONF to communicate with the boxes.

PyEZ library is built for two type of users:-
1. Non-Programmers - Python as a Power Shell: Non-programmers, such as Network Engineers, can use the native Python shell on their management server (laptop, tablet, phone, and so on) as their point-of-control to remotely manage Junos OS devices.
2. Programmers - Open and Extensible: There is a growing interest and need to automate the network infrastructure into larger IT systems. To do so, traditional software programmers, DevOps, hackers, and so on, need an abstraction library of code to further those activities. Junos PyEZ is designed for extensibility so that the programmer can quickly and easily add new widgets to the library in support of their specific project requirements. There is no need to "wait on the vendor" to provide new functionality. Junos PyEZ is not specifically tied to any version of Junos OS or any Junos OS product family.

Junos PyEZ Features
Junos PyEZ is designed to provide the same capabilities as 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

Closing Comments:
Currently PyEZ is used to control only Juniper Devices but as it is open and available on GIT hub, I think the same functionality can be extended to multi-vendor environment also. Still my research is on, how PyEZ can be used for multi-vendors environment so that we can leverage the open community rather depending on any OEM lock-in.

Stay tune for the next post of PyEZ installation.

Click Here To Read Rest Of The Post...