[Contents] [Prev] [Next] [Index] [Report an Error]

Displaying DNS Hostname Information

This script displays Domain Name System (DNS) information for a routing platform 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 routing platform 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 that using a remote procedure call (RPC). For more information, see Using RPCs and Operational Mode Commands.

The script is shown as two distinct versions. These two versions accept the same argument and produce the same output. Version 1 uses the <xsl:choose> element. Version 2 uses the jcs:first-of() function.

XSLT Syntax 1: <xsl:choose>

<?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 2:
jcs:first-of()

<?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 1: <xsl:choose>

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 2:
jcs:first-of()

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

Testing ex-hostname.xsl

To test the example in this section, perform the following steps:

  1. From Displaying DNS Hostname Information, copy the XSLT or SLAX script into a text file, and name the file ex-hostname.xsl. Copy the ex-hostname.xsl file to the /var/db/scripts/op directory on your routing platform.
  2. Include the file ex-hostname.xsl statement at the [edit system scripts op] hierarchy level. If you are using the SLAX version of the script, change the filename to filename.slax.
    [edit system scripts op]
    file ex-hostname.xsl;
  3. Issue the commit and-quit command.
  4. When you issue the op ex-hostname operational mode command without the dns argument, the DNS information is displayed for the router you are currently using:
    [edit]
    user@host# op ex-hostname
    Name:
    this-router has address 10.168.71.246

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

    [edit]
    user@host# op ex-hostname dns router1
    Name:
    router1 has address 10.168.71.249

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

    [edit]
    user@host# op ex-hostname dns 10.168.71.249
    Name:
    249.71.168.10.IN-ADDR.ARPA domain name pointer router1

[Contents] [Prev] [Next] [Index] [Report an Error]