-
-
Save yakubcze/10d3686b7209ea675aaaa9742cfb01f7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*.yang |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Get YANG models from a device by using the get-schema RPC. | |
# | |
# Kristian Larsson <kristian@spritelink.net> | |
# | |
from urllib.parse import urlparse, parse_qs | |
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() | |
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 = parse_qs(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 = str(r) | |
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