Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev python exercise 1 #10

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
160 changes: 160 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# 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/
97 changes: 97 additions & 0 deletions solutions/python/exercise1/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""Get backups of running config from devices."""
import sys
import logging
from pathlib import Path
import yaml
from yaml.loader import FullLoader
from netmiko import (
ConnectHandler,
NetmikoTimeoutException,
NetmikoAuthenticationException,
)
from constants import USERNAME, PASSWORD, BACKUP_DIR, COMMANDS


def send_command(device: dict, command: str) -> str:
"""Connect with netmiko and send one command.

Args:
device (dict): Device info in netmiko dict format
command (str): Command to send to device

Returns:
str: command output
"""
output = ""
try:
logging.info(f"Connect to device { device['host'] }")
with ConnectHandler(**device) as ssh:
output = ssh.send_command(command)

return output
except (NetmikoTimeoutException, NetmikoAuthenticationException) as error:
logging.error(
f"Could not connect to device { device['host'] } with error {error}"
)
return output


def write_file(file_name: str, text: str):
"""Write text to file.

Args:
file_name (str): file name and parent dir
text (str): text to be written
"""
# Create parent directory if not exists
file_dir = Path(file_name).parent.absolute()
if not file_dir.exists():
logging.info(f"Creating directory { file_dir } for backups")
file_dir.mkdir(parents=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be probably moved into it's own function called once during the setup.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was my initial approach and changed it, changed it back to have it's own function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can user mkdir with exist_ok=True if you want to avoid the if check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, sounds better.


try:
logging.info(f"Write output to file { file_name }")
with open(file_name, "w", encoding="UTF-8") as f:
f.write(text)
except IOError as err:
logging.error(f"File { file_name } could not be opened!")
logging.error(f"I/O error { err.errno } '{ err.strerror }'")


def read_yaml(file_name: str) -> dict:
"""Read yaml file.

Args:
filename (str): name of file

Returns:
dict: data from yaml file
"""
try:
logging.info(f"Read data from { file_name }")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say this is more of a debug level message than info.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to .debug().

with open(file_name, encoding="UTF-8") as f:
yaml_data = yaml.load(f, Loader=FullLoader)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use SafeLoader instead of FullLoader. FullLoader has known vulnerabilities, see yaml/pyyaml#420 for examples.

Only use FullLoader if absolutely trust the data and SafeLoader is not working with your data structure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright! Thank you :)

except IOError as err:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Catch OSError instead. IOError was aliased to OSError starting wtih Python3.3.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, didn't know that tbh.

logging.error(f"Yaml data file { file_name } could not be opened!")
logging.error(f"I/O error { err.errno } '{ err.strerror }'")
sys.exit(1)

return yaml_data


def main() -> None:
"""Connect to devices and save the running config to files as backup."""
logging.basicConfig(level=logging.INFO)
devices_info = read_yaml("hosts.yaml")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of frameworks in the networking automation refer to structures like that as inventory so it's good to follow that convention. Naming variables is hard :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it to inventory. And yes, naming variables is a tough business 😨


for device, device_info in devices_info["devices"].items():
device_info["username"] = USERNAME
device_info["password"] = PASSWORD
device_output = send_command(
device=device_info, command=COMMANDS[device_info["device_type"]]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.get() is usually safer in situations like this. This also allows you to provide the default value. E.g.

... command=COMMANDS.get(device_info["device_type"], "show running-config")

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree, changed to .get().

)
write_file(file_name=f"{BACKUP_DIR}/{device}.cfg", text=device_output)


if __name__ == "__main__":
main()