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 all 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/
110 changes: 110 additions & 0 deletions solutions/python/exercise1/backup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""Get backups of running config from devices."""
import sys
import logging
from pathlib import Path
import yaml
from yaml.loader import SafeLoader
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 create_parent_dir(dir_name: str) -> None:
"""Create parent directory for backup files.

Args:
dir_name (str): file name with parent dir
"""

try:
files_dir = Path(dir_name).absolute()
logging.info(f"Creating directory { dir_name } for backups if not exists")
files_dir.mkdir(parents=True, exist_ok=True)
except OSError as err:
logging.error(f"Could not create dir { dir_name }!")
logging.error(f"OS error { err.errno } '{ err.strerror }'")


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

Args:
file_name (str): file name and parent dir
text (str): text to be written
"""

try:
logging.info(f"Write output to file { file_name }")
with open(file_name, "w", encoding="UTF-8") as f:
f.write(text)
except OSError 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.debug(f"Read data from { file_name }")
with open(file_name, encoding="UTF-8") as f:
yaml_data = yaml.load(f, Loader=SafeLoader)
except OSError as err:
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)
create_parent_dir(dir_name=BACKUP_DIR)
inventory = read_yaml("hosts.yaml")

for device, device_info in inventory["devices"].items():
device_info["username"] = USERNAME
device_info["password"] = PASSWORD
device_output = send_command(
device=device_info,
command=COMMANDS.get(device_info["device_type"], "show running-config"),
)
write_file(file_name=f"{BACKUP_DIR}/{device}.cfg", text=device_output)


if __name__ == "__main__":
main()