All about Juniper JET Automation Framework consists lot of APIs which helps network engineer to automate their day to day operation. The framework leverages Junos PyEZ framework which is nothing but library developed for Juniper routers. In my earlier post, I have already explained how the PyEZ can be installed in your laptop and use the library to connect your first router.
In this post, I will be covering more about Junos PyEZ tables and views. Tables and Views enable you to extract operational information and configuration data from devices running Junos OS as well as configure devices running Junos OS. When you add the Table to the Junos PyEZ framework, Junos PyEZ dynamically creates a configuration class for the resource, which you can use to programmatically configure the resource on a device. Tables and Views are defined using YAML, so no complex coding is required to create your own custom Tables and Views. Tables and Views provide a simple and efficient way to extract information from complex operational command output or configuration data and map it to a Python data structure. Operational (op) Tables select items from the RPC reply of an operational command, and configuration Tables select data from specific hierarchies in the selected configuration database. Each Table item represents a record of data and has a unique key. A Table also references a Table View, which is used to map the tag names in the data to the variable property names used within the Python module.
The below OP table retrieves output of get-route-information which corresponds to the show route protocol static extensive | display xml. The table extracts the information from the output and corresponding staticView selects two fields from each get-route-information output. The OP table has item and key, item is the xpath top level hierarchy and key is the unique value under the xpath hierarchy.
staticRoutes:
rpc: get-route-information
args:
protocol: static
extensive: True
item: route-table/rt
key: rt-destination
view: staticView
staticView:
fields:
rt_destination: rt-destination
protocol_name: rt-entry/protocol-name
Now the same code has to be saved in YAML file or we can directly call the same code in python. Below is the code what need to be run on your machine. I am using Junos PyEZ docker on my laptop to test all the functionality.
PS C:\lab> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
juniper/pyez latest 122dff90c4db 6 days ago 279MB
PS C:\lab>
PS C:\lab> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
628e5c01da14 juniper/pyez "sh" 37 hours ago Up 37 hours pyez
PS C:\lab>
Below is the code with YAML model called under the same python file.
/scripts # cat srx_facts.py
from jnpr.junos import Device
import sys
import pprint
from jnpr.junos.factory.factory_loader import FactoryLoader
import yaml
dev = Device('add your ip address of the router',user='user',passwd='passwd')
dev.open()
hhost_name = dev.facts['hostname']
#Defining YAML Model
yml = """
---
staticRoutes:
rpc: get-route-information
args:
protocol: static
extensive: True
item: route-table/rt
key: rt-destination
view: staticView
staticView:
fields:
rt_destination: rt-destination
protocol_name: rt-entry/protocol-name
"""
####YAML Ends Here #####
#### Load Table and View definitions via YAML into namespace ####
globals().update(FactoryLoader().load(yaml.load(yml)))
st = staticRoutes(dev).get()
# Load for interfaces
interface_var = interfaceStatus(dev).get()
#print(interface_var)
#print(st)
# Print all the information received from device
print("-----------------Static Routes------------------")
print("---------------------------------------------")
for item in st:
print('*')
print(hhost_name, "is having", item.rt_destination,"as", item.protocol_name, "route.")
print("*")
print("---------------------------------------------")
dev.close()/scripts #
Below is the output after running the above code. The below output is the lab output and all the public IP are used only for LAB purpose and are not connected, advertise and reachable in the public cloud.
Model Number is: SRX1400
Hostname is: vSRX120
-----------------Static Routes------------------
---------------------------------------------
*
vSRX120 is having 0.0.0.0 as Static route.
*
---------------------------------------------
*
vSRX120 is having 192.1.1.1 as Static route.
*
---------------------------------------------
*
vSRX120 is having 193.2.2.2 as Static route.
*
---------------------------------------------
*
vSRX120 is having 172.11.11.11 as Static route.
*
---------------------------------------------
*
vSRX120 is having 173.22.22.22 as Static route.
*
---------------------------------------------
*
The view property associates the Table definition with a particular View. A View maps your user-defined field names to elements in the selected Table items using XPath expressions. You can customize the View to only select the necessary elements from the Table items.
References: Juniper Documentation
No comments:
Post a Comment