Automation
Automation

Scripting How-To: Change route metric in OSPF

by Juniper Employee on ‎02-09-2016 08:29 AM - edited on ‎09-19-2017 09:33 AM by Administrator Administrator
02.09.16   |   08:29 AM

This applies to SLAX version 1.0 and higher.

 

Overview

 

Change the route metric for an OSPF interface.

 

 Description

 

The first simple event script modifies the OSPF metric based on a single time-event, as a simple introduction on how to use an event script type.  The second script loads a portion of the router configuration and verifies that the metric is not set before setting it to the desired value.

 

 

Example Configuration

This event policy configuration is triggered by a single time-event for simplicity of testing, set to run at 10:30.

 

> show configuration event-options 
generate-event {
    myEventTrigger time-of-day "10:30:00 +0000";
}
policy myEventTrigger {
    events myEventTrigger;
    then {
        event-script myEvent.slax;
    }
}
event-script {
    file myEvent.slax;
}

 

SLAX Script Contents

Store this as the myEvent.slax script in the /var/db/scripts/event folder on the device. 

 

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 / {
  <event-script-results> {
    var $configuration-change = <configuration>{
      < protocols> {
        <ospf> {
          <area> {
            <name> "0.0.0.0";
            <interface> {
              <name> "ge-0/0/0";
              <metric> "10";
            }
          }
        }
      }
    }

    var $connection = jcs:open();
    var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
    if( $results//xnm:error ) {
      for-each( $results//xnm:error ) {
        <output> message;
      }
    }
    var $close-results = jcs:close($connection);
  }
}

 

Example Output

 To start, we see that at 10:24, before the script runs:

 

> show system uptime 
Current time: 2016-01-11 10:24:53 UTC

There is no route metric configured for OSPF:

 

> show configuration protocols ospf    
area 0.0.0.0 {
    interface ge-0/0/0.0;
}

 Then at 10:31, after our event script runs:

 

> show system uptime                   
Current time: 2016-01-11 10:31:10 UTC

 

We see the route metric for OSPF was set to 10 by the myEvents.slax script:

 

> show configuration protocols ospf    
area 0.0.0.0 {
    interface ge-0/0/0.0 {
        metric 10;
    } 
}

 

We can check the logs to see how the script ran:

 

> show log escript.log 
.
.
.
Jan 11 10:30:01 event script processing begins
Jan 11 10:30:01 reading event details
Jan 11 10:30:01 testing event details
Jan 11 10:30:01 running event script 'myEvent.slax'
Jan 11 10:30:01 opening event script '/var/db/scripts/event/myEvent.slax'
Jan 11 10:30:01 reading event script 'myEvent.slax'
Jan 11 10:30:56 event script output
Jan 11 10:30:56 begin dump
<?xml version="1.0"?>
<event-script-results xmlns:junos="http://xml.juniper.net/junos/*/junos" xmlns:xnm="http://xml.juniper.net/xnm/1.1/xnm" xmlns:jcs="http://xml.juniper.net/junos/commit-scripts/1.0"/>
Jan 11 10:30:56 end dump
Jan 11 10:30:56 inspecting event output 'myEvent.slax'
Jan 11 10:30:56 finished event script 'myEvent.slax'
Jan 11 10:30:56 event script processing ends

 

Alternate SLAX Script - change the OSPF metric if it is not set

This alternate SLAX script loads a subset of the configuration from the device, queries those results to see if the metric is configured or not, and only applies the configuration change when necessary. 

 

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 $connection = jcs:open();  /* create a connection to reuse */

    /* rpc to get some configuration */
    var $rpc-get-configuration = <get-configuration database="committed" inherit="inherit"> {
      <configuration> {
        <protocols> {
          <ospf> {
            <area> {
              <name> "0.0.0.0";
              <interface> {
                <name> "ge-0/0/0";
              }
            }
          }
        }
      }
    }
    var $get-configuration = jcs:execute($connection, $rpc-get-configuration);

    if ($get-configuration/protocols/ospf/area[name="0.0.0.0"]/interface[name="ge-0/0/0.0"]/metric == 10) {
      /* nothing to do */
    } else {
      var $configuration-change = <configuration>{
        <protocols> {
          <ospf> {
            <area> {
              <name> "0.0.0.0";
              <interface> {
                <name> "ge-0/0/0";
                <metric> "10";
              }
            }
          }
        }
      }

      var $results := { call jcs:load-configuration( $connection, $configuration = $configuration-change ); }
      if( $results//xnm:error ) {
        for-each( $results//xnm:error ) {
          <output> message;
        }
      }
    }
    expr jcs:close($connection);  /* close the connection */
  }
}

 

Note: This alternate script should not use the time-based event configuration that the first script used.