Example: Displaying DNS Hostname Information Using an Op Script
This script displays Domain Name System (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 that using a remote procedure call (RPC). For more information, see Using RPCs and Operational Mode Commands in Op Scripts.
The script is provided in two distinct versions, one using the <xsl:choose> element and the other the jcs:first-of() function. Both versions accept the same argument and produce the same output.
XSLT Syntax Using the <xsl:choose> Element
<?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
<?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
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
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 the ex-hostname Script
To test the ex-hostname script,
perform the following steps:
- Copy the XSLT or SLAX script from Example: Displaying DNS Hostname Information Using an Op Script into a text file, name the file
ex-hostname.xslorex-hostname.slaxas appropriate, and copy it to the/var/db/scripts/opdirectory on the device. In configuration mode, include the file ex-hostname.extension statement at the [edit system scripts op] hierarchy level, substituting .slax or .xsl for extension as appropriate.
[edit system scripts op]file ex-hostname.(slax | xsl);Issue the commit and-quit command.
[edit]user@host# commit and-quit
When you issue the op ex-hostname operational mode command without the dns option, DNS information is displayed for the local device:
user@host> op ex-hostname Name: this-router has address 10.168.71.246
When you issue the op ex-hostname dns hostname command, DNS information is displayed for the specified device:
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, DNS information is displayed for the specified address:
user@host> op ex-hostname dns 10.168.71.249 Name: 249.71.168.10.IN-ADDR.ARPA domain name pointer router1
Hide Navigation Pane
Show Navigation Pane
Download
SHA1