[j-nsp] PyEZ - variable into rpc.get request

serge vautour sergervautour at gmail.com
Wed Sep 5 11:28:15 EDT 2018


Hello,

I've been coding a lot lately in python using PEYZ. I love it! I've found
using tables/views to be by far the easiest way to retrieve config data and
show commands. There's lots of info in the developer guide:

https://www.juniper.net/documentation/en_US/junos-pyez/information-products/pathway-pages/junos-pyez-developer-guide.html

Here's how to accomplish your command:

---------------------------------------------------

import yaml
from jnpr.junos import Device
from jnpr.junos.factory.factory_loader import FactoryLoader

DeviceIP = "1.2.3.4"
Username = "user"
Password = "pass"

dev = Device(host=DeviceIP, user=Username, password=Password,
normalize=True).open()

## YAML for the interface list
RouteYAML = """
---
RouteTable:
  rpc: get-route-information
  item: route-table/rt/rt-entry
  args:
    table: 'inet.0'
    destination: '10.1.2.4'
  args_key:
    table
    destination
  view: RouteView
RouteView :
  fields:
    protocolName: protocol-name
    activeTag: active-tag
    preference: preference
"""
globals().update(FactoryLoader().load(yaml.load(RouteYAML)))
RouteTableList = RouteTable(dev)
RouteTableList.get()

for item in RouteTableList:
   print "protocolName: " + item.protocolName
   print "activeTag: " + item.activeTag
   print "preference:" + item.preference
---------------------------------------------------

This route at the CLI:

show route 10.1.2.4 table inet.0

inet.0: 836 destinations, 855 routes (831 active, 0 holddown, 5 hidden)
+ = Active Route, - = Last Active, * = Both

10.1.2.4/31        *[OSPF/10] 5d 09:57:18, metric 3400
                    > to 10.12.100.209 via ae64.100
                      to 10.12.100.254 via ae62.0

The XML:

show route 10.1.2.4 table inet.0 | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/17.4R2/junos">
    <route-information xmlns="
http://xml.juniper.net/junos/17.4R2/junos-routing">
        <route-table>
            <table-name>inet.0</table-name>
            <destination-count>836</destination-count>
            <total-route-count>855</total-route-count>
            <active-route-count>831</active-route-count>
            <holddown-route-count>0</holddown-route-count>
            <hidden-route-count>5</hidden-route-count>
            <rt junos:style="brief">
                <rt-destination>10.1.2.4/31</rt-destination>
                <rt-entry>
                    <active-tag>*</active-tag>
                    <current-active/>
                    <last-active/>
                    <protocol-name>OSPF</protocol-name>
                    <preference>10</preference>
                    <age junos:seconds="467857">5d 09:57:37</age>
                    <metric>3400</metric>
                    <nh>
                        <selected-next-hop/>
                        <to>10.12.100.209</to>
                        <via>ae64.100</via>
                    </nh>
                    <nh>
                        <to>10.12.100.254</to>
                        <via>ae62.0</via>
                    </nh>
                </rt-entry>
            </rt>
        </route-table>
    </route-information>
    <cli>
        <banner>{master}</banner>
    </cli>
</rpc-reply>

In the PEYZ YAML definition I pulled back a few fields for fun:

[root at blah]# python test2.py
protocolName: OSPF
activeTag: *
preference:10

I hope this helps.
Serge

On Mon, Sep 3, 2018 at 9:40 PM Stacy W. Smith <stacy at acm.org> wrote:

> Remove the quotes around r2check.
>
> i.e. the line should be:
> r2response = dev.rpc.get_route_information(table='inet.0',
> destination=r2check)
>
> --Stacy
>
> > On Sep 3, 2018, at 6:33 PM, Jason Taranto <Jason.Taranto at vocus.com.au>
> wrote:
> >
> > Hi All,
> >
> > After a while of my head colliding with the wall beside me, would anyone
> know how to get a variable into an rpc command via pyez.
> >
> > My latest attempt is below.
> >
> >  r2check = raw_input("Route to check e.g XXX.XXX.XXX.XXX : ")
> >  print ("Checking if route is in the table......")
> >  time.sleep(2)
> >  r2response = dev.rpc.get_route_information(table='inet.0',
> destination='r2check')
> >  time.sleep(2)
> >
> print("################################################################################")
> >  print("
>                 ")
> >  print("                      Response from router below...
>                ")
> >  print (etree.tostring(r2response))
> >
> >
> > The error message I get is:
> > jnpr.junos.exception.RpcError: RpcError(severity: error, bad_element:
> r2check, message: could not resolve name: r2check)
> >
> >
> >
> > Thanks in advance,
> > Jason
> >
> >
> >
> > _______________________________________________
> > juniper-nsp mailing list juniper-nsp at puck.nether.net
> > https://puck.nether.net/mailman/listinfo/juniper-nsp
>
> _______________________________________________
> juniper-nsp mailing list juniper-nsp at puck.nether.net
> https://puck.nether.net/mailman/listinfo/juniper-nsp
>


More information about the juniper-nsp mailing list