Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Download PNETLab Platform

PNETLAB Store
PNETLab.com

Use NETCONF Python ncclient to retrieve the


configuration of IOS XE

Lab Topology:

I. Requirement

Use Python ncclient to Connect to NETCONF


Use Python ncclient to Retrieve the Configuration
Use Python ncclient to Configure a Device : Configure Hostname (IOS_XE_NEW) and
Configure one more Loopback interafaces (L100 ==100.1.1.1/32).

II. Prerequisite

1
Download PNETLab Platform
PNETLAB Store
PNETLab.com

When you guys load the lab to your PNETlab, it will get all things of this lab except
Ubuntu_server docker. Therefore, you should install by yourself an Ubuntu_server docker and
connect to R2. Now I will show you how to do.
Step 1. Go to Device item of PNETlab site to get Ubuntu_server docker

Step 2. Read the guide to know how to do, or follow my method below.
Step 2.1 After you get Device, then go back to running lab on PNETlab box. Click “Add an
Object” , then Add new Node, then you choose the docker.io. After choosing that box, please
input information follow this picture

2
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Step 2.2 Then you go to Start up configure section. Insert one command line: dhclient eth1
Then save it, enable it. Follow the picture blow.

3
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Step 2.3 Now you have Ubunto_server docker, it is time to connect it to Cloud_NAT

All things related to Ubuntu_server docker are okay. Just click to that device and telnet . Then
just enjoy your Ubuntu in PNETlab now.

4
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Step 3. Check python version. Make sure it has suitable version. Version 3 is new version of
Python. // Click and telnet to your Ubunto , sodu -i with pass : admin to go to admin level.

admin@Ubuntu_server:~$ python3
Python 3.8.2 (default, Jul 16 2020, 14:00:26)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Step 4. Create the virtual environment


sudo apt-get install python3-venv
sudo apt-get install python3-venv
[sudo] password for admin:
Reading package lists... Done
Building dependency tree
Reading state information... Done
// If this command is not Done with this output “E: Unable to fetch some archives, maybe run
apt-get update or try with --fix-missing?”. Please use this command to fix it
apt-get update --fix-missing

Step 5. Create a Directory for Python that it is easy to control python files
mkdir python_on_PNETlab
cd python_on_PNETlab
python3 -m venv env

If problem happen here please use this command again: sudo apt-get install python3-venv
Then execute command again, make sure it is okay now : python3 -m venv env

5
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Step 6. IOS XE on “devices” ,


With quite same method above , you get CSR 1000v from “devices” first, then follow the
“guide” of install CSR 1000v. After that, you add node in your PNETlab to choose CSR 1000v
device, with one more option is :

Step 7 Configure initial cfg and ssh info, account for IOS XE

enable
configure terminal
hostname IOS_XE
no ip domain lookup
line vty 0 15

6
Download PNETLab Platform
PNETLAB Store
PNETLab.com

exec-t 0 0
logg sync
login local
transport input ssh
ip domain name example.pnetlab.com
crypto key generate rsa modulus 2048
username cisco priv 15 password cisco

III. Labbing showtime

Step 1: Verify that ncclient is installed and ready for use


root@Ubuntu_server:~/python_on_PNETlab# pip3 list --
format=columns
Package Version
------------- ----------
bcrypt 3.1.7
certifi 2019.11.28
chardet 3.0.4
cryptography 2.8
dbus-python 1.2.16
distro 1.4.0
future 0.18.2
idna 2.8
7
Download PNETLab Platform
PNETLAB Store
PNETLab.com

netmiko 2.4.2
paramiko 2.6.0
pip 20.0.2
PyGObject 3.36.0
PyNaCl 1.3.0
pyserial 3.4
requests 2.22.0
scp 0.13.0
setuptools 45.2.0
six 1.14.0
ssh-import-id 5.10
textfsm 1.1.0
urllib3 1.25.8
wheel 0.34.2
root@Ubuntu_server:~/python_on_PNETlab#
Don’t have ncclient module yet so you we should install it . We use the
common command: pip3 install ncclient
root@Ubuntu_server:~/python_on_PNETlab# pip3 list --
format=columns
Package Version
------------- ----------
bcrypt 3.1.7
ncclient 0.6.9
8
Download PNETLab Platform
PNETLAB Store
PNETLab.com

Step 2. Create python file name : python_netconf


Then insert full configuration below. I take many note inside the code,
you guys will understand how to configure Hostname and 2 loopback
interfaces by python
#start of coding
from ncclient import manager
import xml.dom.minidom

#The m object returned by the manager.connect() function represents


the NETCONF remote session. As you saw in previous lab,
#in every NETCONF session, the server first sends its list of capabilities
which is a list, in XML format, of supported YANG models.
# With the ncclient module, the received list of capabilities is stored in
the m.server_capabilities list.
m = manager.connect(
host="10.0.137.86",
port=830,
username="cisco",
password="cisco",
hostkey_verify=False
)

9
Download PNETLab Platform
PNETLAB Store
PNETLab.com

#You can use the get_config() method of the m NETCONF session object
to retrieve the configuration for IOS_XE.
#The get_config() method expects a source string parameter that
specifies the source NETCONF datastore. Use a print function to display
the results.
#The only NETCONF datastore currently on IOS_XE is the running
datastore. You can verify this with the show netconf-yang datastores
command.
#If you want to skip displaying the output from Part 3, comment out
the block of statements that print the capabilities, as shown in the
following:

print("#Supported Capabilities (YANG models):")


for capability in m.server_capabilities:
print(capability)

netconf_reply = m.get_config(source="running")

#Python has built in support for working with XML files.


#The xml.dom.minidom module can be used to prettify the output with
the toprettyxml() function.

print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())

10
Download PNETLab Platform
PNETLAB Store
PNETLab.com

#A network administrator may only want to retrieve a portion of the


running configuration on a device.
#NETCONF supports returning only data that is defined in a filter
parameter of the get_conf() function.
#Create a variable called netconf_filter that only retrieves data defined
by the Cisco IOS XE Native YANG model.

netconf_filter = """
<filter>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native" />
</filter>
"""
netconf_reply = m.get_config(source="running", filter=netconf_filter)
print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())

#Step 1: Use ncclient to edit the hostname for IOS_XE


# Easy to read XML format by : https://jsonformatter.org/xml-pretty-
print
#or another web: https://codebeautify.org/xmlviewer

11
Download PNETLab Platform
PNETLAB Store
PNETLab.com

netconf_hostname = """
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<hostname>IOS_XE_NEW</hostname>
</native>
</config>
"""

netconf_reply = m.edit_config(target="running",
config=netconf_hostname)
print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())

#Use ncclient to create a new loopback interface on IOS_XE

netconf_loopack = """
<config>
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<interface>
<Loopback>
<name>100</name>
<description>Loopback 100</description>
<ip>

12
Download PNETLab Platform
PNETLAB Store
PNETLab.com

<address>
<primary>
<address>100.1.1.1</address>
<mask>255.255.255.255</mask>
</primary>
</address>
</ip>
</Loopback>
</interface>
</native>
</config>
"""

netconf_reply = m.edit_config(target="running",
config=netconf_loopack)
print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())
#the end of code, enjoy your lab.

13

You might also like