Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?


Example: Change the Configuration Using a NETCONF Perl Client Application

The NETCONF Perl distribution includes several sample Perl scripts to perform various functions on devices running Junos OS. The script locks, modifies, uploads, and commits the configuration on a device. It uses the basic structure for sending requests but also defines a graceful_shutdown subroutine that handles errors. The following sections describe the different functions that the script performs:

Handling Error Conditions

The graceful_shutdown subroutine in the script handles errors encountered in the NETCONF session. It employs the following additional constants:

The first two if statements in the subroutine refer to the STATE_CONFIG_LOADED and STATE_LOCKED conditions, which apply specifically to loading a configuration in the script.

Locking the Configuration

The main section of the script begins by establishing a connection to a NETCONF server. It then invokes the lock_configuration method to lock the configuration database. If an error occurs, the script invokes the graceful_shutdown subroutine described in Handling Error Conditions.

Reading In the Configuration Data

In the following code sample, the script reads in and parses a file that contains Junos XML configuration tag elements or ASCII-formatted statements. A detailed discussion of the functional subsections follows the complete code sample.

The first subsection of the preceding code sample verifies the existence of the file containing configuration data. The name of the file was previously obtained from the command line and assigned to the $xmlfile variable. If the file does not exist, the script invokes the graceful_shutdown subroutine.

The script then invokes the read_xml_file subroutine, which opens the file for reading and assigns its contents to the $config variable. The queryargs key target is set to the value candidate. When the script calls the edit_configuration method, the candidate configuration is edited.

If the -t command-line option was included when the script was invoked, the file referenced by the $xmlfile variable should contain ASCII-formatted configuration statements like those returned by the CLI configuration-mode show command. If the configuration statements are in ASCII-formatted text, the script encloses the configuration stored in the $config variable within the configuration-text tag element and stores the result in the value associated with the queryargs hash key config-text.

If the -t command-line option was not included when the script was invoked, the file referenced by the $xmlfile variable contains Junos XML configuration tag elements. In this case, the script stores just the $config variable as the value associated with the queryargs hash key config.

Editing the Configuration Data

The script invokes the edit_config method to load the configuration changes onto the device. It invokes the graceful_shutdown subroutine if the response from the NETCONF server has errors.

Committing the Configuration

If there are no errors up to this point, the script invokes the commit method to commit the configuration on the device and make it the active configuration.