Wednesday, March 17, 2021

How to add the yang model to the xml file for Cisco switch version 1741?

Hey guys so I found a python script to add ports to an existing vlan. However, I could not understand how to add the yang models to the xml attribute xmlns? I provided a link to the model on github and tried with and without the '.yang' file type. It just doesn't seem to work.

Any idea on how to include the correct yang model or if the url is wrong?

The code is provided below and so too is the error.

Thank you so much.

'''import modules to code''' 

'''import modules to code'''
from ncclient import manager
import xmltodict
import xml.dom.minidom
'''XML encoded YANG data - YANG vlan data model and YANG interface data model for IOS XE version 16.9.3
<native xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-native">
<vlan>
<vlan-list xmlns="http://cisco.com/ns/yang/Cisco-IOS-XE-vlan"><id>{vlan}</id><name>VLAN {vlan}</name></vlan-list>
</vlan>
</native>
'''
netconf_add_vlan_to_if = """
<config>
<native xmlns="https://github.com/YangModels/yang/tree/master/vendor/cisco/xe/1741/Cisco-IOS-XE-native.yang">
<interface>
<GigabitEthernet>
<name>Gi1/{number}</name>
<switchport>
<access xmlns="https://github.com/YangModels/yang/tree/master/vendor/cisco/xe/1741/Cisco-IOS-XE-switch.yang">
<vlan><vlan>{vlan}</vlan></vlan>
</access>
</switchport>
<description> Configured by NETCONF </description>
</GigabitEthernet>
</interface>
</native>
</config>"""
''' loop through 10 interfaces and assign vlans'''
for i in range(1,6):
if i <= 3:
vlan_id = 20
else:
vlan_id = 110
''' use ncclient to push down config'''
netconf_data = netconf_add_vlan_to_if.format(number=i, vlan = vlan_id)
with manager.connect(host="192.133.8.245",port="333",username="admin",password="Cisco123",
hostkey_verify=False) as m:
netconf_reply = m.edit_config(netconf_data, target = 'running')

'''print status'''
print("The configuration payload to be sent over NETCONF.\n")
print(netconf_data)   
print("Here is the raw XML data returned from the device.\n")
print(xml.dom.minidom.parseString(netconf_reply.xml).toprettyxml())
print("") 
print(str(i) + " OK")

Thank you.

[Running] python -u "c:\Users\blah\Documents\LinkedIn\Web Programming\Cisco\Switch\ncclient_vlans_netconf.py" 

Traceback (most recent call last):   File "c:\Users\zahid\Documents\LinkedIn\Web Programming\Cisco\Switch\ncclient_vlans_netconf.py", line 70, in <module>     netconf_reply = m.edit_config(netconf_data, target = 'running')   File "C:\Users\blah\anaconda3\envs\flask_trial\lib\site-packages\ncclient\manager.py", line 239, in execute     return cls(self._session,   File "C:\Users\blah\anaconda3\envs\flask_trial\lib\site-packages\ncclient\operations\edit.py", line 75, in request     return self._request(node)   File "C:\Users\blah\anaconda3\envs\flask_trial\lib\site-packages\ncclient\operations\rpc.py", line 367, in _request     raise self._reply.error ncclient.operations.rpc.RPCError: {'type': 'protocol', 'tag': 'unknown-element', 'app_tag': None, 'severity': 'error', 'info': '<?xml version="1.0" encoding="UTF-8"?><error-info xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><bad-element>config</bad-element>\n</error-info>\n', 'path': '\n    /rpc/edit-config\n  ', 'message': None}

[Done] exited with code=1 in 1.663 seconds



No comments:

Post a Comment