Discussion:
[j-nsp] PyEZ - variable into rpc.get request
Jason Taranto
2018-09-04 00:33:20 UTC
Permalink
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-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Stacy W. Smith
2018-09-04 00:39:52 UTC
Permalink
Remove the quotes around r2check.

i.e. the line should be:
r2response = dev.rpc.get_route_information(table='inet.0', destination=r2check)

--Stacy
Post by Jason Taranto
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))
jnpr.junos.exception.RpcError: RpcError(severity: error, bad_element: r2check, message: could not resolve name: r2check)
Thanks in advance,
Jason
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
serge vautour
2018-09-05 15:28:15 UTC
Permalink
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:

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

I hope this helps.
Serge
Remove the quotes around r2check.
r2response = dev.rpc.get_route_information(table='inet.0',
destination=r2check)
--Stacy
Post by Jason Taranto
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.
Post by Jason Taranto
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')
Post by Jason Taranto
time.sleep(2)
print("################################################################################")
Post by Jason Taranto
print("
")
Post by Jason Taranto
print(" Response from router below...
")
Post by Jason Taranto
print (etree.tostring(r2response))
r2check, message: could not resolve name: r2check)
Post by Jason Taranto
Thanks in advance,
Jason
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
_______________________________________________
https://puck.nether.net/mailman/listinfo/juniper-nsp
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp

James Bensley
2018-09-04 08:50:33 UTC
Permalink
Post by Jason Taranto
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))
jnpr.junos.exception.RpcError: RpcError(severity: error, bad_element: r2check, message: could not resolve name: r2check)
Hi Jason,

Someone with more knowledge than I has already provided some info to
you, I just wanted to point out you could also join this Slack
channel: https://networktocode.slack.com

There are rooms for Python, Juniper, Cisco, Ansible, Salt and many
others all relating to network coding and automation. It's a good
place to get help with your network coding issues if you didn't know
about it already.

Cheers,
James.
_______________________________________________
juniper-nsp mailing list juniper-***@puck.nether.net
https://puck.nether.net/mailman/listinfo/juniper-nsp
Loading...