Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 
 

Convert Disallowed Characters in Junos XML Protocol Perl Client Applications

Junos XML protocol Perl client applications that handle configuration data usually accept and output the data either as Junos XML tag elements or as formatted ASCII statements like those used in the Junos OS CLI. As described in XML and Junos XML Management Protocol Conventions Overview, certain characters cannot appear in their regular form in an XML document. These characters include the apostrophe ( ), the ampersand ( & ), the greater-than ( > ) and less-than ( < ) symbols, and the quotation mark ( " ). Because these characters might appear in formatted ASCII configuration statements, the script must convert the characters to the corresponding predefined entity references.

The Junos XML Protocol Perl distribution includes several sample Perl scripts that perform various functions on devices running Junos OS. The sample scripts include code to convert all disallowed characters to predefined entity references. The load_configuration.pl sample script uses the get_escaped_text subroutine to substitute predefined entity references for disallowed characters (the get_configuration.pl script includes similar code). The script first defines the mappings between the disallowed characters and predefined entity references, and sets the variable $char_class to a regular expression that contains all of the entity references, as follows:

The following code defines the get_escaped_text subroutine for the load_configuration.pl script. A detailed discussion of the subsections in the routine follows the complete code sample.

The first subsection of the preceding code sample reads in a file containing formatted ASCII configuration statements.

In the next subsection, the subroutine temporarily discards the lines that contain the opening <get-configuration> and closing </get-configuration> tags, then replaces the disallowed characters on each remaining line with predefined entity references and appends the line to the $input_string variable:

The subroutine concludes by replacing the opening <get-configuration> and closing </get-configuration> tags, and returning the converted set of statements: