Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

 

Collecting Parameters Interactively in Junos XML Protocol Perl Client Applications

 

In a Junos XML protocol Perl client application, a script can interactively obtain the parameters required by the JUNOS::Device object from command-line. The Junos XML Protocol Perl distribution includes several sample Perl scripts that perform various functions on devices running Junos OS. Each sample script obtains the parameters required by the JUNOS::Device object from command-line options provided by the user who invokes the script. The script records the options in a Perl hash called %opt, using the getopts function defined in the Getopt::Std Perl module to read the options from the command line. (Scripts used in production environments probably do not obtain parameters interactively, so this section is important mostly for understanding the sample scripts.)

In the following example from the get_chassis_inventory.pl sample script, 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 four parameters required by the JUNOS::Device 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 or to the value "telnet" if the -m option is not provided. If the specified value does not match one of the values 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.