350-401 Question 311
Single answerA network engineer has written the following Python script to retrieve the hostname of a Cisco device via NETCONF. However, the script is returning an error. Identify the issue in the code.
from ncclient import manager
with manager.connect(
host='192.168.1.1',
port=830,
username='admin',
password='password',
hostkey_verify=False
) as m:
filter = "<filter><native xmlns='http://cisco.com/ns/yang/Cisco-IOS-XE-native'></native></filter>"
response = m.get(filter=filter)
print(response['hostname'])
What is the issue?
- A
The 'hostkey_verify' parameter should be set to 'True' for secure connections.
- B
The 'filter' string is incorrectly formatted and does not match the YANG model.
- C
The 'response' object does not support dictionary-style indexing with square brackets.
- D
The 'port' parameter should be set to 22 instead of 830 for NETCONF.
Show answer and explanation
Correct answer: C
Explanation
The issue lies in the way the 'response' object is being accessed. The 'm.get()' method returns an XML object, not a dictionary, so using square brackets to access 'hostname' is incorrect. To fix this, the engineer should parse the XML response using a library like 'xml.etree.ElementTree' or 'lxml' to extract the hostname.
- A. Incorrect.
The 'hostkey_verify' parameter being set to 'False' is acceptable for testing purposes, though it is not recommended for production environments.
- B. Incorrect.
The format of the 'filter' string is correct and matches the structure of the YANG model for Cisco IOS XE.
- C. Correct.
The 'response' object returned by the 'm.get()' method is an XML object, and it does not support dictionary-style indexing. The correct approach is to parse the XML response to extract the hostname.
- D. Incorrect.
Port 830 is the default port for NETCONF, so this is correctly configured in the script.