Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
Automation Scripting User Guide
Table of Contents Expand all
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Example: Display DNS Hostname Information Using an Op Script

date_range 15-Nov-23

This example uses an op script to display Domain Name System (DNS) information for a device in your network.

Requirements

This example uses a device running Junos OS.

Overview and Op Script

This script displays DNS information for a device in your network. The script offers a slight improvement over the show host hostname command because you do not need to enter a hostname or IP address to view DNS information for the device you are currently using.

There is no Junos Extensible Markup Language (XML) equivalent for the show host hostname command. Therefore, this script uses the show host hostname command directly rather than using a remote procedure call (RPC).

The script is provided in two distinct versions, one using the <xsl:choose> element and the other using the jcs:first-of() function. Both versions accept the same argument and produce the same output. Each version is shown in both XSLT and SLAX syntax.

XSLT Syntax Using the <xsl:choose> Element

content_copy zoom_out_map
<?xml version="1.0" standalone="yes"?>
<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>dns</name>
            <description>Name or IP address of a host</description>
        </argument>
    </xsl:variable>
    <xsl:param name="dns"/>
    <xsl:template match="/">
        <op-script-results>
            <xsl:variable name="query">
                <xsl:choose>
                    <xsl:when test="$dns">
                        <command> 
                            <xsl:value-of select="concat('show host ', $dns)"/>
                        </command>
                    </xsl:when>
                    <xsl:when test="$hostname">
                        <command> 
                            <xsl:value-of select="concat('show host ', $hostname)"/>
                        </command>
                    </xsl:when>
                </xsl:choose>
            </xsl:variable>
            <xsl:variable name="result" select="jcs:invoke($query)"/>
            <xsl:variable name="host" select="$result"/>
            <output>
                <xsl:value-of select="concat('Name: ', $host)"/>
            </output>
        </op-script-results>
    </xsl:template>
</xsl:stylesheet>

XSLT Syntax Using the jcs:first-of() Function

content_copy zoom_out_map
<?xml version="1.0" standalone="yes"?>
<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>dns</name>
            <description>Name or IP address of a host</description>
        </argument>
    </xsl:variable>
    <xsl:param name="dns"/>
    <xsl:template match="/">
        <op-script-results>
            <xsl:variable name="target" select="jcs:first-of($dns, $hostname)"/>
            <xsl:variable name="query">
                <command>
                    <xsl:value-of select="concat('show host ', $target)"/>
                </command>
            </xsl:variable>
            <xsl:variable name="result" select="jcs:invoke($query)"/>
            <xsl:variable name="host" select="$result"/>
            <output>
                <xsl:value-of select="concat('Name: ', $host)"/>
            </output>
        </op-script-results>
    </xsl:template>
</xsl:stylesheet>

SLAX Syntax Using the <xsl:choose> Element

content_copy zoom_out_map
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> "dns";
        <description> "Name or IP address of a host";
    }
}
param $dns;
match / {
    <op-script-results> {
        var $query = {
            if ($dns) {
                <command> 'show host ' _  $dns;
            } else if ($hostname) {
                <command> 'show host ' _  $hostname;
            }
        }
        var $result = jcs:invoke($query);
        var $host = $result;
        <output> 'Name: ' _  $host;
    }
}

SLAX Syntax Using the jcs:first-of() Function

content_copy zoom_out_map
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> "dns";
        <description> "Name or IP address of a host";
    }
}
param $dns;

match / {
    <op-script-results> {
        var $target = jcs:first-of($dns, $hostname);
        var $query = {
            <command> 'show host ' _  $target;
        }
        var $result = jcs:invoke($query);
        var $host = $result;
        <output> 'Name: ' _  $host;
    }
}

Configuration

Procedure

Step-by-Step Procedure

To download, enable, and test the script:

  1. Copy the XSLT or SLAX script into a text file, name the file hostname.xsl or hostname.slax as appropriate, and copy 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 hostname.xsl or hostname.slax as appropriate.

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

    content_copy zoom_out_map
    [edit]
    user@host# commit and-quit
    
  4. Execute the op script by issuing the op hostname <dns (hostname | address)> operational mode command.

Verification

Verifying the Op Script Execution

Purpose

Verify that the script behaves as expected.

Action

When you issue the op hostname operational mode command without the dns option, DNS information is displayed for the local device:

content_copy zoom_out_map
user@host1> op hostname 
Name:
host1 has address 10.168.71.246

When you issue the op hostname dns hostname command, DNS information is displayed for the specified device:

content_copy zoom_out_map
user@host1> op hostname dns router1 
Name:
router1 has address 10.168.71.249

When you issue the op hostname dns address command, DNS information is displayed for the specified address:

content_copy zoom_out_map
user@host1> op hostname dns 10.168.71.249 
Name:
249.71.168.10.IN-ADDR.ARPA domain name pointer router1
footer-navigation