Junos Automation (Scripting)
Junos Automation (Scripting)

Show specific information from output

‎05-16-2018 08:35 AM

Thanks to user PK, I was able to get my route op script working. Now I have another question. One of the elements (is that the right term?) prints out multiple lines:


user@router1> op route ip
Prefix            Source Router       AS Path          AS path: 1234 I (Originator)
Cluster list:
Originator ID:
AS path: Recorded

If I only want the 'AS path' line and nothing else, where should I look to figure this out? So far, I have looked into regex(), substring-after() and split(), but I'm not sure which one is correct. Here is the entire script:


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";

 * Shows the source router and AS path of an IP in the network

var $arguments = {
   <argument> {
      <name> "ip";
      <description> "Destination IP address";
   param $ip;
   match / {
   <op-script-results> {
       var $rpc = {
          <get-route-information> {
          <destination> $ip;
       var $out = jcs:invoke($rpc);
       <rt junos:style="detail"> {
             <output> 'Prefix               Source Router     AS Path';
                 for-each ($out/route-table/rt) {
             <output> jcs:printf('%-20s %-17s %-s', rt-destination _ "/" _ rt-prefix-length, rt-entry/protocol-nh/to, rt-entry/as-path);

I am thinking what I need is something like:


       var $path = substring-after("AS path:", ":")

But I am not sure how to apply it to the script or how to relate it to the rt-entry/as-path element. Do I need another for-each function to cycle through the output of the specific rt-entry/as-path element?