Connect to the NETCONF Server in Perl Client Applications
The following sections explain how to use the NET::Netconf::Manager
object in a Perl client application
to connect to the NETCONF server on a device running Junos OS:
Satisfy Protocol Prerequisites
The NETCONF server supports several access protocols. For each connection to the NETCONF server on a device running Junos OS, the application must specify the protocol it is using. Perl client applications can communicate with the NETCONF server via SSH only.
Before your application can run, you must satisfy the prerequisites
for SSH. This involves enabling NETCONF on the device by configuring
the set system services netconf ssh
statement.
Group Requests
Establishing a connection to the NETCONF server on a device
running Junos OS is one of the more time-intensive and resource-intensive
functions performed by an application. If the application sends multiple
requests to a device, it makes sense to send all of them within the
context of one connection. If your application sends the same requests
to multiple devices, you can structure the script to iterate through
either the set of devices or the set of requests. Keep in mind, however,
that your application can effectively send only one request to one
NETCONF server at a time. This is because the NET::Netconf::Manager
object does not return control to the application until it receives
the closing </rpc-reply>
tag that represents
the end of the NETCONF server's response to the current request.
Obtain and Record Parameters Required by the NET::Netconf::Manager Object
The NET::Netconf::Manager
object
takes the following required parameters, specified as keys in a Perl
hash:
access
—The access protocol to use when communicating with the NETCONF server. Before the application runs, satisfy the SSH prerequisites.hostname
—The name of the device to which to connect. For best results, specify either a fully-qualified hostname or an IP address.login
—The username under which to establish the connection to the NETCONF server and issue requests. The username must already exist on the specified device and have the permission bits necessary for making the requests invoked by the application.password
—The password corresponding to the username.
The sample scripts in the NETCONF Perl distribution record
the parameters in a Perl hash called %deviceinfo
, declared as follows:
my %deviceinfo = ( 'access' => $access, 'login' => $login, 'password' => $password, 'hostname' => $hostname, );
The sample scripts included in the NETCONF Perl client distribution obtain the parameters from options entered on the command line by a user. For more information about collecting parameter values interactively, see Collect Parameters Interactively in NETCONF Perl Client Applications. Your application can also obtain values for the parameters from a file or database, or you can hardcode one or more of the parameters into the application code if they are constant.
Obtaining Application-Specific Parameters
In addition to the parameters required by the NET::Netconf::Manager
object, applications might need to define other parameters, such
as the name of the file to which to write the data returned by the
NETCONF server in response to a request.
As with the parameters required by the NET::Netconf::Manager
object, the client application can hardcode the values in the application
code, obtain them from a file, or obtain them interactively. The sample
scripts obtain values for these parameters from command-line options
in the same manner as they obtain the parameters required by the NET::Netconf::Manager
object. Several examples follow.
The following line enables a debugging trace if the user
includes the -d
command-line option:
my $debug_level = $opt{'d'};
The following line sets the $outputfile
variable to the value specified by the -o
command-line
option. It names the local file to which the NETCONF server's response
is written. If the -o
option is not provided, the variable
is set to the empty string.
my $outputfile = $opt{'o'} || "";
Establishing the Connection
After obtaining values for the parameters required for
the NET::Netconf::Manager
object, each
sample script records them in the %deviceinfo
hash.
my %deviceinfo = ( 'access' => $access, 'login' => $login, 'password' => $password, 'hostname' => $hostname, );
The script then invokes the NETCONF-specific new
subroutine to create a NET::Netconf::Manager
object and establish a connection to the specified routing, switching,
or security platform. If the connection attempt fails (as tested by
the ref
operator), the script exits.
my $jnx = new Net::Netconf::Manager(%deviceinfo); unless (ref $jnx) { croak "ERROR: $deviceinfo{hostname}: failed to connect.\n"; }