Navigation
Guide That Contains This Content
[+] Expand All
[-] Collapse All

    Example: Searching Files Using an Op Script

    This sample script searches a file on a device running Junos OS for lines matching a given regular expression. The example uses the jcs:grep template in an op script.

    Requirements

    This example uses a device running Junos OS.

    Overview and Op Script

    The jcs:grep template searches an ASCII file for lines matching a regular expression. The template resides in the junos.xsl import file, which is included with the standard Junos OS installation available on all switches, routers, and security devices running Junos OS. To use the jcs:grep template in a script, you must import the junos.xsl file into the script and map the jcs prefix to the namespace identified by the URI http://xml.juniper.net/junos/commit-scripts/1.0 .

    In this example, all values required for the jcs:grep template are defined as global parameters. The values for the parameters are passed into the script as command-line arguments. The following script defines two parameters, filename and pattern, which store the values of the input file path and the regular expression. If you omit either argument when you execute the script, the script generates an error and halts execution. Otherwise, the script calls the jcs:grep template and passes in the supplied arguments.

    If the regular expression contains a syntax error, the jcs:grep template generates an error: regex error message for each line in the file. If the regular expression syntax is valid, the template parses the input file. For each match, the template adds a <match> element, which contains <input> and <output> child tags, to the result tree. The template writes the matching string to the <output> child element and writes the corresponding matching line to the <input> child element:

    <match> {
         <input>
         <output>
    }

    In the SLAX script, the := operator copies the results of the jcs:grep template call to a temporary variable and runs the node-set function on that variable. The := operator ensures that the results variable is a node-set rather than a result tree fragment so that the script can access the contents. The XSLT script explicitly calls out the equivalent steps. The script then loops through all resulting input elements and prints each match.

    XSLT Syntax

    <?xml version="1.0" standalone="yes"?>
    <xsl:stylesheet 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" version="1.0">
        <xsl:import href="../import/junos.xsl"/>
    
         <xsl:variable name="arguments">
              <argument>
                   <name>filename</name>
                   <description>name of file in which to search for the specified pattern
                   </description>
              </argument>
              <argument>
                   <name>pattern</name>
                   <description>regular expression</description>
              </argument>
         </xsl:variable>
    
         <xsl:param name="filename"/> 
         <xsl:param name="pattern"/>
    
         <xsl:template match="/">
    
              <op-script-results>
                   <xsl:choose>
                        <xsl:when test="$filename = ''">
                             <xnm:error>
                                  <message>missing mandatory argument 'filename'</message>
                             </xnm:error>
                        </xsl:when>
                        <xsl:when test="$pattern = '';">
                             <xnm:error>
                                  <message>missing mandatory argument 'pattern'</message>
                             </xnm:error>
                        </xsl:when>
                        <xsl:otherwise>
                             <xsl:variable name="results-temp">
                                  <xsl:call-template name="jcs:grep">
                                       <xsl:with-param name="filename" select="$filename"/>
                                       <xsl:with-param name="pattern" select="$pattern"/>
                                  </xsl:call-template>
                             </xsl:variable>
                             <xsl:variable xmlns:ext="http://xmlsoft.org/XSLT/namespace"
                                  name="results" select="ext:node-set($results-temp)"/>
                             <output>
                                  <xsl:value-of select="concat('Search for ', $pattern, ' in ', $filename)"/>
                             </output>
                             <xsl:for-each select="$results//input">
                                  <output>
                                       <xsl:value-of select="."/>
                                  </output>
                             </xsl:for-each>
                        </xsl:otherwise>
                   </xsl:choose>
              </op-script-results>
     
         </xsl:template>
    </xsl:stylesheet>
    

    SLAX Syntax

    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";
    
    var $arguments = {
         <argument> {	
             <name> "filename";
             <description> "name of file in which to search for the specified pattern"; 	
         }
         <argument> {	
             <name> "pattern";
             <description> "regular expression"; 	
         }
    }
    
    param $filename;
    param $pattern;
    
    match / {
         <op-script-results> {
    
              if ($filename = '') {
                   <xnm:error> {
                        <message> "missing mandatory argument 'filename'";
                   }
              }
              else if ($pattern = '') {
                   <xnm:error> {
                        <message> "missing mandatory argument 'pattern'";
                   }
              }
              else {
                   var $results := { call jcs:grep($filename, $pattern);  }
    
                   <output> "Search for " _ $pattern _ " in " _ $filename;
                   for-each ($results//input) {
                        <output> .;
                   }
              }
         }
    }

    Configuration

    Step-by-Step Procedure

    To download, enable, and run the script:

    1. Copy the XSLT or SLAX script into a text file, name the file grep.xsl or grep.slax as appropriate, and download it to the /var/db/scripts/op/ directory on the device.
    2. In configuration mode, include the file statement at the [edit system scripts op] hierarchy level and grep.xsl or grep.slax as appropriate.

      [edit system scripts op]user@host# set file grep.(slax | xsl)
    3. Issue the commit and-quit command to commit the configuration and to return to operational mode.

      [edit]user@host# commit and-quit
    4. Execute the op script by issuing the op grep filename filename pattern pattern operational mode command.

    Verification

    Verifying the Script Arguments

    Purpose

    Verify that the argument names and descriptions appear in the command-line interface (CLI) help.

    Action

    Issue the op grep ? operational mode command. The CLI lists the possible completions for the script arguments based on the definitions within the global variable arguments in the script.

    user@host> op grep
    Possible completions:
      <[Enter]>            Execute this command
      <name>               Argument name
      detail               Display detailed output
      filename             name of file in which to search for the specified pattern
      pattern              regular expression
      |                    Pipe through a command

    Verifying Op Script Execution

    Purpose

    Verify that the script behaves as expected.

    Action

    If you issue the op grep command, but you fail to supply either the filename or the regex pattern, the script issues an error message and halts execution. For example:

    user@host> op grep filename /var/log/messages
    error: missing mandatory argument 'pattern'
    
    user@host> op grep pattern SNMP_TRAP_LINK_DOWN
    error: missing mandatory argument 'filename'

    When you issue the op grep filename filename pattern pattern command, the script lists all lines from the input file that match the regular expression.

    user@host> op grep filename /var/log/messages pattern SNMP_TRAP_LINK_DOWN
    Search for SNMP_TRAP_LINK_DOWN in /var/log/messages
    Feb 24 09:04:00  host mib2d[1325]: SNMP_TRAP_LINK_DOWN: ifIndex 543, ifAdminStatus down(2), ifOperStatus down(2), ifName
    lt-0/1/0.9
    Feb 24 09:04:00  host mib2d[1325]: SNMP_TRAP_LINK_DOWN: ifIndex 542, ifAdminStatus down(2), ifOperStatus down(2), ifName
    lt-0/1/0.10

    Published: 2013-03-05