Skip to content

Instantly share code, notes, and snippets.

@plajjan
Created September 14, 2016 08:51
Show Gist options
  • Save plajjan/30d9697d1bef040cd7f1292ff48fcef3 to your computer and use it in GitHub Desktop.
Save plajjan/30d9697d1bef040cd7f1292ff48fcef3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Get YANG models from a device by using the get-schema RPC.
#
# Kristian Larsson <[email protected]>
#
import urlparse
import logging
from ncclient import manager
import ncclient
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--username", required=True, help="Username")
parser.add_argument("-p", "--password", required=True, help="Password")
parser.add_argument("-P", "--port", default=830, help="Port")
parser.add_argument("hosts", nargs="+")
args = parser.parse_args()
logging.basicConfig(level=logging.DEBUG)
for host in args.hosts:
with manager.connect(host=host, port=args.port, username=args.username,
password=args.password, hostkey_verify=False,
device_params={'name': 'junos'}) as m:
for cap in m.server_capabilities:
print "Capability:", cap
cap_parsed = urlparse.parse_qs(urlparse.urlparse(cap).query)
if 'module' in cap_parsed:
module_name = cap_parsed['module'][0]
else:
module_name = cap
print "Retrieving module:", module_name
try:
r = m.get_schema(identifier=module_name)
except ncclient.operations.rpc.RPCError as exc:
print "Failed, doing next.."
continue
dx = r._data
try:
with open("%s.yang" % module_name, 'w') as f:
f.write(dx)
except Exception as exc:
print "CRAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP", exc
continue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment