項目一覧
例: カスタム警告メッセージの生成
Junos OS コミット スクリプトは、コミット操作中にカスタム警告メッセージを生成し、設定がカスタム設定ルールに準拠していない場合に警告することができます。コミット プロセスは警告の影響を受けません。この例では、特定のステートメントがデバイスコンフィギュレーションに含まれていない場合に、カスタム警告メッセージを生成するコミットスクリプトを作成します。
必要条件
Junos OS リリース 16.1R3 以降のリリース(Python スクリプトを使用する場合)。
概要とコミット スクリプト
コミットスクリプトを使用して、 source-route
ステートメントが [edit chassis]
階層レベルに含まれていない場合に表示されるカスタム警告メッセージを記述します。
スクリプトは、XSLT、SLAX、および Python で表示されます。
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:template match="configuration"> <xsl:if test="not(chassis/source-route)"> <xnm:warning> <xsl:call-template name="jcs:edit-path"> <xsl:with-param name="dot" select="chassis"/> </xsl:call-template> <message>IP source-route processing is not enabled.</message> </xnm:warning> </xsl:if> </xsl:template> </xsl:stylesheet>
SLAX 構文
version 1.2; 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"; match configuration { if (not(chassis/source-route)) { <xnm:warning> { call jcs:edit-path($dot = chassis); <message> "IP source-route processing is not enabled."; } } }
Python 構文
from junos import Junos_Configuration import jcs def main(): root = Junos_Configuration if not(root.xpath("./chassis/source-route")): jcs.emit_warning("IP source-route processing is not enabled.") if __name__ == '__main__': main()
構成
プロシージャ
手順
スクリプトをダウンロードして有効にし、テストします。コミット スクリプトで警告メッセージが正しく生成されるかどうかをテストするには、警告を発生させる条件が候補の構成に含まれていることを確認します。この例では、 source-route
ステートメントが [edit chassis]
階層レベルに含まれていないことを確認します。
このトピックの例をテストするには、次のようにします。
スクリプトをテキストファイルにコピーし、ファイル名を source-route.xsl、 source-route.slax、または source-route.py に適宜付けて、デバイスの /var/db/scripts/commit/ ディレクトリにコピーします。
手記:署名されていないPythonスクリプトは、rootまたはJunos OS
super-user
ログインクラスのユーザーによって所有されている必要があり、ファイル所有者のみがファイルへの書き込み権限を持つことができます。設定モードで、
[edit system scripts commit]
階層レベルでfile
ステートメントとスクリプトファイル名を設定します。[edit] user@host# set system scripts commit file source-route.xsl
スクリプトが Python で記述されている場合は、署名されていない Python スクリプトの実行を有効にします。
[edit] user@host# set system scripts language python
手記:Python 3 を使用して Python スクリプトを実行するように
language python3
ステートメントを設定するか、Python 2.7 を使用して Python スクリプトを実行するようにlanguage python
ステートメントを設定します。詳細については、「 言語」を参照してください。source-route
ステートメントが[edit chassis]
階層レベルに含まれている場合は、delete chassis source-route
設定モードコマンドを発行します。[edit] user@host# delete chassis source-route
検証
スクリプトの実行の検証
目的
コミット スクリプトによって生成された警告メッセージを確認します。
アクション
commit check
または commit
コマンドを実行し、出力を確認します。コミットスクリプトは、設定の[edit chassis]
階層レベルにsource-route
ステートメントが含まれていない場合、警告メッセージを生成します。この警告はコミット プロセスには影響しません。
[edit] user@host# commit check [edit chassis] warning: IP source-route processing is not enabled. configuration check succeeds
[edit] user@host# commit [edit chassis] warning: IP source-route processing is not enabled. commit complete
XML 形式の警告メッセージを表示するには、 commit check | display xml
コマンドを発行します。
[edit] user@host# commit check | display xml <rpc-reply xmlns:junos="http://xml.juniper.net/junos/10.0R1/junos"> <commit-results> <routing-engine junos:style="normal"> <name>re0</name> <xnm:warning> <edit-path> [edit chassis] </edit-path> <message> IP source-route processing is not enabled. </message> </xnm:warning> <commit-check-success/> </routing-engine> </commit-results> </rpc-reply>
コミット スクリプト処理の詳細なトレースを表示するには、 commit check | display detail
コマンドを発行します。
[edit] user@host# commit check | display detail 2009-06-15 14:40:29 PDT: reading commit script configuration 2009-06-15 14:40:29 PDT: testing commit script configuration 2009-06-15 14:40:29 PDT: opening commit script '/var/db/scripts/commit/source-route-warning.xsl' 2009-06-15 14:40:29 PDT: reading commit script 'source-route-warning.xsl' 2009-06-15 14:40:29 PDT: running commit script 'source-route-warning.xsl' 2009-06-15 14:40:29 PDT: processing commit script 'source-route-warning.xsl' [edit chassis] warning: IP source-route processing is not enabled. 2009-06-15 14:40:29 PDT: no errors from source-route-warning.xsl 2009-06-15 14:40:29 PDT: saving commit script changes 2009-06-15 14:40:29 PDT: summary: changes 0, transients 0 (allowed), syslog 0 2009-06-15 14:40:29 PDT: no commit script changes 2009-06-15 14:40:29 PDT: exporting juniper.conf 2009-06-15 14:40:29 PDT: expanding groups 2009-06-15 14:40:29 PDT: finished expanding groups 2009-06-15 14:40:29 PDT: setup foreign files 2009-06-15 14:40:29 PDT: propagating foreign files 2009-06-15 14:40:30 PDT: complete foreign files 2009-06-15 14:40:30 PDT: daemons checking new configuration configuration check succeeds