Skip to content

vblazhkun/netconf-perl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NETCONF Perl client

   Contents
     * Abstract
     * Supported Platforms
     * Downloads
     * Installation
     * Running the Sample Scripts

Abstract

The NETCONF API provides mechanisms to install, manipulate, and delete the configuration of network devices. The NETCONF API uses an Extensible markup Language (XML) based data encoding for the configuration data as well as operations and messages defined in the API.

The Net::Netconf::Manager module provides an object-oriented interface for communicating with the NETCONF server so you can start using the NETCONF API quickly and easily. There are several modules in this library but client applications directly invoke the Manager object only. When the client application creates a Net::Netconf::Manager object, it specifies a router name and the login name to use when accessing the router (which determines the client application's access level).

The following code segment shows how to use the Net::Netconf::Manager object to request information from a routing platform. This example invokes the query called get_chassis_inventory.

 # Step 1: set up the query
 // RPC tag <get-chassis-inventory> should be querried as below
 my $query = "get_chassis_inventory";
 my %queryargs = ( detail => 1 );
 # Step 2: Create a Netconf Manager object and connect to Networks routing platform
 my %deviceinfo = (
  access => "ssh",
  login => "johndoe",
  password => "secret",
  hostname => "router11"
  );
  my $jnx = new Net::Netconf::Manager(%deviceinfo);
  unless ( ref $jnx ) {
         croak "ERROR: $deviceinfo{hostname}: failed to connect.\n";
  }
 # Step 3: send the query and receive a reply
 my $res = $jnx->$query( %queryargs );
 # Step 4: check for error
 if ($jnx->has_error) {
      croak "ERROR: in processing request \n $jnx->{request} \n";
  } 
  else {
 # Step 5: do something with the result
  }
 # Step 6: always disconnect from the server when you're done
 $jnx->disconnect();

Supported Platforms

The current version of this module has been tested on the following platforms. Later releases may support additional platforms.

 * Ubuntu 12.04LTS 
 * Fedora 15 i686
 * Centos-6.5-i386

Prerequisites

Following are the Prerequisites for using this API:
   1. Expect Module (it depends on tcl, tk, tcl-dev and tk-dev)
   2. File::Which
   3. XML::LibXML

Also make sure that your system has libxml2 and libxml2-dev library. If not then either install it from source package in ubuntu website or by using apt-get. For more info look at the Troubleshooting

Installation

    Instructions for UNIX Systems

     1. Make sure Perl is installed. If necessary, see Installation of
        Perl.
        % which perl
        % perl -v
        The NETCONF Perl Client requires version 5.6.1 or later of the perl executable. Verify that you are running
        that version of the perl executable. If not, check your PATH or install the latest release of perl.

    Install the prerequisites of Perl modules. 
    Following are the prerequites
    1. Expect Module (it depends on tcl, tk, tcl-dev and tk-dev)
    2. File::Which
    3. XML::LibXML
    
    Steps to install Prerequisites in Ubuntu12.04LTS :
    1. apt-get install tcl tcl-dev tk tk-dev
    2. apt-get install expect expect-dev
    3. cpan Expect
    4. cpan File::Which
    5. cpan XML::LibXML
        
    After successfully installing Prerequisites install NETCONF PERL CLIENT
      
    1. Download the Perl NETCONF zip archive from Juniper's Repository in github 
        
    2. Unzip the archive.
        % unzip netconf-Perl-master.zip
    3. Change to the NETCONF directory.
        % cd netconf-perl-master
    4. Create Net::Netconf Makefile.
       If installing Net::Netconf::Manager under the standard directory
        (by default it is /usr/local/lib):
        [/my/netconf-perl-master]% perl Makefile.PL
         
     6. Install the Net::Netconf module.
        [/my/netconf-perl-master]% make
        [/my/netconf-perl--master]% make install
        (to run make install user should have root permission)

Running the Sample Scripts

The NETCONF Perl distribution includes sample scripts that demonstrate how to use NETCONF to retrieve and change the configuration of a Networks routing platform. The samples reside in the netconf-perl-n.n/examples directory.

Reading configuration: System Information This example sends a request to the Networks routing platform and displays the result to the standard output.It also shows how to parse reply from server

        use Net::Netconf::Manager;
        print "Enter hostname\n";
        my $hostname = <>;
        print "Enter username\n";
        my $login= <>;
        print "Enter password\n";
        my $pass = <>;
        chomp($hostname);
        chomp($login);
        chomp($pass);
        $jnx = new Net::Netconf::Manager( 'access' => 'ssh',
                      'login' => $login,
                      'password' => $pass,
                      'hostname' => $hostname);
        if(! $jnx ) {
              print STDERR "Unable to connect to Junos device \n";
              exit 1;
         }
         print "Connection established: " . $jnx->get_session_id . "\n";
         my $reply=$jnx->get_system_information();
         if ($jnx->has_error) {
         print "ERROR: in processing request\n";
         # Get the error
         my $error = $jnx->get_first_error();
         $jnx->print_error_info(%$error);
         exit 1;
         }
         print "Rpc reply from server.\n";
         print ">>>>>>>>>>\n";
         print $reply;
         print "<<<<<<<<<<\n";
         
         # this parsing is specifically for <get-system-information> tag
         # you can write your own application in similar way
         #parsing reply from server
         my $config= $jnx->get_dom();
         $res= $config->getElementsByTagName("hardware-model")->item(0)->getFirstChild->getData;
         $res2= $config->getElementsByTagName("os-name")->item(0)->getFirstChild->getData;
         $res3= $config->getElementsByTagName("host-name")->item(0)->getFirstChild->getData;
         print "\nhardware information  ". $res ."\n";
         print "os-name  " .$res2 . "\n";
         print "host-name  ". $res3. "\n";
         $jnx->disconnect();

Troubleshooting (Ubuntu12.04LTS or higher version)

1. Installing libxml2 and libxml2-dev 
   - Install using apt-get
        apt-get install libxml2
        apt-get install libxml2-dev
   - Installing from source code
     Download your system specific package from http://packages.ubuntu.com/
     For Precise(12.04 LTS) download its .dpkg packages
     For libxml2: http://packages.ubuntu.com/precise/libxml2
     For libxml2-dev : http://packages.ubuntu.com/precise/libxml2-dev
     
     Install these packages in your system :
        dpkg -i libxml2_2.7xyz__.dpkg
        dpkg -i libxml2-dev_2xyz__.dpkg
         
If you get libxml related error even after installing its dependency like libxml2 and libxml2-dev then try 
below  command
        apt-get install libxml-libxml-perl
   
     
2. For YAML related Errors
   Sometimes you may get that yaml is not installed then run following commands:
         apt-get install libyaml-appconfig-perl
         apt-get install libconfig-yaml-perl
    
3. For cpan related errors:
   While installing some files using cpan, for example "cpan File::Which" you may get error like checksum mismatch for distribution, then try installing that package using "cpanm"
   First install cpanm in your system by:
   apt-get install cpanminus
   and then install desired package by using cpanm, for example:
   cpanm File::Which
   
   While installing perl dependency using cpan, if you get errors like "XML::NamespaceSupport package not found" or unmet dependency then first install that package using cpan / cpanm and then install your desired package.
   you can also try installing by force :
   apt-get -f install

About

Perl library for Netconf

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Perl 100.0%