예: NETCONF Perl 클라이언트 애플리케이션을 사용하여 구성 변경
NETCONF Perl 배포에는 Junos OS 실행하는 디바이스에서 다양한 기능을 수행하기 위한 여러 샘플 펄 스크립트가 포함되어 있습니다. 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;
서브루틴의 처음 두 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 스크립트가 호출될 때 명령줄 옵션이 포함되어 있는 경우, 변수가 참조하는 파일에는 CLI 구성 모드 show
명령에 의해 $xmlfile
반환된 것과 같은 ASCII 형식의 구성 문이 포함되어야 합니다. 구성 문이 ASCII 형식 텍스트로 되어 있는 경우 스크립트는 태그 요소 내의 변수에 $config
저장된 구성을 configuration-text
묶고 해시 키config-text
와 연관된 queryargs
값을 저장합니다.
-t
edit_configuration.pl 스크립트가 호출될 때 명령줄 옵션이 포함되지 않은 경우, 변수에서 $xmlfile
참조하는 파일에는 Junos XML 구성 태그 요소가 포함됩니다. 이 경우 스크립트는 해시 키와 queryargs
연관된 값으로 변수만 $config
저장합니다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); }