LAB: Fetching Running Configurations using NETCONF
In this lab, we will explore how to use the ncclient library to establish an SSH connection to a network device and
fetch the running configurations from a router. The ncclient library allows us to interact with network devices using
NETCONF protocol, which is a standardized protocol for managing network devices.
Step 1: Setting up the network device for configuration.
- Go to your eve-ng and add a Cisco CSR 1000V Routers and connect to Cloud1 to establish connectivity between
PC and router, make sure you modify the resources like shown below.
- Boot the router and provide IP to the management interface using DHCP.
enable
configure terminal
interface gig 1
ip address dhcp
no shutdown
exit
- Configure SSH on your router with the credentials: admin/cisco.
ip domain-name [Link]
cryptokey generate rsa general-keys modulus 1024
ip ssh ver 2
username admin privilege 15 password cisco
line vty 0 4
login local
transport input all
exit
- Enable Netconf on the Cisco Device.
netconf ssh
netconf-yang
- Verify the reachability by pinging the IP address of router from local device.
Step 2: Install the ncclient Library:
- Go to the terminal of your device using VS code or using cmd and run the following command to begin the
installation of the library ncclient:
python -m pip install ncclient
Step 3: Time to begin the script for creating the loopback interface on Router.
- Import the library into your code. Getpass is used to collect the information in hidden manner. parseString from
[Link] is used to convert the deserialized output of data in serialized manner.
- Create a dictionary for your device defining the required parameters.
-
- Call the [Link] module to connect with the router. (** before the dictionary name is used to tell the
python to consider the content of the dictionary as key-value pairs instead of single elements.)
- Following statements will fetch out the running configurations from the device and will print the output. You will
observe that the output will be printed out in deserialized manner.
- Now let’s try to convert this into serialized form by using the module parseString.
- Let’s also understand how we can write this data into a text file. We will create a text file and will open it in the
script using the path of that file like shown below. Write module helps in writing data inside the file.