Junos Automation (Scripting)
Reply
Visitor
sam.moore@imtechict.co.uk
Posts: 3
Registered: ‎03-22-2011
0

File input woes

[ Edited ]

I’m having problems looping through get-interface-information output loaded from a file.  I get the expected output when looping through the output from RPC call directly.

 

version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
ns exsl extension = "http://exslt.org/common";

import "../import/junos.xsl";

match / {

     <op-script-results> {
     
     var $file = document("/var/tmp/interface_stats.xml");

     for-each ( $file/physical-interface ) {
     if (oper-status == "up") {
     <output> "Interface " _ name;
         }
      }
   }
}

 

 

 

The file appears almost-identical to what you get from the RPC call:

 

user> file show /var/tmp/interface_stats.xml

<?xml version="1.0"?>

<interface-information xmlns="http://xml.juniper.net/junos/10.4R9/junos-interface" xmlns:junos="http://xml.juniper.net/junos/*/junos" junos:style="normal">

<physical-interface>

<name>ge-0/0/0</name>

<admin-status junos:format="Enabled">up</admin-status>

<oper-status>down</oper-status>

<local-index>138</local-index>

<snmp-index>506</snmp-index>

<generation>141</generation>

 

[output truncated]

 

user> show interfaces detail | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/11.2R1/junos">
    <interface-information xmlns="http://xml.juniper.net/junos/11.2R1/junos-interface" junos:style="normal">
        <physical-interface>
            <name>ge-0/0/0</name>
            <admin-status junos:format="Enabled">up</admin-status>
            <oper-status>down</oper-status>
            <local-index>138>/local-index>
            <snmp-index>506</snmp-index>
            <generation>141</generation>

[output truncated]

 

I've messed about with the location path operators to no avail.  I’m obviously missing something but I'm struggling to see what!

 

Cheers,

 

Sam

 

Just to add I do have interfaces that are up so it's not the if logic. :smileyhappy:  Also, the script runs there's just no output.

Recognized Expert
ccall
Posts: 237
Registered: ‎06-18-2008
0

Re: File input woes

The node-set returned by the document() function will be pointing at the root node of the XML document rather than the <interface-information> element like the RPC results would be, so you need to include the full path in your location path:

 

$file/interface-information/physical-interface

 

Visitor
sam.moore@imtechict.co.uk
Posts: 3
Registered: ‎03-22-2011
0

Re: File input woes

Hey ccall, thanks for the reply.

 

Unfortunately it's no better.

 

I've simplied the code:

 

version 1.0;
ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";
ns exsl extension = "http://exslt.org/common";

import "../import/junos.xsl";

match / {

    <op-script-results> {
    var $file = document("/var/tmp/interface_stats.xml");

    for-each ( $file/interface-information/physical-interface ) {
            <output> .;
           }
        }
    }

 

But still no output :smileyfrustrated:

 

No issues present in the trace file (attached).

 

Cheers,

 

Sam

Visitor
sam.moore@imtechict.co.uk
Posts: 3
Registered: ‎03-22-2011
0

Re: File input woes

After reading the automation ref, document() doesn’t appear to work as described.

 

I’ve manage to do what I need to do though using local- name().  Here’s the code for anyone else that might be struggling with this:

 

version 1.0;

ns junos = "http://xml.juniper.net/junos/*/junos";
ns xnm = "http://xml.juniper.net/xnm/1.1/xnm";
ns jcs = "http://xml.juniper.net/junos/commit-scripts/1.0";

import "../import/junos.xsl";

match / {

        <op-script-results> {

        var $stats = document("/var/tmp/interface_stats.xml");
        var $ifd = $stats//*[local-name() == "physical-interface"];

        for-each ($ifd){
        var $oper = ./*[local-name() == "oper-status"];
         if ($oper == "up") {
          var $ifd_name = ./*[local-name() == "name"];
          <output> $ifd_name;
                }
             }
         }
     }

 

Cheers,

 

Sam

 

Copyright© 1999-2013 Juniper Networks, Inc. All rights reserved.