このページで
例:Opスクリプトを使用したDNSホスト名情報の表示
この例では、op スクリプトを使用して、ネットワーク内のデバイスの DNS(ドメイン名システム)情報を表示します。
要件
この例では、Junos OSを実行しているデバイスを使用しています。
概要と運用スクリプト
このスクリプトは、ネットワーク内のデバイスの DNS 情報を表示します。現在使用しているデバイスのDNS情報を show host hostname 表示するためにホスト名やIPアドレスを入力する必要がないため、このスクリプトではコマンドが若干改善されます。
コマンドに相当する show host hostname Junos 拡張マークアップ言語(XML)はありません。そのため、このスクリプトはshow host hostnameリモート プロシージャ コール(RPC)を使用するのではなく、 コマンドを直接使用します。
スクリプトは2つの異なるバージョンで提供され、1つは要素を <xsl:choose> 使用し、もう1つは関数を jcs:first-of() 使用します。どちらのバージョンも同じ引数を受け取り、同じ出力を生成します。各バージョンは、XSLT と SLAX の両方の構文で示されています。
<xsl:choose>要素を使用した XSLT 構文
<?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>
jcs:first-of()関数を使用した XSLT 構文
<?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>
<xslを使用したSLAX構文:選択>要素
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;
}
}
jcs:first-of()関数を使用したSLAX構文
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;
}
}
構成
手順
手順
スクリプトをダウンロード、有効化、テストするには、以下の手順にしたがっています。
XSLT または SLAX スクリプトをテキスト ファイルにコピーし、該当する場合は hostname.xsl または hostname.slax に名前を付け、デバイス上の /var/db/scripts/op/ ディレクトリにコピーします。
設定モードでは、 階層レベルに ステートメントを
file、必要に[edit system scripts op]応じて hostname.xsl または hostname.slax を 含めます。[edit system scripts op] user@host# set file hostname.(slax | xsl)
コマンドを
commit and-quit発行して設定をコミットし、動作モードに戻ります。[edit] user@host# commit and-quit
運用モード コマンドを発行して、op スクリプトを
op hostname <dns (hostname | address)>実行します。
検証
コミット スクリプトの実行の検証
目的
スクリプトが想定どおりに動作していることを確認します。
アクション
オプションなしで動作モードコマンドをop hostnamedns発行すると、ローカルデバイスのDNS情報が表示されます。
user@host1> op hostname Name: host1 has address 10.168.71.246
コマンドを op hostname dns hostname 発行すると、指定されたデバイスのDNS情報が表示されます。
user@host1> op hostname dns router1 Name: router1 has address 10.168.71.249
コマンドを op hostname dns address 発行すると、指定されたアドレスのDNS情報が表示されます。
user@host1> op hostname dns 10.168.71.249 Name: 249.71.168.10.IN-ADDR.ARPA domain name pointer router1