Skip to content

Commit

Permalink
Release 1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
akamura committed Feb 18, 2024
1 parent 961b34f commit bbf8170
Show file tree
Hide file tree
Showing 34 changed files with 3,052 additions and 7 deletions.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#**************************************************************************
# App: Cisco Meraki CLU *
# Version: 1.3 *
# Author: Matia Zanella *
# Description: Cisco Meraki CLU (Command Line Utility) is an essential *
# tool crafted for Network Administrators managing Meraki *
# Github: https://github.com/akamura/cisco-meraki-clu/ *
# *
# Icon Author: Cisco Systems, Inc. *
# Icon Author URL: https://meraki.cisco.com/ *
# *
# Copyright (C) 2024 Matia Zanella *
# https://www.matiazanella.com *
# *
# This program is free software; you can redistribute it and/or modify *
# it under the terms of the GNU General Public License as published by *
# the Free Software Foundation; either version 2 of the License, or *
# (at your option) any later version. *
# *
# This program is distributed in the hope that it will be useful, *
# but WITHOUT ANY WARRANTY; without even the implied warranty of *
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# GNU General Public License for more details. *
# *
# You should have received a copy of the GNU General Public License *
# along with this program; if not, write to the *
# Free Software Foundation, Inc., *
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
#**************************************************************************


# ==================================================
# IMPORT various libraries and modules
# ==================================================
import os
from pysqlcipher3 import dbapi2 as sqlite
from termcolor import colored


# ==================================================
# SAVE the Cisco Meraki API Key in the Encrypted DB
# ==================================================
def save_api_key(db_password, api_key):
try:
db_path = os.path.join('/opt/akamura/ciscomerakiclu/db', 'cisco_meraki_clu_db.db')
conn = sqlite.connect(db_path)
conn.execute(f"PRAGMA key = '{db_password}'")
conn.execute("INSERT OR REPLACE INTO sensitive_data (id, data) VALUES (1, ?)", (api_key,))
conn.commit()
print("API key saved successfully.")
except Exception as e:
print(f"An error occurred: {e}")
finally:
conn.close()

def get_api_key(db_password):
conn = None # Initialize connection to None
try:
db_path = os.path.join('/opt/akamura/ciscomerakiclu/db', 'cisco_meraki_clu_db.db')
conn = sqlite.connect(db_path)
conn.execute(f"PRAGMA key = '{db_password}'")

cursor = conn.execute("SELECT data FROM sensitive_data WHERE id = 1")
api_key = cursor.fetchone()

return api_key[0] if api_key else None

except Exception as e:
print(colored("An error occurred while accessing the database: ", "red") + str(e))
return None

finally:
if conn:
conn.close()
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
191 changes: 191 additions & 0 deletions Source code/LINUX_MACOS/opt/akamura/ciscomerakiclu/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
#**************************************************************************
# App: Cisco Meraki CLU *
# Version: 1.3 *
# Author: Matia Zanella *
# Description: Cisco Meraki CLU (Command Line Utility) is an essential *
# tool crafted for Network Administrators managing Meraki *
# Github: https://github.com/akamura/cisco-meraki-clu/ *
# *
# Icon Author: Cisco Systems, Inc. *
# Icon Author URL: https://meraki.cisco.com/ *
# *
# Copyright (C) 2024 Matia Zanella *
# https://www.matiazanella.com *
# *
# This program is free software; you can redistribute it and/or modify *
# it under the terms of the GNU General Public License as published by *
# the Free Software Foundation; either version 2 of the License, or *
# (at your option) any later version. *
# *
# This program is distributed in the hope that it will be useful, *
# but WITHOUT ANY WARRANTY; without even the implied warranty of *
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
# GNU General Public License for more details. *
# *
# You should have received a copy of the GNU General Public License *
# along with this program; if not, write to the *
# Free Software Foundation, Inc., *
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
#**************************************************************************


# ==================================================
# IMPORT various libraries and modules
# ==================================================
import os
import sys
import logging
import traceback

required_packages = {
"tabulate": "tabulate",
"pathlib": "pathlib",
"datetime": "datetime",
"termcolor": "termcolor",
"requests": "requests",
"rich": "rich",
"setuptools": "setuptools",
"pysqlcipher3": "pysqlcipher3"
}

missing_packages = []
for module, package in required_packages.items():
try:
__import__(module)
except ImportError:
missing_packages.append(package)

if missing_packages:
print("Missing required Python packages: " + ", ".join(missing_packages))
print("Please install them using the following command:")
print(f"{sys.executable} -m pip install " + " ".join(missing_packages))
sys.exit(1)

