| •1 |
#!/usr/bin/python3 |
| 2 |
# |
| 3 |
|
| 4 |
import logging |
| 5 |
import sys |
| 6 |
|
| 7 |
from ncclient import manager |
| 8 |
from ncclient.xml_ import to_xml |
| 9 |
from ncclient.operations.rpc import RPCError |
| 10 |
|
| 11 |
_NS_MAP = { |
| 12 |
'nc': 'urn:ietf:params:xml:ns:netconf:base:1.0', |
| 13 |
'nokia-conf': 'urn:nokia.com:sros:ns:yang:sr:conf' |
| 14 |
} |
| 15 |
|
| 16 |
_NOKIA_MGMT_INT_FILTER = ''' |
| 17 |
<configure xmlns="%s"> |
| 18 |
<system> |
| 19 |
<management-interface/> |
| 20 |
</system> |
| 21 |
</configure> |
| 22 |
''' % (_NS_MAP['nokia-conf']) |
| 23 |
|
| 24 |
def connect(host, port, user, password): |
| 25 |
m = manager.connect(host=host, port=port, |
| 26 |
username=user, password=password, |
| 27 |
device_params={'name': 'sros'}, |
| 28 |
hostkey_verify=False) |
| 29 |
|
| 30 |
## Retrieve full configuration from the running datastore |
| 31 |
running_xml = m.get_config(source='running') |
| 32 |
logging.info(running_xml) |
| 33 |
|
| 34 |
## Retrieve full configuration from the running datastore and strip |
| 35 |
## the rpc-reply + data elements |
| 36 |
running_xml = m.get_config(source='running').xpath( |
| 37 |
'/nc:rpc-reply/nc:data/nokia-conf:configure', |
| 38 |
namespaces=_NS_MAP)[0] |
| 39 |
logging.info(to_xml(running_xml, pretty_print=True)) |
| 40 |
|
| 41 |
## Retrieve full configuration from the running datastore and strip |
| 42 |
## out elements except for /configure/system/management-interface |
| 43 |
running_xml = m.get_config(source='running').xpath( |
| 44 |
'/nc:rpc-reply/nc:data/nokia-conf:configure' \ |
| 45 |
'/nokia-conf:system/nokia-conf:management-interface', |
| 46 |
namespaces=_NS_MAP)[0] |
| 47 |
logging.info(to_xml(running_xml, pretty_print=True)) |
| 48 |
|
| 49 |
## Retrieve a specific filtered subtree from the running datastore |
| 50 |
## and handle any rpc-error should a portion of the filter criteria |
| 51 |
## be invalid |
| 52 |
try: |
| 53 |
running_xml = m.get_config(source='running', |
| 54 |
filter=('subtree', _NOKIA_MGMT_INT_FILTER), |
| 55 |
with_defaults='report-all').xpath( |
| 56 |
'/nc:rpc-reply/nc:data/nokia-conf:configure', |
| 57 |
namespaces=_NS_MAP)[0] |
| 58 |
logging.info(to_xml(running_xml, pretty_print=True)) |
| 59 |
|
| 60 |
except RPCError as err: |
| 61 |
logging.info('Error: %s' % err.message.strip()) |
| 62 |
|
| 63 |
m.close_session() |
| 64 |
|
| 65 |
if __name__ == '__main__': |
| 66 |
LOG_FORMAT = '%(asctime)s %(levelname)s %(filename)s:%(lineno)d %(message)s' |
| 67 |
logging.basicConfig(stream=sys.stdout, level=logging.INFO, format=LOG_FORMAT) |
| 68 |
|
| 69 |
try: |
| 70 |
connect(sys.argv[1], '830', 'admin', 'admin') |
| 71 |
|
| 72 |
except IndexError: |
| 73 |
logging.error('Must supply a valid hostname or IP address') |
| 74 |
|