Skip to content

Commit 400d362

Browse files
committed
Add nc_entity.py, a Python script to print ENTITY-MIB results from
a device.
1 parent cd93651 commit 400d362

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

netconf_entity/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# NETCONF: Print the ENTITY-MIB tree from a device
2+
3+
This is a Python script that uses ncclient to get the ENTITY-MIB
4+
tree from a device and print it as "pretty" XML.
5+
6+
For example:
7+
8+
```
9+
nc_entit.py -a 10.1.1.1 -u admin -p admin --port 830
10+
<?xml version="1.0" ?>
11+
<data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0">
12+
<ENTITY-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:ENTITY-MIB">
13+
<entityGeneral>
14+
<entLastChangeTime>114619</entLastChangeTime>
15+
</entityGeneral>
16+
<entPhysicalTable>
17+
<entPhysicalEntry>
18+
<entPhysicalIndex>1</entPhysicalIndex>
19+
<entPhysicalDescr>Cisco CSR1000V Chassis</entPhysicalDescr>
20+
<entPhysicalVendorType>1.3.6.1.4.1.9.12.3.1.3.1165</entPhysicalVendorType>
21+
<entPhysicalContainedIn>0</entPhysicalContainedIn>
22+
...
23+
```
24+
25+
## Setup
26+
27+
This script has been tested with Python 2.7, and requires the following non-default module(s):
28+
29+
* ncclient (pip install ncclient)

netconf_entity/nc_entity.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env python
2+
#
3+
# Copyright (c) 2017 Joe Clarke <jclarke@cisco.com>
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions
8+
# are met:
9+
# 1. Redistributions of source code must retain the above copyright
10+
# notice, this list of conditions and the following disclaimer.
11+
# 2. Redistributions in binary form must reproduce the above copyright
12+
# notice, this list of conditions and the following disclaimer in the
13+
# documentation and/or other materials provided with the distribution.
14+
#
15+
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16+
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18+
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21+
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22+
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23+
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24+
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25+
# SUCH DAMAGE.
26+
#
27+
# This script retrieves the ENTITY-MIB tree from a device via NETCONF and
28+
# prints it out in a "pretty" XML tree.
29+
#
30+
31+
from ncclient import manager
32+
import xml.dom.minidom
33+
import logging
34+
import argparse
35+
import sys
36+
37+
if __name__ == '__main__':
38+
parser = argparse.ArgumentParser(
39+
prog=sys.argv[0], description='Print ENTITY-MIB data via NETCONF from a device')
40+
41+
parser.add_argument('-a', '--host', type=str, required=True,
42+
help="Device IP address or Hostname")
43+
parser.add_argument('-u', '--username', type=str, required=True,
44+
help="Device Username (NETCONF server username)")
45+
parser.add_argument('-p', '--password', type=str, required=True,
46+
help="Device Password (NETCONF server password)")
47+
parser.add_argument('--port', type=int, default=830,
48+
help="Netconf agent port")
49+
parser.add_argument('-d', '--debug', action='store_true',
50+
help="Enable ncclient debugging")
51+
52+
parser.set_defaults(debug=False)
53+
args = parser.parse_args()
54+
55+
if args.debug:
56+
logging.basicConfig(level=logging.DEBUG)
57+
58+
with manager.connect_ssh(host=args.host, port=args.port, username=args.username, hostkey_verify=False, password=args.password) as m:
59+
entity_filter = '''
60+
<filter>
61+
<ENTITY-MIB xmlns="urn:ietf:params:xml:ns:yang:smiv2:ENTITY-MIB"/>
62+
</filter>
63+
'''
64+
65+
try:
66+
c = m.get(entity_filter).data_xml
67+
print(xml.dom.minidom.parseString(c).toprettyxml())
68+
except Exception as e:
69+
print('Failed to execute <get> RPC: {}'.format(e))

0 commit comments

Comments
 (0)