from getpass import getpass
from datetime import datetime
from termcolor import colored
from pysqlcipher3 import dbapi2 as sqlite


# ==================================================
# IMPORT custom modules
# ==================================================
from api import meraki_api_manager
from settings import term_extra
from settings import db_creator
from utilities import submenu


# ==================================================
# ERROR handling and logging
# ==================================================
logger = logging.getLogger('ciscomerakiclu')
logger.setLevel(logging.ERROR)

log_directory = '/opt/akamura/ciscomerakiclu/log/'
if not os.path.exists(log_directory):
os.makedirs(log_directory)

log_file = os.path.join(log_directory, 'error.log')
file_handler = logging.FileHandler(log_file)
file_handler.setLevel(logging.ERROR)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)


# ==================================================
# VISUALIZE the Main Menu
# ==================================================
def main_menu(db_password):
while True:
term_extra.clear_screen()
term_extra.print_ascii_art()

api_key = meraki_api_manager.get_api_key(db_password)
options = [
"Network wide [under dev]",
"Security & SD-WAN",
"Switch and wireless",
"Environmental [under dev]",
"Organization [under dev]",
"The Swiss Army Knife [under dev]",
f"{'Edit Cisco Meraki API Key' if api_key else 'Set Cisco Meraki API Key'}",
"Exit the Command Line Utility"
]
current_year = datetime.now().year
footer = f"\033[1mPROJECT PAGE\033[0m\n© {current_year} Matia Zanella\nhttps://developer.cisco.com/codeexchange/github/repo/akamura/cisco-meraki-clu/\n\n\033[1mSUPPORT ME\033[0m\n☕️ Fuel me with a coffee if you found it useful https://www.paypal.com/paypalme/matiazanella/\n\n\033[1mDISCLAIMER\033[0m\nThis utility is not an official Cisco Meraki product but is based on the official Cisco Meraki API.\nIt is intended to provide Network Administrators with an easy daily companion in the swiss army knife."

# Description header over the menu
print("\n")
print("┌" + "─" * 58 + "┐")
print("│".ljust(59) + "│")
for index, option in enumerate(options, start=1):
print(f"│ {index}. {option}".ljust(59) + "│")
print("│".ljust(59) + "│")
print("└" + "─" * 58 + "┘")

term_extra.print_footer(footer)
choice = input(colored("Choose a menu option [1-8]: ", "cyan"))

if choice.isdigit() and 1 <= int(choice) <= 8:
if choice == '2':
if api_key:
submenu.submenu_mx(api_key)
else:
print("Please set the Cisco Meraki API key first.")
input(colored("\nPress Enter to return to the main menu...", "green"))

elif choice == '3':
if api_key:
submenu.submenu_sw_and_ap(api_key)
else:
print("Please set the Cisco Meraki API key first.")
input(colored("\nPress Enter to return to the main menu...", "green"))

elif choice == '7':
manage_api_key(db_password)
elif choice == '8':
term_extra.clear_screen()
term_extra.print_ascii_art()

print("\nThank you for using the Cisco Meraki Command Line Utility!")
print("Exiting the program. Goodbye, and have a wonderful day!")
print("\n🚀 \033[1mCONTRIBUTE\033[0m\nThis is not just a project; it's a community effort.\nI'm inviting you to be a part of this journey.\nStar it, fork it, contribute, or just play around with it.\nEvery feedback, issue, or pull request is an opportunity for us to make this tool even more amazing.\nYou are more than welcome to discuss it on GitHub https://github.com/akamura/cisco-meraki-clu/discussions")
print("\n" * 2)

break
else:
print(colored(f"You selected: {options[int(choice) - 1]}", "green"))
else:
print(colored("Invalid choice. Please try again.", "red"))


def manage_api_key(db_password):
term_extra.clear_screen()
api_key = input("\nEnter the Cisco Meraki API Key: ")
meraki_api_manager.save_api_key(db_password, api_key)


# ==================================================
# ERROR handling and logging
# ==================================================
if __name__ == "__main__":
try:
db_password = ""
if not db_creator.database_exists():
os.system('clear')
term_extra.print_ascii_art()
db_password = db_creator.prompt_create_database()
else:
os.system('clear')
term_extra.print_ascii_art()
db_password = getpass(colored("\n\nWelcome to Cisco Meraki Command Line Utility!\nThis program contains sensitive information. Please insert your password to continue: ", "green"))
db_creator.verify_database_password(db_password)
main_menu(db_password)
except Exception as e:
logger.error("An error occurred", exc_info=True)
print("An error occurred:")
print(e)
traceback.print_exc()
input("\nPress Enter to exit.\n")
Empty file.
Loading

0 comments on commit bbf8170

Please sign in to comment.