as in PyEZ. PyEZ is a micro-framework to remotely manage and automate Juniper devices. It works with Python and allows you to pull Junos specific features into an abstraction layer. This is great because you don't have to do any screen scraping to pull out any fields. I installed this module on my Mac to test this out.
The documentation is located here is great because you can look at the apis on how to create your script. The first script I wanted to test out is how to pull information from a router.
PyEZ can use YAML which is a human readable format markup language. I created a yaml file to extract the fields I was looking for.
Here's my yaml file.
vrf.yml
VRF:
get: routing-instances/instance
args_key: name
view: VRFView
VRFView:
fields:
instance_name: name
instance_type: instance-type
rd_type: route-distinguisher/rd-type
vrf_target: vrf-target/community
interface: interface/name
My script will parse VRFs on a router. I created two routing instances for this demo.
jnpr@R1# show routing-instances
VRF1 {
instance-type vrf;
interface lo0.1;
route-distinguisher 1.1.1.1:100;
vrf-target target:100:100;
}
VRF2 {
instance-type vrf;
interface lo0.2;
route-distinguisher 1.1.1.1:101;
vrf-target target:100:101;
}
Now I can test this in python.
$ python
Python 2.7.5 (default, Mar 9 2014, 22:15:05)
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
First I import all the necessary libraries.
>>> from pprint import pprint
>>> from jnpr.junos import Device
>>> from jnpr.junos.op.routes import RouteTable
>>> from lxml import etree
>>> from jnpr.junos.factory import loadyaml
>>> globals().update( loadyaml('vrf.yml') )
Then I open a connection to a junos device
>>> dev = Device('hostname_or_ip', user='username', password='password')
>>> dev.open()
Device(x.x.x.x)
Next I create a table
>>> tbl = VRF(dev)
Then get the fields for the table
>>> tbl.get(values=True) #make sure to pass values=True
VRF:x.x.x.x: 2 items
Now I can iterate through the table an print the contents.
>>> for item in tbl:
... print 'instance_name:', item.instance_name
... print 'instance_type:', item.instance_type
... print 'rd_type:', item.rd_type
... print 'vrf_target:', item.vrf_target
... print 'interface:', item.interface
...
instance_name: VRF1
instance_type: vrf
rd_type: 1.1.1.1:101
vrf_target: target:100:101
interface: lo0.1
instance_name: VRF2
instance_type: vrf
rd_type: 1.1.1.1:102
vrf_target: target:100:102
interface: lo0.2
Now I can then manipulate the tables and look at individual fields.
>>> find = tbl['VRF1']
>>> find.interface
'lo0.1'
Now imagine a router with a hundred VRFs. I can now parse through this router remotely and automate operations.
No comments:
Post a Comment