09-10-2023 06:38 PM
Hello,
When running the following code to retrieve YANG capabilities, I do receive the following errors:
DK_YANG.py", line 11, in <module>
with manager.connect(
File "/usr/local/lib/python3.8/dist-packages/ncclient/manager.py", line 176, in connect
return connect_ssh(*args, **kwds)
File "/usr/local/lib/python3.8/dist-packages/ncclient/manager.py", line 143, in connect_ssh
session.connect(*args, **kwds)
File "/usr/local/lib/python3.8/dist-packages/ncclient/transport/ssh.py", line 401, in connect
self._post_connect()
File "/usr/local/lib/python3.8/dist-packages/ncclient/transport/session.py", line 112, in _post_connect
raise SessionError("Capability exchange timed out")
ncclient.transport.errors.SessionError: Capability exchange timed out
The following is enabled on the IOS XE switch:
netconf ssh
netconf-yang
Based on logs, netconf session has been established.
PY Code:
import re
from ncclient import manager
import urllib3
from getpass import getpass
USERNAME = input("Enter your username:")
PASSWORD = getpass(prompt='Input your password: ') # the default prompt is 'Password
# Context manager keeping ncclient connection alive for the duration of
# the context.
with manager.connect(
host='10.254.199.199', # IP address of the XR device in your pod
port=22, # Port to connect to
username=USERNAME, # SSH Username
password=PASSWORD, # SSH Password
hostkey_verify=False # Allow unknown hostkeys not in local store
) as m: # Context manager reference, i.e. instance of connected manager
# The rest of your script code should go here.
capabilities = []
# Write each capability to console
for capability in m.server_capabilities:
# Print the capability
print("Capability: %s" % capability)
# Store the capability list for later.
capabilities.append(capability)
# Sort the list alphabetically.
capabilities = sorted(capabilities)
# List of modules that we store for use later
modules = []
# Iterate server capabilities and extract supported modules.
for capability in capabilities:
# Scan the capabilities and extract modules via this regex.
# i.e., if this was the capability string:
# http://www.cisco.com/calvados/show_diag?module=show_diag&revision=2012-03-27
# then:
# show_diag
# .. would be the module printed.
# Scan capability string for module
supported_model = re.search('module=(.*)&', capability)
# If module found in string, store it.
if supported_model is not None:
# Module string was found, store it.
print("Supported Model: %s" % supported_model.group(1))
# Store the module for later use.
modules.append(supported_model.groups(0)[0])
# List of models that we want to download.
# We will get the schema for each and write it to disk.
models_desired = ['openconfig-extensions', 'openconfig-interfaces']
# Iterate each desired model and write it to ./lab/models/
for model in models_desired:
# Get the model schema.
schema = m.get_schema(model)
# Open new file handle.
with open("./{}.yang".format(model), 'w') as f:
# Write schema
f.write(schema.data)
09-10-2023 11:10 PM
Hi, the port is 830, not 22 - either change it in the connect-call or leave it empty (as it's the default value):
# CHANGE PORT TO 830
with manager.connect(
host='10.254.199.199', # IP address of the XR device in your pod
port=830, # Port to connect to
username=USERNAME, # SSH Username
password=PASSWORD, # SSH Password
hostkey_verify=False # Allow unknown hostkeys not in local store
) as m:
# LEAVE PORT OUT (830=DEFAULT)
with manager.connect(
host='10.254.199.199', # IP address of the XR device in your pod
username=USERNAME, # SSH Username
password=PASSWORD, # SSH Password
hostkey_verify=False # Allow unknown hostkeys not in local store
) as m:
09-12-2023 10:29 AM
Thank Marcel.
Should it work when I add in Cisco IOS XE config the following at global config level:
netconf ssh
Please advise.
Thanks,
Netmart
09-12-2023 10:42 AM
On IOS-XE you just need to configure netconf-yang + a level 15 user (local or via AAA), see https://developer.cisco.com/docs/ios-xe/#!enabling-netconf-on-ios-xe for details.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide