Junos
Highlighted
Junos

Op Script Overview

‎11-09-2007 01:48 PM

Op Scripts give you the ability to automate network troubleshooting and network management.

JUNOS is a XML parser. It can receive XML as input and send XML as output. What this means is that Op Scripts can also allow you to customize your own cli commands or output.

To get started you need to first find out what command you want to execute.

 Here we will create a variable called "result". The variable will execute the "get-arp-table-information" command which is mapped to the "show arp" command on JUNOS

<xsl:variable name="result" select="jcs:invoke('get-arp-table-information')"/>&nbsp;

We are also able to pass arguments to the script. We define the argument and the parameter

Code:
<argument>

 <name>ip</name>

 <description>IP address of neighbor</description>

 </argument>
 <xsl:param name="ip"/>

Then we take the information which is stored in  "result" and parse the data.

We use a for-loop to check each arp-table-entry and parse the ip address. The xml output will be parsed and sent to the CLI.

Code:
<xsl:for-each select="$result/arp-table-entry">

 <xsl:choose>

 <xsl:when test="ip-address = $ip">
 <xsl:value-of select="mac-address"/>
<xsl:text> </xsl:text>
 <xsl:value-of select="ip-address"/>
 <xsl:text> </xsl:text>
 <xsl:value-of select="interface-name"/>
 <xsl:text> </xsl:text>
 </xsl:when>

 See illustration below. This could replace the "show arp | match xxx"  

Code:
jnpr@M7i-B> op find-arp —
Possible completions:
  <[Enter]>            Execute this command
  <name>               Argument name
  detail               Display detailed output
  interface            Name of physical or logical interface
  ip                   IP address of neighbor
  mac                  MAC Address of neighbor xx:xx:xx:xx:xx:xx
  |                    Pipe through a command
jnpr@M7i-B> op find-arp interface fxp0.0
00:c0:9f:42:9a:39     172.19.91.46     fxp0.0
00:10:db:c6:a7:88     172.19.91.254     fxp0.0

jnpr@M7i-B> op find-arp ip 12.1.1.1
00:19:e2:92:5c:02       12.1.1.1        ge-0/0/2.0

jnpr@M7i-B> op find-arp mac 00:10:db:c6:a7:88
00:10:db:c6:a7:88     172.19.91.254     fxp0.0


 

SCRIPT - SOURCE CODE
Code:
<—xml version="1.0" standalone="yes"–>

<!-- find-arp command by: FERDINAND CASIDO -->

<xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 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">
 <xsl:import href="../import/junos.xsl"        />

 <xsl:variable name="arguments">

 <argument>
 <name>ip</name>
 <description>IP address of neighbor</description>
 </argument>

 <argument>
 <name>mac</name>
 <description>MAC Address of neighbor xx:xx:xx:xx:xx:xx</description>
 </argument>

 <argument>
 <name>interface</name>
 <description>Name of physical or logical interface</description>
 </argument>

 </xsl:variable>

 <xsl:param name="ip"/>
 <xsl:param name="mac"/>
 <xsl:param name="interface"/>

 <xsl:template match="/">
 <op-script-results>
 <xsl:variable name="result" select="jcs:invoke('get-arp-table-information')"/>

 <output>
 <xsl:for-each select="$result/arp-table-entry">

 <xsl:choose>

 <xsl:when test="ip-address = $ip">
 <xsl:value-of select="mac-address"/>
<xsl:text> </xsl:text>
 <xsl:value-of select="ip-address"/>
 <xsl:text> </xsl:text>
 <xsl:value-of select="interface-name"/>
 <xsl:text> </xsl:text>
 </xsl:when>

 <xsl:when test="mac-address = $mac">
 <xsl:value-of select="mac-address"/>
 <xsl:text>     </xsl:text>
 <xsl:value-of select="ip-address"/>
 <xsl:text>     </xsl:text>
 <xsl:value-of select="interface-name"/>
 <xsl:text> </xsl:text>
 </xsl:when>

 <xsl:when test="interface-name = $interface">
 <xsl:value-of select="mac-address"/>
 <xsl:text>     </xsl:text>
 <xsl:value-of select="ip-address"/>
 <xsl:text>     </xsl:text>
 <xsl:value-of select="interface-name"/>
 <xsl:text> </xsl:text>
 </xsl:when>

 <xsl:otherwise> </xsl:otherwise>

 </xsl:choose>

 </xsl:for-each>
 </output>
 </op-script-results>
 </xsl:template >

</xsl:stylesheet>