Junos Automation (Scripting)
Junos Automation (Scripting)

How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 07:18 AM

I work with the following slax op script:

---snip---

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 $ping-target = "10.1.1.1";
    call pinger($ping-target);
        }  
    
}

template pinger($ping-target) {

   /* ping the target   */  
    var $ping-rpc = {
        <ping> {
               <host> $ping-target;
               <count> "1";
               }
    }

        var $ping := jcs:invoke($ping-rpc);
    <output> $ping;

}

---snap---

 

When running it as "op ping.slax", I get the following output:

user@juniper> op ping2                 
10.1.1.1
10.1.1.1
56

1
0
100

no response

{master:0}
user@juniper>

 

Even when running it as "op ping2 | display xml", I'm just getting

user@juniper> op ping2 | display xml   
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.3R6/junos">
    <output>
        10.1.1.1
        10.1.1.1
        56
       
        1
        0
        100
       
        no response
    </output>
    <cli>
        <banner>{master:0}</banner>
    </cli>
</rpc-reply>

{master:0}
user@juniper>

 

How can I get the output in XML format?

 

Thanks,

Stefan

 

6 REPLIES 6
Junos Automation (Scripting)

Re: How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 08:10 AM

If you want the raw XML, use

  copy-of $ping

instead of

  <output> $ping

 

 

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST
Junos Automation (Scripting)

Re: How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 08:26 AM

For some reason copy-of does not seem to work for me.

---snip---

% vi ping1.slax
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 $ping-target = "10.1.1.1";
    <output> "Vorher";
    call pinger($ping-target);
    <output> "Nachher";
        }

}

template pinger($ping-target) {
   /* ping the target   */
    var $ping-rpc = {
        <ping> {
               <host> $ping-target;
               <count> "1";
               }
    }

/*    var $ping := jcs:invoke($ping-rpc);   */
      copy-of jcs:invoke($ping-rpc);
/*    <output> $ping;   */
/*    expr jcsSmiley Surprisedutput( $ping );   */
/*      copy-of $ping;   */

}

---snap---

 

I tried both "copy-of $ping;" as well as "copy-of jcs:invoke($ping-rpc);".

 

What am I missing?

 

Thx,

Stefan

Junos Automation (Scripting)

Re: How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 08:32 AM

Add-on: Actually I get no output at all when using copy-of...

Junos Automation (Scripting)
Solution
Accepted by topic author vogts
‎09-16-2015 08:54 AM

Re: How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 08:49 AM

Works for me:

Here's my edited version:

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 $ping-target = "192.168.131.2";
    <output> "Vorher";
    call pinger($ping-target);
    <output> "Nachher";
  }
}
template pinger($ping-target) {
   /* ping the target   */
    var $ping-rpc = {
        <ping> {
               <host> $ping-target;
               <count> "1";
               }
    }
    var $ping := jcs:invoke($ping-rpc);
    copy-of jcs:invoke($ping-rpc);
}

I had to edit to test on my vsrx.  Here's what I got when I ran::

regress@vsrx-3> op url /var/db/scripts/op/pinger.slax | display xml
<rpc-reply xmlns:junos="http://xml.juniper.net/junos/12.1X47/junos">
    <output>
        Vorher
    </output>
    <ping-results xmlns="http://xml.juniper.net/junos/12.1X47/junos-probe-tests">
        <target-host>
            192.168.131.2
        </target-host>
        <target-ip>
            192.168.131.2
        </target-ip>
        <packet-size>
            56
        </packet-size>
        <probe-result date-determined="1442418373">
            <probe-index>
                1
            </probe-index>
            <probe-success/>
            <sequence-number>
                0
            </sequence-number>
            <ip-address>
                192.168.131.2
            </ip-address>
            <time-to-live>
                128
            </time-to-live>
            <response-size>
                64
            </response-size>
            <rtt>
                9905
            </rtt>
        </probe-result>
        <probe-results-summary>
            <probes-sent>
                1
            </probes-sent>
            <responses-received>
                1
            </responses-received>
            <packet-loss>
                0
            </packet-loss>
            <rtt-minimum>
                9905
            </rtt-minimum>
            <rtt-maximum>
                9905
            </rtt-maximum>
            <rtt-average>
                9905
            </rtt-average>
            <rtt-stddev>
                0
            </rtt-stddev>
        </probe-results-summary>
        <ping-success/>
    </ping-results>
    <output>
        Nachher
    </output>
    <cli>
        <banner></banner>
    </cli>
</rpc-reply>

regress@vsrx-3>

As a practical note: consider specifying a time-out on the ping, else your script will appear to hang...

 

/doug

 

--
"There he goes. One of God's own prototypes. A high-powered mutant of some kind never even considered for mass production. Too weird to live, and too rare to die." --HST
Junos Automation (Scripting)

Re: How to write SLAX result tree to STDOUT in XML format?

‎09-16-2015 08:54 AM

Hi doug,

 

stupid me... I was missing the "| xml". Thanks for your additional hint as well.

 

Thx,

stefan

Junos Automation (Scripting)

Re: How to write SLAX result tree to STDOUT in XML format?

‎11-03-2017 08:27 AM

Can this be done via python also?