Collect 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.
my %opt; getopts('l:p:dx: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.
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. It can be clear-text, ssl, ssh or telnet. Default: telnet. -x <format> The name of the XSL file to display the response. Default: xsl/chassis_inventory_csv.xsl -o <filename> File to which to write output, instead of standard output. -d Turn on debugging.\n\n"; die $usage; }
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.
# Get the hostname my $hostname = shift || output_usage(); # Get the access method my $access = $opt{m} || "telnet"; use constant VALID_ACCESSES => "telnet|ssh|clear-text|ssl"; output_usage() unless (VALID_ACCESSES =~ /$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.
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 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.
my $access = $opt{m} || "telnet"; use constant VALID_ACCESSES => "telnet|ssh|clear-text|ssl"; output_usage() unless (VALID_ACCESSES =~ /$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.
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.
my $password = ""; if ($opt{p}) { $password = $opt{p}; } else { print STDERR "password: "; ReadMode 'noecho'; $password = ReadLine 0; chomp $password; ReadMode 'normal'; print STDERR "\n"; }