Remove classes from easy modules

* bashrc
* hacker
* helloworld
* installer
* update
* teamspeak
This commit is contained in:
Marcel Schwarz 2020-08-02 00:55:48 +02:00
parent d155d0ada9
commit e633965a1a
14 changed files with 185 additions and 149 deletions

28
main.py
View File

@ -1,25 +1,25 @@
import fire import fire
from modules.helloworld.HelloWorld import HelloWorld import modules.bashrc as bashrc
from modules.install.ToolsInstaller import ToolsInstaller 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.swap.SwapModule import SwapModule
from modules.systemupdate.Systemupdate import Systemupdate
from modules.vim.VimModule import VimModule 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 = { modules = {
"helloworld": HelloWorld().run, "bashrc": bashrc.functions,
"install": ToolsInstaller, "fail2ban": fail2ban.functions,
"hacker": hacker.functions,
"helloworld": helloworld.functions,
"install": install.functions,
"swap": SwapModule().run, "swap": SwapModule().run,
"update": Systemupdate().run, "update": update.functions,
"teamspeak": teamspeak.functions,
"vim": VimModule().run, "vim": VimModule().run,
"teamspeak": Teamspeak,
"hacker": Hacker,
"fail2ban": fail2ban.run,
"bashrc": BashrcModule().run
} }
fire.Fire(modules) fire.Fire(modules)

View File

@ -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!")

View File

@ -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,
}

View File

@ -13,3 +13,8 @@ def run():
subprocess.call("ufw allow ssh && ufw enable", shell=True) subprocess.call("ufw allow ssh && ufw enable", shell=True)
copyfile("./modules/fail2ban/jail.local", "/etc/fail2ban/jail.local") copyfile("./modules/fail2ban/jail.local", "/etc/fail2ban/jail.local")
functions = {
"run": run
}

View File

@ -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")

View File

@ -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
}

View File

@ -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)

View File

@ -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

View File

@ -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)

View File

@ -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
}

View File

@ -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.")

View File

@ -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
}

View File

@ -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__))

View File

@ -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
}