The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Net::Netconf::Device - Implements a remote Netconf device

SYNOPSIS

The Net::Netconf::Device module implements a remote Netconf device. It can be used to connect and talk to a Netconf server.

DESCRIPTION

The Net::Netconf::Device module implements an object-oriented interface to the Netconf API supported by Juniper Networks. Objects of this class represent the local side of connection to a Juniper Networks device running JUNOS, over which the Netconf protocol will be spoken.

EXAMPLE

This example connects to a Netconf server, locks the candidate database, updates the router's configuration, commits the changes and closes the Netconf session. It also does error handling.

    use Net::Netconf::Device;

    # State information constants
    use constant STATE_CONNECTED => 0;
    use constant STATE_LOCKED => 1;
    use constant STATE_EDITED => 2;

    # Variables used
    my $response;

    my %deviceargs = (
      'hostname' => 'routername',
      'login' => 'loginname',
      'password' => 'secret',
      'access' => 'ssh',
      'server' => 'netconf',
      'debug_level' => 1,
      'client_capabilities' => [
        'urn:ietf:params:xml:ns:netconf:base:1.0',
        'urn:ietf:params:xml:ns:netconf:capability:candidate:1.0',
        'urn:ietf:params:xml:ns:netconf:capability:confirmed-commit:1.0',
        'urn:ietf:params:xml:ns:netconf:capability:validate:1.0',
        'urn:ietf:params:xml:ns:netconf:capability:url:1.0?protocol=http,ftp,file',
        'http://xml.juniper.net/netconf/junos/1.0',
          ]
    );

    my $device = new Net::Netconf::Device(%deviceargs);
    print 'Could not create Netconf device' unless $device;

    # Lock the configuration datastore
    $response = $device->lock_config(target => 'candidate');
    graceful_shutdown($device, STATE_CONNECTED) if ($device->has_error);

    # Edit the configuration
    my %editargs = (
        target => 'candidate',
        config => '<config>
                     <configuration>
                       <interfaces>
                         <interface>
                           <name>fe-0/0/0</name>
                           <mtu>1200</mtu>
                         </interface>
                       </interfaces>
                     </configuration>
                   </config>'
    );

    $response = $device->edit_config(%editargs);
    graceful_shutdown($device, STATE_LOCKED) if ($device->has_error);

    # Commit the changes
    $response = $device->commit();
    graceful_shutdown($device, STATE_EDITED) if ($device->has_error);

    # Close the session
    $response = $device->close_session();
    print 'Unable to close the Netconf session' if ($device->has_error);
    # Disconnect
    $device->disconnect();

    sub graceful_shutdown
    {
        my ($device, $state) = @_;

        # Could not commit
        if ($state >= STATE_EDITED) {
            print "Failed to commit changes..So, discarding the changes...\n";
            $device->discard_changes();
        }

        # Could not edit the config
        if ($state >= STATE_LOCKED) {
            print "Failed to edit the configuration...\n";
            $device->unlock();
        }

        # Could not lock the candidate config
        if ($state >= STATE_CONNECTED) {
            print "Failed to lock the candidate datastore...\n";
            $device->close_session();
            $device->disconnect();
        }
        exit 0;
    }

CONSTRUCTOR

new(%ARGS)

The constructor accepts a hash table %ARGS containing the following keys:

hostname

Name of Juniper box to connect to.

login

Username to log into box as.

password

Password for login username.

access

Access method - can be 'ssh' only.

server

The server to connect to. Default is 'netconf'.

debug_level

The debugging level. Debug level = 1, Trace level = 2, Information level = 3, Warning level = 4, Error level = 5, Critical level = 6.

client_capabilities

The capabilities supported by the client as an array reference.

METHODS

connect()

This is called by the constructor to connect to a Netconf server.

disconnect()

Disconnects from a Netconf server and performs other clean-up operations.

has_error

Returns the number of <rpc-error> tags seen in the Netconf server response.

get_first_error

Returns a hash containing the first <rpc-error> message returned by the Netconf server on the last request. The hash keys include error_severity, error_message etc.

get_all_errors

Returns a hash containing all the <rpc-error> messages returned by the Netconf server key-ed by the error number.

SEE ALSO

  • Net::Netconf::Manager

  • Net::Netconf:Trace

Juniper Networks Perl Team, send bug reports, hints, tips and suggestions to netconf-support@juniper.net.

2 POD Errors

The following errors were encountered while parsing the POD:

Around line 791:

You forgot a '=back' before '=head1'

Around line 803:

=back doesn't take any parameters, but you said =back =head1 AUTHOR