このページで
例:NETCONF Perl クライアント アプリケーションを使用した設定の変更
NETCONF Perl ディストリビューションには、Junos OS を実行しているデバイスでさまざまな機能を実行するためのいくつかのサンプル Perl スクリプトが含まれています。 edit_configuration.pl スクリプトは、デバイス上の設定をロック、変更、アップロード、コミットします。リクエストの送信に基本的な構造を使用しますが、エラーを graceful_shutdown
処理するサブルーチンも定義します。以下のセクションでは、スクリプトが実行するさまざまな機能について説明します。
エラー状態の処理
edit_configuration.plスクリプトのサブルーチンはgraceful_shutdown
、NETCONFセッションで発生したエラーを処理します。次の追加定数を使用します。
# query execution status constants use constant REPORT_SUCCESS => 1; use constant REPORT_FAILURE => 0; use constant STATE_CONNECTED => 1; use constant STATE_LOCKED => 2; use constant STATE_CONFIG_LOADED => 3;
サブルーチンの最初の2つのif
ステートメントは、 および STATE_LOCKED
条件をSTATE_CONFIG_LOADED
参照しています。これは、edit_configuration.plスクリプト内の設定の読み込みに特に適用されます。
sub graceful_shutdown { my ($jnx, $state, $success) = @_; if ($state >= STATE_CONFIG_LOADED) { # We have already done an <edit-config> operation # - Discard the changes print "Discarding the changes made ...\n"; $jnx->discard_changes(); if ($jnx->has_error) { print "Unable to discard <edit-config> changes\n"; } } if ($state >= STATE_LOCKED) { # Unlock the configuration database $jnx->unlock_config(); if ($jnx->has_error) { print "Unable to unlock the candidate configuration\n"; } } if ($state >= STATE_CONNECTED) { # Disconnect from the Netconf server $jnx->disconnect(); } if ($success) { print "REQUEST succeeded !!\n"; } else { print "REQUEST failed !!\n"; } exit; }
設定のロック
edit_configuration.pl スクリプトのメイン セクションは、NETCONF サーバーへの接続を確立することから始まります。次に、メソッドをlock_configuration
呼び出して、構成データベースをロックします。エラーが発生した場合、スクリプトはエラー状態の処理で説明されているgraceful_shutdown
サブルーチンを呼び出します。
print "Locking configuration database ...\n"; my %queryargs = ( 'target' => 'candidate' ); $res = $jnx->lock_config(%queryargs); # See if you got an error if ($jnx->has_error) { print "ERROR: in processing request \n $jnx->{'request'} \n"; graceful_shutdown($jnx, STATE_CONNECTED, REPORT_FAILURE); }
設定データの読み取り
次のコード サンプルでは、 edit_configuration.pl スクリプトは、Junos XML 構成要素または ASCII 形式のステートメントを含むファイルを読み取り、解析します。機能サブセクションの詳細については、完全なコード サンプルに従います。
# Load the configuration from the given XML file print "Loading configuration from $xmlfile \n"; if (! -f $xmlfile) { print "ERROR: Cannot load configuration in $xmlfile\n"; graceful_shutdown($jnx, STATE_LOCKED, REPORT_FAILURE); } # Read in the XML file my $config = read_xml_file($xmlfile); print "\n\n$config \n\n"; %queryargs = ( 'target' => 'candidate' ); # If we are in text mode, use config-text arg with wrapped # configuration-text, otherwise use config arg with raw XML if ($opt{t}) { $queryargs{'config-text'} = '<configuration text> . $config . </configuration-text>'; } else { $queryargs{'config'} = $config;
前述のコードサンプルの最初のサブセクションでは、設定データを含むファイルの存在を検証します。ファイルの名前は、以前はコマンドラインから取得し、 変数に $xmlfile
割り当てられました。ファイルが存在しない場合、スクリプトはサブルーチンを graceful_shutdown
呼び出します。
print "Loading configuration from $xmlfile \n"; if (! -f $xmlfile) { print "ERROR: Cannot load configuration in $xmlfile\n"; graceful_shutdown($jnx, STATE_LOCKED, REPORT_FAILURE); }
その後、スクリプトはサブルーチンをread_xml_file
呼び出し、読み取り用のファイルを開き、その内容を変数に$config
割り当てます。キーtarget
は queryargs
値candidate
に設定されます。スクリプトがメソッドをedit_configuration
呼び出すと、候補の設定が編集されます。
# Read in the XML file my $config = read_xml_file($xmlfile); print "\n\n$config \n\n"; %queryargs = ( 'target' => 'candidate' );
-t
edit_configuration.plスクリプトが呼び出されたときにコマンドラインオプションが含まれている場合、変数が$xmlfile
参照するファイルには、CLI設定モードshow
コマンドで返されるようなASCIIフォーマット設定ステートメントが含まれている必要があります。設定ステートメントがASCII形式のテキストにある場合、スクリプトはタグ要素内の変数に$config
格納された設定をconfiguration-text
囲み、その結果をハッシュキーにqueryargs
関連付けられた値に格納しますconfig-text
。
-t
edit_configuration.pl スクリプトの呼び出し時にコマンド行オプションが含まれていない場合、変数が$xmlfile
参照するファイルには Junos XML 構成要素が含まれます。この場合、スクリプトはハッシュキーに$config
関連付けられたqueryargs
値として変数のみを格納しますconfig
。
if ($opt{t}) { $queryargs{'config-text'} = '<configuration text> . $config . </configuration-text>'; } else { $queryargs{'config'} = $config;
設定データの編集
このスクリプトは、 edit_config
構成変更をデバイスに読み込むメソッドを呼び出します。NETCONF サーバーからの応答に graceful_shutdown
エラーがある場合は、サブルーチンを呼び出します。
$res = $jnx->edit_config(%queryargs); # See if you got an error if ($jnx->has_error) { print "ERROR: in processing request \n $jnx->{'request'} \n"; # Get the error my $error = $jnx->get_first_error(); get_error_info(%$error); # Disconnect graceful_shutdown($jnx, STATE_LOCKED, REPORT_FAILURE);
設定のコミット
この時点までのエラーがない場合、スクリプトはメソッドを commit
呼び出して、デバイス上の設定をコミットしてアクティブな設定にします。
# Commit the changes print "Committing the <edit-config> changes ...\n"; $jnx->commit(); if ($jnx->has_error) { print "ERROR: Failed to commit the configuration.\n"; graceful_shutdown($jnx, STATE_CONFIG_LOADED, REPORT_FAILURE); }