Automated Network Configuration Management using NETCONF and WebEx Teams, Exams of Programming Languages

A python script that automates network configuration management using netconf. It demonstrates how to establish a netconf connection, retrieve the current running configuration, modify the configuration (e.g., change an interface description), verify the changes, and notify webex teams about the updates. The script uses the ncclient and requests libraries.

Typology: Exams

2021/2022

Uploaded on 01/11/2024

garcia-raul-jason
garcia-raul-jason 🇵🇭

1 document

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
task 1:
# Connect to the device using NETCONF
def establish_netconf_connection(ip, port, username, password):
return manager.connect(host=ip, port=port, username=username, password=password,
device_params={'name':'default'}, hostkey_verify=False)
# Retrieve current running configuration
def retrieve_running_configuration(netconf_manager):
return netconf_manager.get_config(source='running')
# Main function
def main():
# Establish NETCONF connection
with establish_netconf_connection(DEVICE_IP_ADDRESS, NETCONF_PORT_NUMBER,
NETCONF_USERNAME, NETCONF_PASSWORD) as netconf_manager:
# Get the current configuration
current_configuration = retrieve_running_configuration(netconf_manager)
print(xml.dom.minidom.parseString(current_configuration.xml).toprettyxml())
if name == "main":
main()
task 2:
# Modify configuration - Example: Change an interface description
def apply_configuration_change(netconf_manager, new_configuration):
netconf_manager.edit_config(target='running', config=new_configuration)
pf3
pf4
pf5

Partial preview of the text

Download Automated Network Configuration Management using NETCONF and WebEx Teams and more Exams Programming Languages in PDF only on Docsity!

task 1:

Connect to the device using NETCONF

def establish_netconf_connection(ip, port, username, password): return manager.connect(host=ip, port=port, username=username, password=password, device_params={'name':'default'}, hostkey_verify=False)

Retrieve current running configuration

def retrieve_running_configuration(netconf_manager): return netconf_manager.get_config(source='running')

Main function

def main():

Establish NETCONF connection

with establish_netconf_connection(DEVICE_IP_ADDRESS, NETCONF_PORT_NUMBER, NETCONF_USERNAME, NETCONF_PASSWORD) as netconf_manager:

Get the current configuration

current_configuration = retrieve_running_configuration(netconf_manager) print(xml.dom.minidom.parseString(current_configuration.xml).toprettyxml()) if name == "main": main() task 2:

Modify configuration - Example: Change an interface description

def apply_configuration_change(netconf_manager, new_configuration): netconf_manager.edit_config(target='running', config=new_configuration)

Main function

def main():

Establish NETCONF connection

with establish_netconf_connection(DEVICE_IP_ADDRESS, NETCONF_PORT_NUMBER, NETCONF_USERNAME, NETCONF_PASSWORD) as netconf_manager:

Define the new configuration changes

modified_configuration = """

GigabitEthernet1 New Description

"""

Apply the configuration changes

apply_configuration_change(netconf_manager, modified_configuration) if name == "main": main() task 3:

Main function

def main():

Establish NETCONF connection

if name == "main": main() *******************************************’ pip install ncclient requests from ncclient import manager import xml.dom.minidom import requests

Define the NETCONF settings

DEVICE_IP_ADDRESS = '192.168.233.129' NETCONF_PORT_NUMBER = '830' NETCONF_USERNAME = 'admin' NETCONF_PASSWORD = 'admin'

Connect to the device using NETCONF

def establish_netconf_connection(ip, port, username, password): return manager.connect(host=ip, port=port, username=username, password=password, device_params={'name':'default'}, hostkey_verify=False)

Retrieve current running configuration

def retrieve_running_configuration(netconf_manager): return netconf_manager.get_config(source='running')

Modify configuration - Example: Change an interface description

def apply_configuration_change(netconf_manager, new_configuration): netconf_manager.edit_config(target='running', config=new_configuration)

Notify WebEx Teams about the changes

def send_webex_notification(message, teams_token, teams_room_id): url = f"https://webexapis.com/v1/messages" headers = { "Authorization": f"Bearer {teams_token}", "Content-Type": "application/json" } data = { "roomId": teams_room_id, "text": message } requests.post(url, headers=headers, json=data)

Main function

def main(): with establish_netconf_connection(DEVICE_IP_ADDRESS, NETCONF_PORT_NUMBER, NETCONF_USERNAME, NETCONF_PASSWORD) as netconf_manager:

Get the current configuration

current_configuration = retrieve_running_configuration(netconf_manager) print(xml.dom.minidom.parseString(current_configuration.xml).toprettyxml())

Define the new configuration changes