From 0ab918c1d681fdeb5b7023d595352c7e81ab9984 Mon Sep 17 00:00:00 2001 From: pww217 Date: Sat, 15 Jul 2023 10:48:48 -0500 Subject: [PATCH] cleanup --- Dockerfile | 11 ++++++++--- main.py | 48 ++++++------------------------------------------ requirements.txt | 4 ++-- 3 files changed, 16 insertions(+), 47 deletions(-) diff --git a/Dockerfile b/Dockerfile index d694ddb..4f5ba0a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,8 +1,13 @@ -FROM ubuntu:20.04 +FROM ubuntu:22.04 + WORKDIR /app + COPY . . + ENV TZ=America/Chicago + RUN apt-get update && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \ -&& apt-get install -y python3 python3-pip openssh-client -RUN pip3 install -r requirements.txt +&& apt-get install -y python3 python3-pip openssh-client \ +&& pip3 install -r requirements.txt + CMD python3 main.py diff --git a/main.py b/main.py index 8df648f..edda9d3 100755 --- a/main.py +++ b/main.py @@ -1,48 +1,12 @@ from flask import Flask, request, render_template, redirect import logging.config import logging -from netmiko import ConnectHandler, ssh_exception +from netmiko import ConnectHandler, NetmikoAuthenticationException, NetmikoTimeoutException import os """ Flask-based Netmiko application for Cisco IOS and Linux -Peter W -Github.com/pww217 - -To do: (* means done) - -1. Cleaner landing page * -2. Better exception handling and logging * -3. File cleanup * -4. Hide password on browser * -5. Revamp index with radio, checkbox, redirectors * -6. Implement Cisco pages * -7. Add show host file page * -8. Implement logging * -9. Spruce up UI -10. More debugging * -11. Dark Mode?!?! -12. User/Password/Command exceptions* -13. Specific error at index page for missing fields* -14. Add show log to index page* - - -Reach goals: - -1. Implement real database -2. Allow for multiple concurrent users (hosts.txt would need to separate sessions) -3. Encrypt password in memory or db -4. Build into docker container * -5. Implemented automated testing -6. HTTPS -7. SSH key support - branch key_support -8. NX-OS Support -9. Github Actions pipeline for app* -10. DockerHub Deployment* -11. Pre-deployment testing -12. Cache Docker layers for faster deploy -13. Pipeline for infrastructure* (terratest) - +https://github.com/pww217 """ # ~~~ Global Variables @@ -73,7 +37,7 @@ 'backupCount': 5, }}, 'root': { - 'level': 'INFO', + 'level': 'DEBUG', 'handlers': ['wsgi', 'file'] } }) @@ -159,17 +123,17 @@ def run_command(hosts, os_type, user, password, command, enable_mode='None'): if enable_mode == 'on': # Check for enable mode and activate connection.enable() reply = f'

{device} returned the following output:

'+ \ - connection.send_command(command) # Runs a single command + connection.send_command(command, expect_string="[\\ ~\\#]") # Runs a single command write_output_file(reply) logging.info(f'Wrote {device} output to file') connection.disconnect() # Graceful SSH disconnect # Common exceptions and a catch-all - except ssh_exception.NetmikoTimeoutException: + except NetmikoTimeoutException: message = f'

{device} could not be reached on port 22 - ' \ 'skipping to next device.

' logging.warning(message.replace('', '').replace('

', '')) write_output_file(message) - except ssh_exception.NetmikoAuthenticationException: + except NetmikoAuthenticationException: message = f'

{device} had invalid credentials

' logging.warning(message.replace('', '').replace('

', '')) write_output_file(message) diff --git a/requirements.txt b/requirements.txt index b521947..a0ceae7 100755 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -flask -netmiko \ No newline at end of file +flask==2.3.2 +netmiko==4.2.0 \ No newline at end of file