From e633965a1a9e1de5ed502497ddbca37e311f64b7 Mon Sep 17 00:00:00 2001 From: Marcel Schwarz Date: Sun, 2 Aug 2020 00:55:48 +0200 Subject: [PATCH] Remove classes from easy modules * bashrc * hacker * helloworld * installer * update * teamspeak --- main.py | 28 ++++++------- modules/bashrc/BashrcModule.py | 26 ------------ modules/bashrc/__init__.py | 31 ++++++++++++++ .../{Fail2BanModule.py => __init__.py} | 5 +++ modules/hacker/Hacker.py | 29 -------------- modules/hacker/__init__.py | 34 ++++++++++++++++ modules/helloworld/HelloWorld.py | 13 ------ modules/helloworld/__init__.py | 16 ++++++++ modules/install/ToolsInstaller.py | 22 ---------- modules/install/__init__.py | 29 ++++++++++++++ modules/systemupdate/Systemupdate.py | 13 ------ modules/systemupdate/__init__.py | 16 ++++++++ modules/teamspeak/Teamspeak.py | 32 --------------- modules/teamspeak/__init__.py | 40 +++++++++++++++++++ 14 files changed, 185 insertions(+), 149 deletions(-) delete mode 100644 modules/bashrc/BashrcModule.py create mode 100644 modules/bashrc/__init__.py rename modules/fail2ban/{Fail2BanModule.py => __init__.py} (94%) delete mode 100644 modules/hacker/Hacker.py create mode 100644 modules/hacker/__init__.py delete mode 100644 modules/helloworld/HelloWorld.py delete mode 100644 modules/install/ToolsInstaller.py delete mode 100644 modules/systemupdate/Systemupdate.py delete mode 100644 modules/teamspeak/Teamspeak.py create mode 100644 modules/teamspeak/__init__.py diff --git a/main.py b/main.py index aedb96f..41c80e8 100644 --- a/main.py +++ b/main.py @@ -1,25 +1,25 @@ import fire -from modules.helloworld.HelloWorld import HelloWorld -from modules.install.ToolsInstaller import ToolsInstaller +import modules.bashrc as bashrc +import modules.fail2ban as fail2ban +import modules.hacker as hacker +import modules.helloworld as helloworld +import modules.install as install +import modules.systemupdate as update +import modules.teamspeak as teamspeak from modules.swap.SwapModule import SwapModule -from modules.systemupdate.Systemupdate import Systemupdate from modules.vim.VimModule import VimModule -from modules.teamspeak.Teamspeak import Teamspeak -from modules.hacker.Hacker import Hacker -import modules.fail2ban.Fail2BanModule as fail2ban -from modules.bashrc.BashrcModule import BashrcModule modules = { - "helloworld": HelloWorld().run, - "install": ToolsInstaller, + "bashrc": bashrc.functions, + "fail2ban": fail2ban.functions, + "hacker": hacker.functions, + "helloworld": helloworld.functions, + "install": install.functions, "swap": SwapModule().run, - "update": Systemupdate().run, + "update": update.functions, + "teamspeak": teamspeak.functions, "vim": VimModule().run, - "teamspeak": Teamspeak, - "hacker": Hacker, - "fail2ban": fail2ban.run, - "bashrc": BashrcModule().run } fire.Fire(modules) diff --git a/modules/bashrc/BashrcModule.py b/modules/bashrc/BashrcModule.py deleted file mode 100644 index 7f77174..0000000 --- a/modules/bashrc/BashrcModule.py +++ /dev/null @@ -1,26 +0,0 @@ -from PyInquirer import prompt -import os -from shutil import copyfile -from print_helpers import print_gr, print_fail - - -class BashrcModule: - - def run(self): - bashrc_path = os.path.join(os.path.expanduser('~'), ".bashrc") - bashrc_exists = os.path.exists(bashrc_path) - - if bashrc_exists: - print_gr(f"Found .bashrc in {bashrc_path}") - ask_to_keep = { - 'type': 'confirm', - 'message': 'Do you want to overwrite the existing .bashrc?', - 'name': 'overwrite', - 'default': False, - } - if not prompt(ask_to_keep)['overwrite']: - print_fail("Stopping") - return - - copyfile("./modules/bashrc/bashrc_default", bashrc_path) - print_gr("Successfully overwritten .bashrc!") if bashrc_exists else print_gr(f"Successfully created .bashrc!") diff --git a/modules/bashrc/__init__.py b/modules/bashrc/__init__.py new file mode 100644 index 0000000..98297b2 --- /dev/null +++ b/modules/bashrc/__init__.py @@ -0,0 +1,31 @@ +import os +from shutil import copyfile + +from PyInquirer import prompt + +from print_helpers import print_gr, print_fail + + +def run(): + bashrc_path = os.path.join(os.path.expanduser('~'), ".bashrc") + bashrc_exists = os.path.exists(bashrc_path) + + if bashrc_exists: + print_gr(f"Found .bashrc in {bashrc_path}") + ask_to_keep = { + 'type': 'confirm', + 'message': 'Do you want to overwrite the existing .bashrc?', + 'name': 'overwrite', + 'default': False, + } + if not prompt(ask_to_keep)['overwrite']: + print_fail("Stopping") + return + + copyfile("./modules/bashrc/bashrc_default", bashrc_path) + print_gr("Successfully overwritten .bashrc!") if bashrc_exists else print_gr(f"Successfully created .bashrc!") + + +functions = { + "run": run, +} diff --git a/modules/fail2ban/Fail2BanModule.py b/modules/fail2ban/__init__.py similarity index 94% rename from modules/fail2ban/Fail2BanModule.py rename to modules/fail2ban/__init__.py index 6520387..8f1ff35 100644 --- a/modules/fail2ban/Fail2BanModule.py +++ b/modules/fail2ban/__init__.py @@ -13,3 +13,8 @@ def run(): subprocess.call("ufw allow ssh && ufw enable", shell=True) copyfile("./modules/fail2ban/jail.local", "/etc/fail2ban/jail.local") + + +functions = { + "run": run +} diff --git a/modules/hacker/Hacker.py b/modules/hacker/Hacker.py deleted file mode 100644 index 17b83a5..0000000 --- a/modules/hacker/Hacker.py +++ /dev/null @@ -1,29 +0,0 @@ -import subprocess -from os import path -import os -import random - - -class Hacker: - - def run_bash(self): - try: - subprocess.run("bash hacker.sh", shell=True, cwd=path.dirname(__file__)) - except KeyboardInterrupt: - pass - - def run_py(self): - columns = int(os.popen('stty size', 'r').read().split()[1]) # length of current line - try: - while True: - num_to_show = random.randint(0, 1) - count = random.randint(3, columns // 20) - hacker = " " * columns - for i in range(count): - pos = random.randint(0, columns) - hacker = hacker[:pos] + str(num_to_show) + hacker[pos+1:] - print("\033[92m" + hacker) - except KeyboardInterrupt: - print(columns) - print("Finished") - print("\033[0m") diff --git a/modules/hacker/__init__.py b/modules/hacker/__init__.py new file mode 100644 index 0000000..cdcf758 --- /dev/null +++ b/modules/hacker/__init__.py @@ -0,0 +1,34 @@ +import os +import random +import subprocess +from os import path + + +def run_bash(): + try: + subprocess.run("bash hacker.sh", shell=True, cwd=path.dirname(__file__)) + except KeyboardInterrupt: + pass + + +def run_py(): + columns = int(os.popen('stty size', 'r').read().split()[1]) # length of current line + try: + while True: + num_to_show = random.randint(0, 1) + count = random.randint(3, columns // 20) + hacker = " " * columns + for i in range(count): + pos = random.randint(0, columns) + hacker = hacker[:pos] + str(num_to_show) + hacker[pos + 1:] + print("\033[92m" + hacker) + except KeyboardInterrupt: + print(columns) + print("Finished") + print("\033[0m") + + +functions = { + "runBash": run_bash, + "runPy": run_py +} diff --git a/modules/helloworld/HelloWorld.py b/modules/helloworld/HelloWorld.py deleted file mode 100644 index 50975bb..0000000 --- a/modules/helloworld/HelloWorld.py +++ /dev/null @@ -1,13 +0,0 @@ -import subprocess - - -class HelloWorld: - """docstring for SampleModule.""" - - def run(self): - print("This shows that your installation is working correctly.") - subprocess.call("screenfetch", shell=True) - print("You can now start using the tool.") - output = subprocess.check_output("cat /etc/*-release", shell=True).decode("UTF-8") - with open("your_system_information.txt", "w") as file: - file.write(output) diff --git a/modules/helloworld/__init__.py b/modules/helloworld/__init__.py index e69de29..899a56f 100644 --- a/modules/helloworld/__init__.py +++ b/modules/helloworld/__init__.py @@ -0,0 +1,16 @@ +"""Docstring""" +import subprocess + +from print_helpers import print_gr + + +def helloworld(): + print_gr("This shows that your installation is working correctly.") + subprocess.call("screenfetch", shell=True) + print_gr("You can now start using the tool.") + output = subprocess.check_output("cat /etc/*-release", shell=True).decode("UTF-8") + with open("your_system_information.txt", "w") as file: + file.write(output) + + +functions = helloworld diff --git a/modules/install/ToolsInstaller.py b/modules/install/ToolsInstaller.py deleted file mode 100644 index e6c64f9..0000000 --- a/modules/install/ToolsInstaller.py +++ /dev/null @@ -1,22 +0,0 @@ -import subprocess - - -class ToolsInstaller: - - def basic_tools(self): - """Install the most basic tools!""" - print("Try to install basic Command-line tools") - subprocess.run("apt-get install -y bleachbit nano xrdp htop bash-completion dialog powertop tree wget", - shell=True) - print("Script ran to completion.") - - def hard_drive_tools(self): - """Install tools to look up smart information from your hard-drives""" - subprocess.run("apt-get install -y smartmontools gsmartcontrol", shell=True) - - def docker(self): - """Install the Docker service on the machine""" - subprocess.run("curl -fsSL https://get.docker.com | bash", shell=True) - subprocess.run("curl -L \"https://github.com/docker/compose/releases/download/" - "1.25.4/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose" - "&& chmod +x /usr/local/bin/docker-compose", shell=True) diff --git a/modules/install/__init__.py b/modules/install/__init__.py index e69de29..344a72b 100644 --- a/modules/install/__init__.py +++ b/modules/install/__init__.py @@ -0,0 +1,29 @@ +import subprocess + + +def basic_tools(): + """Install the most basic tools!""" + print("Try to install basic Command-line tools") + subprocess.run("apt-get install -y bleachbit nano xrdp htop bash-completion dialog powertop tree wget", + shell=True) + print("Script ran to completion.") + + +def hard_drive_tools(): + """Install tools to look up smart information from your hard-drives""" + subprocess.run("apt-get install -y smartmontools gsmartcontrol", shell=True) + + +def docker(): + """Install the Docker service on the machine""" + subprocess.run("curl -fsSL https://get.docker.com | bash", shell=True) + subprocess.run("curl -L \"https://github.com/docker/compose/releases/download/" + "1.25.4/docker-compose-$(uname -s)-$(uname -m)\" -o /usr/local/bin/docker-compose" + "&& chmod +x /usr/local/bin/docker-compose", shell=True) + + +functions = { + "basictools": basic_tools, + "harddrive": hard_drive_tools, + "docker": docker +} diff --git a/modules/systemupdate/Systemupdate.py b/modules/systemupdate/Systemupdate.py deleted file mode 100644 index fe8a81d..0000000 --- a/modules/systemupdate/Systemupdate.py +++ /dev/null @@ -1,13 +0,0 @@ -import subprocess - - -class Systemupdate: - - def run(self): - print("Running update") - subprocess.call(["apt", "update", "-y"]) - subprocess.call(["apt", "upgrade", "-y"]) - subprocess.call(["apt", "dist-upgrade", "-y"]) - subprocess.call(["apt", "autoremove", "-y"]) - subprocess.call(["apt", "autoclean", "-y"]) - print("All update processes finished, please check output for further details.") diff --git a/modules/systemupdate/__init__.py b/modules/systemupdate/__init__.py index e69de29..896bada 100644 --- a/modules/systemupdate/__init__.py +++ b/modules/systemupdate/__init__.py @@ -0,0 +1,16 @@ +import subprocess + + +def run(): + print("Running update") + subprocess.call(["apt", "update", "-y"]) + subprocess.call(["apt", "upgrade", "-y"]) + subprocess.call(["apt", "dist-upgrade", "-y"]) + subprocess.call(["apt", "autoremove", "-y"]) + subprocess.call(["apt", "autoclean", "-y"]) + print("All update processes finished, please check output for further details.") + + +functions = { + "run": run +} diff --git a/modules/teamspeak/Teamspeak.py b/modules/teamspeak/Teamspeak.py deleted file mode 100644 index 294bb2c..0000000 --- a/modules/teamspeak/Teamspeak.py +++ /dev/null @@ -1,32 +0,0 @@ -import subprocess -from os import path -import time -import re - - -class Teamspeak: - """Manage a Docker backed Teamspeak server""" - - def start(self): - """Starts a Teamspeak Server with docker-compose""" - show_creds = False if path.exists(path.join(path.dirname(__file__), "ts3_data")) else True - subprocess.run("docker-compose up -d", shell=True, cwd=path.dirname(__file__)) - if show_creds: - logs = subprocess.check_output("docker-compose logs", shell=True, cwd=path.dirname(__file__)).decode("UTF-8") - while re.search(r"token", logs) is None: - time.sleep(2) - logs = subprocess.check_output("docker-compose logs", shell=True, cwd=path.dirname(__file__)).decode("UTF-8") - print("Server Query Admin Account: " + re.search(r"loginname=.*", logs).group(0)) - print("Server Admin Token: " + re.search(r"token=(.*)", logs).group(1)) - - def stop(self): - """Stops the Teamspeak Server with docker-compose""" - subprocess.run("docker-compose down -v", shell=True, cwd=path.dirname(__file__)) - - def pack_data(self, name="ts3_data.tar.gz"): - """Pack all user data of the server started with this script as tar file""" - subprocess.run(f"tar -zcvf {name} ts3_data", shell=True, cwd=path.dirname(__file__)) - - def show_logs(self): - """show the logs of the running Teamspeak server""" - subprocess.run("docker-compose logs", shell=True, cwd=path.dirname(__file__)) diff --git a/modules/teamspeak/__init__.py b/modules/teamspeak/__init__.py new file mode 100644 index 0000000..91296f9 --- /dev/null +++ b/modules/teamspeak/__init__.py @@ -0,0 +1,40 @@ +import re +import subprocess +import time +from os import path + + +def start(): + """Starts a Teamspeak Server with docker-compose""" + show_creds = False if path.exists(path.join(path.dirname(__file__), "ts3_data")) else True + subprocess.run("docker-compose up -d", shell=True, cwd=path.dirname(__file__)) + if show_creds: + logs = subprocess.check_output("docker-compose logs", shell=True, cwd=path.dirname(__file__)).decode("UTF-8") + while re.search(r"token", logs) is None: + time.sleep(2) + logs = subprocess.check_output("docker-compose logs", shell=True, cwd=path.dirname(__file__)).decode("UTF-8") + print("Server Query Admin Account: " + re.search(r"loginname=.*", logs).group(0)) + print("Server Admin Token: " + re.search(r"token=(.*)", logs).group(1)) + + +def stop(): + """Stops the Teamspeak Server with docker-compose""" + subprocess.run("docker-compose down -v", shell=True, cwd=path.dirname(__file__)) + + +def pack_data(name="ts3_data.tar.gz"): + """Pack all user data of the server started with this script as tar file""" + subprocess.run(f"tar -zcvf {name} ts3_data", shell=True, cwd=path.dirname(__file__)) + + +def show_logs(): + """show the logs of the running Teamspeak server""" + subprocess.run("docker-compose logs", shell=True, cwd=path.dirname(__file__)) + + +functions = { + "start": start, + "stop": stop, + "pack": pack_data, + "logs": show_logs +}