このページの目次
例: Op スクリプトを使用したファイルのインポート
この例の op スクリプト は、Junos XML プロトコル file-get
操作を使用して、リモート サーバーからファイルの内容を読み取ります。
要件
この例では、Junos OS を実行しているデバイスを使用します。
概要と Op スクリプト
Junos XML プロトコル file-get
操作は、ファイルの内容を読み取ります。コマンドを使用する file-get
ための基本的な構文は次のとおりです。
<rpc> <file-get> <filename>value</filename> <encoding>value</encoding> </file-get> </rpc>
コマンドでは file-get
、以下の tag 要素が使用されます。
encoding
- (必須)使用するエンコーディングのタイプを指定します。、 、base64
またはraw
エンコーディングを使用できますASCII
。filename
—(必須)このタグ内に、インポートするファイルの完全パスまたは相対パスとファイル名を含めます。相対パスを使用する場合、操作がローカルで実行される場合file-get
、指定されたパスは /var/tmp/ ディレクトリに対する相対パスになります。操作が接続ハンドルのコンテキスト内でリモートで実行される場合、パスはユーザーのホーム ディレクトリに対する相対パスです。
ASCII エンコードを使用すると、 file-get
インポートされたファイル内のすべての制御文字が Unicode 文字 'SECTION SIGN' (U+00A7) に変換されます。
XSLT 構文
次のサンプル スクリプトは、リモート デバイスに接続し、指定されたファイルの内容を読み取ります。このスクリプトは、リモート デバイスの IP アドレスまたはホスト名、ファイル名、ファイル エンコーディングの 3 つの引数を取ります。変数は arguments
スクリプトのグローバル レベルで宣言されるため、引数の名前と説明はコマンド ライン インターフェイス (CLI) に表示されます。
スクリプトは、操作のリモート プロシージャ コール (RPC) を含む変数を宣言しfileget
ますfile-get
。コマンドライン引数は、 および encoding
タグ要素の値filename
を定義します。必須の引数myhost
が指定されていない場合、スクリプトはエラーを発行し、実行を停止します。それ以外の場合、スクリプトはリモート デバイスへの接続に使用するユーザー名とパスワードの入力を求めます。
リモート デバイスへの接続が成功すると、スクリプトは接続ハンドルのコンテキスト内で RPC を実行します。関数の結果jcs:execute()
である操作の出力file-get
は、変数に格納されますout
。操作でエラーが発生した場合、スクリプトはエラーを CLI に出力します。file-get
操作が成功すると、ファイルの内容が変数に格納されout
、CLI に出力されます。その後、リモートホストへの接続が閉じられます。
<?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>myhost</name> <description>IP address or hostname of the remote host</description> </argument> <argument> <name>filename</name> <description>name of file</description> </argument> <argument> <name>encoding</name> <description>ascii, base64, or raw</description> </argument> </xsl:variable> <xsl:param name="myhost"/> <xsl:param name="filename"/> <xsl:param name="encoding"/> <xsl:template match="/"> <op-script-results> <xsl:variable name="fileget"> <file-get> <filename> <xsl:value-of select="$filename"/> </filename> <encoding> <xsl:value-of select="$encoding"/> </encoding> </file-get> </xsl:variable> <xsl:choose> <xsl:when test="$myhost = ''"> <xnm:error> <message>missing mandatory argument 'myhost'</message> </xnm:error> </xsl:when> <xsl:otherwise> <xsl:variable name="username" select="jcs:get-input('Enter username: ')"/> <xsl:variable name="pw" select="jcs:get-secret('Enter password: ')"/> <xsl:variable name="connect" select="jcs:open($myhost, $username, $pw)"/> <xsl:choose> <xsl:when test="$connect"> <output>Connected to host. Reading file... </output> <xsl:variable name="out" select="jcs:execute($connect, $fileget)"/> <xsl:choose> <xsl:when test="$out//xnm:error"> <xsl:copy-of select="$out//xnm:error"/> </xsl:when> <xsl:otherwise> <output> <xsl:value-of select="concat('File contents: ', $out)"/> </output> </xsl:otherwise> </xsl:choose> <xsl:value-of select="jcs:close($connect)"/> </xsl:when> <xsl:otherwise> <output>No connection to host.</output> </xsl:otherwise> </xsl:choose> </xsl:otherwise> </xsl:choose> </op-script-results> </xsl:template> </xsl:stylesheet>
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> "myhost"; <description> "IP address or hostname of the remote host"; } <argument> { <name> "filename"; <description> "name of file"; } <argument> { <name> "encoding"; <description> "ascii, base64, or raw"; } } param $myhost; param $filename; param $encoding; match / { <op-script-results> { var $fileget = { <file-get> { <filename>$filename; <encoding>$encoding; } } if ($myhost = '') { <xnm:error> { <message> "missing mandatory argument 'myhost'"; } } else { var $username = jcs:get-input("Enter username: "); var $pw = jcs:get-secret("Enter password: "); var $connect = jcs:open($myhost, $username, $pw); if ($connect) { <output> "Connected to host. Reading file... \n"; var $out = jcs:execute($connect, $fileget); if ($out//xnm:error) { copy-of $out//xnm:error; } else { <output> "File contents: " _ $out; } expr jcs:close($connect); } else { <output> "No connection to host."; } } } }
構成
手順
手順
スクリプトをダウンロード、有効化、およびテストするには:
XSLT または SLAX スクリプトをテキスト ファイルにコピーし、必要に応じてファイルに import.xsl または import.slax という名前を付けて、デバイスの /var/db/scripts/op/ ディレクトリにコピーします。
設定モードでは、 ステートメントを
[edit system scripts op]
階層レベルに含めfile
、必要に応じて import.xsl または import.slax を使用します。[edit system scripts op] user@host# set file import.(slax | xsl)
コマンド
commit and-quit
を発行して設定をコミットし、運用モードに戻ります。[edit] user@host# commit and-quit
オペレーショナルモードコマンドを発行
op import
してopスクリプトを実行し、必要な引数を含めます。
検証
スクリプト引数の検証
目的
引数の名前と説明が CLI に表示されていることを確認します。
アクション
運用 op import ?
モードコマンドを発行します。CLI は、スクリプト内のグローバル arguments
変数内の定義に基づいて、スクリプト引数の補完候補をリストします。
user@host> op import ? Possible completions: <[Enter]> Execute this command <name> Argument name detail Display detailed output encoding ascii, base64, or raw filename name of file myhost IP address or hostname of the remote host | Pipe through a command
Op スクリプトの実行の検証
目的
スクリプトが期待どおりに動作することを確認します。
アクション
動作 op import myhost host encoding encoding filename file
モード コマンドを発行し、プロンプトが表示されたら適切なユーザー名とパスワードを含めます。スクリプトの実行が成功すると、要求されたファイルの内容が表示されます。例えば:
root@host> op import myhost router1 encoding ascii filename /var/db/scripts/op/test.slax Enter username: root Enter password: Connected to host. Reading file... File contents: 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"; ...
コマンドライン引数でリモートデバイスのIPアドレスまたはホスト名を指定しなかった場合、スクリプトはエラーを発行し、実行を停止します。
root@host> op import error: missing mandatory argument 'myhost'
また、指定されたパスまたはファイルが存在しない場合、スクリプトはエラーを発行します。
root@host> op import myhost router1 encoding ascii filename /var/db/scripts/op/test1.slax Enter username: root Enter password: Connected to host. Reading file... File contents: Failed to open file (/var/db/scripts/op/test1.slax): No such file or directory