Skip to content
Snippets Groups Projects
Commit 3ded86ca authored by Thomas Schneider's avatar Thomas Schneider
Browse files

Initial import

parents
No related branches found
No related tags found
No related merge requests found
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# nctool
Python tool for managing configuration of
[NETCONF](https://datatracker.ietf.org/doc/rfc6241/)-enabled devices.
#! /usr/bin/env python
import argparse
from getpass import getpass
import os
import os.path
import lxml.etree as et
from ncclient import manager
from tqdm import tqdm
def get_models(args):
try:
password = getpass(prompt="Password (C-d for none/agent only): ")
except EOFError:
password = None
with manager.connect(
host=args.host,
port=args.port,
username=args.username,
password=password,
timeout=90,
device_params={"name": "iosxe"},
) as m:
caps = list(map(lambda x: x.strip(), m.server_capabilities))
if not any(map(lambda x: x.startswith(manager.NETCONF_MONITORING_NS), caps)):
raise Exception("device does not support rfc6022")
_filter = et.fromstring(
f"""
<netconf-state xmlns="{manager.NETCONF_MONITORING_NS}">
<schemas/>
</netconf-state>"""
)
res = m.get(("subtree", _filter))
prefix = f"{{{manager.NETCONF_MONITORING_NS}}}"
os.makedirs(args.destination, exist_ok=True)
pbar = tqdm(
res.data.findall(
"./x:netconf-state/x:schemas/x:schema",
{"x": manager.NETCONF_MONITORING_NS},
)
)
for schema in pbar:
identifier = schema.findtext(prefix + "identifier")
assert identifier is not None
version = schema.findtext(prefix + "version")
fmt = schema.findtext(prefix + "format")
assert any(
[el.text == "NETCONF" for el in schema.findall(prefix + "location")]
)
if version is not None:
v = f"@{version}"
else:
v = ""
filename = f"{identifier}{v}.{fmt}"
# pbar.set_description(filename)
# Don’t pass format because IOS XE doesn’t eat its own definition.
r = m.get_schema(identifier, version)
with open(os.path.join(args.destination, filename), "w") as fp:
fp.write(r.data)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-a", "--host", type=str, required=True, help="Device IP address or Hostname"
)
parser.add_argument(
"-u",
"--username",
type=str,
required=True,
help="Device Username (netconf agent username)",
)
parser.add_argument("--port", type=int, default=830, help="Netconf agent port")
subparsers = parser.add_subparsers(required=True)
parser_gm = subparsers.add_parser(
"get-models", aliases=["gm"], help="Get YANG models from device"
)
parser_gm.add_argument(
"-d", "--destination", default="models", help="Destination directory"
)
parser_gm.set_defaults(func=get_models)
args = parser.parse_args()
args.func(args)
This diff is collapsed.
[project]
name = "nctool"
version = "0.1.0"
description = ""
authors = [
{name = "Thomas Schneider", email = "thomas@fsmpi.rwth-aachen.de"},
]
requires-python = ">=3.10"
readme = "README.md"
license = {text = "EUPL-1.2"}
dependencies = [
"ncclient>=0.6.13",
"lxml>=4.9.2",
"tqdm>=4.65.0",
]
[tool.pdm.dev-dependencies]
dev = [
"ruff>=0.0.261",
]
[build-system]
requires = ["pdm-backend"]
build-backend = "pdm.backend"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment