Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 

Collecting Parameters Interactively in NETCONF Perl Client Applications

 

In a NETCONF Perl client application, a script can interactively obtain the parameters required by the NET::Netconf::Manager object from the command-line.

The NETCONF Perl distribution includes several sample Perl scripts to perform various functions on devices running Junos OS. Each sample script obtains the parameters required by the NET::Netconf::Manager object from command-line options provided by the user who invokes the script. The scripts use the getopts function defined in the Getopt::Std Perl module to read the options from the command line and then record the options in a Perl hash called %opt. (Scripts used in production environments probably do not obtain parameters interactively, so this section is important mostly for understanding the sample scripts.)

The following example references the get_chassis_inventory.pl sample script from the NETCONF Perl GitHub repository at https://github.com/Juniper/netconf-perl/tree/master/examples/get_chassis_inventory.

Note

Prior to Junos OS Release 16.1, every Junos OS release included a new, release-dependent version of the NETCONF Perl client. Beginning in Junos OS Release 16.1, the NETCONF Perl client is release-independent, is hosted on GitHub and CPAN, and can manage devices running any version of the Junos OS release. The sample scripts in the release-dependent versions of the NETCONF Perl distribution differ from those in the release-independent version hosted on GitHub and CPAN.

The first parameter to the getopts function defines the acceptable options, which vary depending on the application. A colon after the option letter indicates that it takes an argument.

The second parameter, \%opt, specifies that the values are recorded in the %opt hash. If the user does not provide at least one option, provides an invalid option, or provides the -h option, the script invokes the output_usage subroutine, which prints a usage message to the screen.

The following code defines the output_usage subroutine for the get_chassis_inventory.pl sample script. The contents of the my $usage definition and the Where and Options sections are specific to the script, and differ for each application.

The get_chassis_inventory.pl script includes the following code to obtain values from the command line for the parameters required by the NET::Netconf::Manager object. A detailed discussion of the various functional units follows the complete code sample.

In the first line of the preceding code sample, the script uses the Perl shift function to read the hostname from the end of the command line. If the hostname is missing, the script invokes the output_usage subroutine to print the usage message, which specifies that a hostname is required.

The script next determines which access protocol to use, setting the $access variable to the value of the -m command-line option. If the specified value does not match the only valid value defined by the VALID_ACCESSES constant, the script invokes the output_usage subroutine to print the usage message.

The script then determines the username, setting the $login variable to the value of the -l command-line option. If the option is not provided, the script prompts for it and uses the ReadLine function (defined in the standard Perl Term::ReadKey module) to read it from the command line.

The script finally determines the password for the username, setting the $password variable to the value of the -p command-line option. If the option is not provided, the script prompts for it. It uses the ReadMode function (defined in the standard Perl Term::ReadKey module) twice: first to prevent the password from echoing visibly on the screen, and then to return the shell to normal (echo) mode after it reads the password.

Release History Table
Release
Description
Beginning in Junos OS Release 16.1, the NETCONF Perl client is release-independent, is hosted on GitHub and CPAN, and can manage devices running any version of the Junos OS release. The sample scripts in the release-dependent versions of the NETCONF Perl distribution differ from those in the release-independent version hosted on GitHub and CPAN.