RPC calls using meta programming

47 views
Skip to first unread message

Gavin McKee

unread,
Sep 28, 2016, 1:44:43 PM9/28/16
to Junos Python EZ
Hi guys,

Can someone provide some example on meta programming using PyEZ.  Here is what I'd like to achieve

I have a list of RPC calls that I'd like to run through:

commands = ["get_route_information()", "get_mpls_lsp_information()"]

I'd like to be able to do something like:

for x in commands:
  dev.rpc.x # execute the command in the list


I hope that example is pretty straight forward

Thanks

Gav

Stacy W. Smith

unread,
Sep 28, 2016, 5:16:51 PM9/28/16
to Gavin McKee, Junos Python EZ
Hi Gavin,

You can do this:

rpcs = ('get_route_information',
'get_mpls_lsp_information')

for rpc in rpcs:
resp = getattr(dev.rpc, rpc)()


If your RPC calls require arguments, things get more complicated. In that case, you can do something like this:

rpcs_and_args = (('get_software_information',{'brief':True}),
('get_interface_information',{'descriptions':True,'interface-name':'lo0.0'}))

for (rpc,args) in rpcs_and_args:
resp = getattr(dev.rpc, rpc)(**args)


Hope that helps,
--Stacy

Gavin McKee

unread,
Sep 29, 2016, 7:50:16 AM9/29/16
to Junos Python EZ, gavmc...@googlemail.com
Hi Stacy,

Thanks for the suggestion - i had figured I could do something like 

In [59]: command = '''<get-interface-information></get-interface-information>'''


In [61]: dev.open()

Out[61]: Device(xxxxx)


In [62]: res = dev.execute(command)


In [63]: res

Out[63]: <Element interface-information at 0x1121dd950>


I think I will use the approach you suggested.

Thanks

Gav
Reply all
Reply to author
Forward
0 new messages