|
| 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