Fetching Configuration Subtrees Using Netconf
It took me a bit of struggling to figure out how to pull configuration subtrees via NetConf. Part of this was because I was using
I’ll work that out later… the main reason for this post is to document what I found on how to get configuration subtrees. This particular example fetches the subtree for a specific neighbor under [protocols l2circuit].
To structure the rpc request, we use the command “get-config” and specify a source of candidate. Then we add a “filter” tag with an attribute of @type=“subtree” and under that we can specify the configuration subtree as xml tags.
<rpc message-id="1002 Tue Jun 17 23:25:48 -0400 2014">
<get-config>
<source><candidate/></source>
<filter type="subtree">
<configuration>
<protocols>
<l2circuit>
<neighbor>
<name>192.168.104.69</name>
</neighbor>
</l2circuit>
</protocols>
</configuration>
</filter>
</get-config>
</rpc>
]]>]]>
This is the reply I got.
rpc-reply xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:junos="http://xml.juniper.net/junos/12.3R4/junos" message-id="1002 Tue Jun 17 23:25:48 -0400 2014">
<get-config>
<source><candidate/></source>
<filter type="subtree">
<configuration>
<protocols>
<l2circuit>
<neighbor>
<name>192.168.104.69</name>
</neighbor>
</l2circuit>
</protocols>
</configuration>
</filter>
</get-config>
</rpc>
<data>
<configuration xmlns="http://xml.juniper.net/xnm/1.1/xnm" junos:changed-seconds="1402600329" junos:changed-localtime="2014-06-12 19:12:09 UTC">
<protocols>
<l2circuit>
<neighbor>
<name>192.168.104.69</name>
<interface>
<name>ae0.3909</name>
<virtual-circuit-id>55509</virtual-circuit-id>
</interface>
<interface>
<name>ae0.3933</name>
<virtual-circuit-id>55533</virtual-circuit-id>
</interface>
<interface>
<name>ae0.3959</name>
<virtual-circuit-id>55559</virtual-circuit-id>
</interface>
<interface>
<name>ae0.3983</name>
<virtual-circuit-id>55583</virtual-circuit-id>
</interface>
<interface>
<name>ae0.3991</name>
<virtual-circuit-id>55591</virtual-circuit-id>
</interface>
</neighbor>
</l2circuit>
</protocols>
</configuration>
</data>
</rpc-reply>
]]>]]>
My particular application in this case was that I wanted to get the interface associated with a certain virtual-circuit-id and neighbor. This made it easy to collect what I wanted and to also present the xml for logging.
NetConf: XML Namespaces
I’ve been bashing my head tonight against XML namespaces tonight. It looks like I have to qualify my XPATH statements with the namespace at each level. e.g… (where j is a namespace label mapped to a URI)
“j:chassis-inventory/j:chassis/j:serial-number/text()”
This is unwieldy. I started poking around the Juniper/ncclient github repo because I didn’t remember having to qualify the hell out of everything with Python and I found that there is a proc that does an XSLT to strip namespaces from the RPC reply. There is probably some drawback that I haven’t yet considered. But so far it seems like it will declutter my XPATH statements considerably if I do so.
[juniper-helpers] I will be doing XML Parsing in TCL using tDom
Looks like tDom is going to be my pick for parsing XML outputs gathered by NetConf. I’m trying to figure out how to incorporate this into my Juniper Helpers TCL framework which I am actively developing.
I had been learning a lot of python because I was being silly and convinced that it had a better community than TCL. Now I am not so sure that that’s totally true.
And ultimately it doesn’t matter. I’m having fun and writing something interesting… and that’s what counts!
Exploring NetConf with SSH
I spent a bit of time tonight exploring NetConf using openssh and a notepad. Per RFC 6242, you initiate a NetConf session to router r1 (user ‘lab’) as follows:
ssh lab@r1 -p 830 -s netconf
And a handy reminder for those of you using JUNOS, you can get the XML-RPC equivalent of any command by piping the command to “| display xml rpc”
lab@R1> show chassis hardware detail | display xml rpc
xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X46/junos">
<rpc>
<get-chassis-inventory>
<detail/>
</get-chassis-inventory>
</rpc>
<cli>
<banner></banner>
</cli>
</rpc-reply>