Help us improve your experience.

Let us know what you think.

Do you have time for a two-minute survey?

header-navigation
keyboard_arrow_up
close
keyboard_arrow_left
list Table of Contents
file_download PDF
{ "lLangCode": "en", "lName": "English", "lCountryCode": "us", "transcode": "en_US" }
English
keyboard_arrow_right

Collect Parameters Interactively in NETCONF Perl Client Applications

date_range 15-Jul-21

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.

content_copy zoom_out_map
my %opt;
getopts('l:p:d:f:m:o:h', \%opt) || output_usage();
output_usage() if $opt{'h'};

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.

content_copy zoom_out_map
sub output_usage
{
    my $usage = "Usage: $0 [options] <target>

Where:

  <target>   The hostname of the target device.

Options:

  -l <login>    A login name accepted by the target device.
  -p <password> The password for the login name.
  -m <access>   Access method. The only supported method is 'ssh'.
  -f <xmlfile>  The name of the XML file to print server response to.
                Default: chassis_inventory.xml
  -o <filename> output is written to this file instead of standard output.
  -d <level>    Debug level [1-6]\n\n";

    croak $usage;
}

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.

content_copy zoom_out_map
# Get the hostname
my $hostname = shift || output_usage();

# Get the access method, can be ssh only
my $access = $opt{'m'} || 'ssh';
use constant VALID_ACCESS_METHOD => 'ssh';
output_usage() unless (VALID_ACCESS_METHOD =~ /$access/);

# Check for login name. If not provided, prompt for it
my $login = "";
if ($opt{'l'}) {
    $login = $opt{'l'};
} else {
    print STDERR "login: ";
    $login = ReadLine 0;
    chomp $login;
}

# Check for password. If not provided, prompt for it
my $password = "";
if ($opt{'p'}) {
    $password = $opt{'p'};
} else {
    print STDERR "password: ";
    ReadMode 'noecho';
    $password = ReadLine 0;
    chomp $password;
    ReadMode 'normal';
    print STDERR "\n";
}

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.

content_copy zoom_out_map
my $hostname = shift || output_usage();

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.

content_copy zoom_out_map
my $access = $opt{'m'} || 'ssh';
use constant VALID_ACCESS_METHOD => 'ssh';
output_usage() unless (VALID_ACCESS_METHOD =~ /$access/);

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.

content_copy zoom_out_map
my $login = "";
if ($opt{'l'}) {
    $login = $opt{'l'};
} else {
    print STDERR "login: ";
    $login = ReadLine 0;
    chomp $login;
}

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.

content_copy zoom_out_map
my $password = "";
if ($opt{'p'}) {
    $password = $opt{'p'};
} else {
    print STDERR "password: ";
    ReadMode 'noecho';
    $password = ReadLine 0;
    chomp $password;
    ReadMode 'normal';
    print STDERR "\n";
}

Change History Table

Feature support is determined by the platform and release you are using. Use Feature Explorer to determine if a feature is supported on your platform.

Release
Description
16.1
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.
footer-navigation