Access RPC On Demand Directly
You should always have the ability to do anything that the Junos OS or XML API provide. Each Device variable has an rpc
property to make accessing Junos OS at this low level easy. Junos PyEZ has metaprogramming to do what you want only when you need it (on demand).
The term "metaprogramming" means that the rpc
will dynamically create Junos OS XML Remote Procedure Calls (RPCs) as you invoke them, rather than pre-coding them as part of the Junos PyEZ library.
In other words, if a Junos OS device provides thousands of RPCs, Junos PyEZ does not contain thousands of RPC functions. It metaprograms only the RPCs that you use, keeping the size of this module small, and the portability flexible.
Usage Example
The following illustrates a basic example using the
Device.rpc
property to retrieve the inventory, and display the model and serial number information.
1 |
from jnpr.junos import Device |
2 |
jdev = Device(user = 'jeremy' , host = 'vsrx_cyan' , password = 'jeremy1' )jdev. open () |
4 |
inv = jdev.rpc.get_chassis_inventory() |
6 |
print "model: %s" % inv.findtext( 'chassis/description' ) print "serial-number: %s" % inv.findtext( 'chassis/serial-number' ) |
RPC Command
It is very easy to determine a Junos OS XML API command. On a Junos CLI you use the | display xml rpc
mechanism, as illustrated:
01 |
jeremy@junos> show chassis hardware | display xml rpc |
04 |
< get-chassis-inventory > |
05 |
</ get-chassis-inventory > |
The content between the
rpc
elements is the XML RPC command, in this case
get-chassis-inventory
. As you can see from the above code example, to invoke this API, use the NETCONF object
rpc
attribute and invoke a method name corresponding to the XML RPC command. If the command has dashes (-), swap them to underscores (_).
1 |
inv = jdev.rpc.get_chassis_inventory() |
If the command has parameters, do the same. Here is an example retrieving the status of a given interface:
01 |
jeremy@junos> show interfaces ge-0/0/0 media | display xml rpc |
04 |
< get-interface-information > |
05 |
< media /> < interface-name >ge-0/0/0</ interface-name > </ get-interface-information > |
The equivalent Python would look like this:
1 |
rsp = jdev.rpc.get_interface_information( media = True , interface_name = 'ge-0/0/0' ) |
Here the
media
parameter does not take a value, so you simply assign it to
True
. Again, for parameter names that contain dashes, you swap them for underscores;
interface-name
becomes
interface_name
.
RPC Response
It is very easy to determine the response of a Junos OS XML API command. On a Junos OS CLI you use the
| display xml
mechanism:
01 |
jeremy@junos> show chassis hardware | display xml |
04 |
< chassis junos:style = "inventory" > |
06 |
< serial-number >AD2909AA0096</ serial-number > |
07 |
< description >SRX210H</ description > |
09 |
< name >Routing Engine</ name > |
10 |
< version >REV 28</ version > |
11 |
< part-number >750-021779</ part-number > |
12 |
< serial-number >AAAH4755</ serial-number > |
13 |
< description >RE-SRX210H</ description > |
17 |
< description >FPC</ description > |
20 |
< description >2x GE, 6x FE, 1x 3G</ description > |
25 |
< version >REV 04</ version > |
26 |
< part-number >750-023367</ part-number > |
27 |
< serial-number >AAAG7981</ serial-number > |
28 |
< description >FPC</ description > |
31 |
< description >1x T1E1 mPIM</ description > |
35 |
< name >Power Supply 0</ name > |
The result of the RPC command is an lxml element, set at the first element after the <rpc-reply>
. In the above example, that would be<chassis-inventory>
. Given that, any XPath expression is relative. So to extract the chassis serial number, the XPath expression is simply chassis/serial-number.
1 |
inv = jdev.rpc.get_chassis_inventory() |
2 |
print "model: %s" % inv.findtext( 'chassis/description' ) print "serial-number: %s" % inv.findtext( 'chassis/serial-number' ) |
Since the response variable is an lxml element, you can use any routine available from that library.
For details, refer to the
lxml documentation.