diff --git a/src/configuration.py b/src/configuration.py index f22ab30..c2d6d36 100644 --- a/src/configuration.py +++ b/src/configuration.py @@ -7,11 +7,12 @@ Deals with the configuration file for bash manager Config file: servers: /path/to/servers quotes: /path/to/quotes.txt +forward: /path/to/forwards.txt mounts: /path/to/ssh/mounts.txt config dictionary -{servers: "/", quotes: "/", mounts:"/"} +{servers: "/", quotes: "/", forwards: "/", mounts:"/"} """ import os.path @@ -57,6 +58,7 @@ def create_config(): f = open(CONFIG_FILE, "w") f.write(single_conf_input("servers") + '\n') f.write(single_conf_input("quotes") + '\n') + f.write(single_conf_input("forwards")+ '\n') f.write(single_conf_input("mounts") + '\n') f.close() @@ -83,6 +85,12 @@ def read_config(): print("Error reading quotes file from config") return config["quotes"] = temp[1] + + if line.find("forwards:") != -1: + if len(temp) <= 1: + print("Error reading forwards file from config") + return + config["forwards"] = temp[1] if line.find("mounts:") != -1: if len(temp) <= 1: print("Error reading mounts file from config") @@ -95,7 +103,7 @@ def valid_config(config): """ Checks to see if a configuration is valid """ - return 'servers' in config and 'quotes' in config and 'mounts' in config + return 'servers' in config and 'quotes' in config and 'forwards' in config and 'mounts' in config def create_config_dependent_files(config): @@ -108,6 +116,9 @@ def create_config_dependent_files(config): if os.path.isfile(config["quotes"]) == False: print("Creating missing quotes file in " + config["quotes"]) module.create_empty_file(config["quotes"]) + if os.path.isfile(config["forwards"]) == False: + print("Creating missing forwards file in " + config["forwards"]) + module.create_empty_file(config["forwards"]) if os.path.isfile(config["mounts"]) == False: print("Creating missing mounts file in " + config["mounts"]) module.create_empty_file(config["mounts"]) diff --git a/src/ssh_manager.py b/src/ssh_manager.py index cfec005..d4e1d57 100644 --- a/src/ssh_manager.py +++ b/src/ssh_manager.py @@ -9,9 +9,10 @@ import collections from utils import module import configuration import mount_ssh_drive +import os INPUT_FILE = configuration.get_config()["servers"] - +FORWARD_FILE = configuration.get_config()["forwards"] Computer = collections.namedtuple("Computer", ('host', 'menu_id')) WELCOME_MESSAGE = "**************************************" @@ -40,6 +41,7 @@ def main(): menu.append("B) Manager tools") menu.append("C) Socks Tunnel") menu.append("D) SSH Drive Manager") + menu.append("E) Local Port Forwarding") module.print_menu("SSH manager V 1.0", menu) @@ -53,6 +55,8 @@ def main(): socks_ssh_tunnel() elif "d" == str.lower(i): mount_ssh_drive.main() + elif "e" == str.lower(i): + port_forwarding_sub_menu() else: for c in cmp: if int(i) == c.menu_id: @@ -204,6 +208,104 @@ def remove_host(): if c.menu_id == int(host): module.remove_line_from_file(INPUT_FILE, c.host) +def add_forward_config(): + """ + appends new local forward configurations to forwards.txt + :return: None + """ + forwardingConfig = input("Enter forwarding config to add or -1 to exit:") + if forwardingConfig != '-1': + module.append_file(FORWARD_FILE, forwardingConfig) + + +def del_forward_config(): + """ + removes a local forward configurations from forwards.txt + :return: None + """ + # check if file is empty before trying to open to read file + if (os.stat(FORWARD_FILE).st_size == 0): + print(print_red("*" * len(WELCOME_MESSAGE))) + print("No forward configurations are available for removal.") + print(print_red("*" * len(WELCOME_MESSAGE))) + + else: + f = open(FORWARD_FILE, "r") + i = 1 + savedConfigs = [] + print(print_red("*" * len(WELCOME_MESSAGE))) + print("Current saved local port forwarding configurations:") + for line in f: + print(print_red("*") + "%s) %s" % (i, line) + print_red("*")) + savedConfigs.append(line.strip()) + i += 1 + print(print_red("*" * len(WELCOME_MESSAGE))) + selection = input("Select a configuration to delete: ") + module.remove_line_from_file(FORWARD_FILE, savedConfigs[int(selection)-1]) + +def select_forward_config(): + """ + Select a local forwarding configuration from forwards.txt + to execute using SSH + :return: None + """ + # check if file is empty before trying to open to read file + if (os.stat(FORWARD_FILE).st_size == 0): + print(print_red("*" * len(WELCOME_MESSAGE))) + print("No forward configurations are available to run.") + print(print_red("*" * len(WELCOME_MESSAGE))) + + else: + f = open(FORWARD_FILE, "r") + i = 1 + savedConfigs = [] + print(print_red("*" * len(WELCOME_MESSAGE))) + print("Current saved local port forwarding configurations:") + for line in f: + print(print_red("*") + "%s) %s" % (i, line) + print_red("*")) + savedConfigs.append(line.strip()) + i += 1 + print(print_red("*" * len(WELCOME_MESSAGE))) + selection = input("Select a configuration to execute: ") + + #Split the selected entry based on a space so we can feed each + #element into the subprocess call + forwardConfig = savedConfigs[int(selection)-1].split(" ") + subprocess.call(["ssh", "-tt", "-L", forwardConfig[0], forwardConfig[1]]) + exit_program() + +def print_forwarding_sub_menu(): + """ + prints out a sub help menu for other forwarding options + :return: None + """ + module.print_menu("Options", ["1) Add local forward configuration", + "2) Remove local forward configuration", + "3) Select local forward configuration", + "4) Exit"]) + +def port_forwarding_sub_menu(): + """ + + print out a sub menu related to port forwarding + options + :return: None + """ + print_forwarding_sub_menu() + + i = input("Enter selection:") + if i != "" and (int(i) in {1, 2, 3, 4}): + options = {1: add_forward_config, + 2: del_forward_config, + 3: select_forward_config, + 4: exit_program + } + options[int(i)]() + else: + print("Invalid selection!") + + port_forwarding_sub_menu() + """ Makes sure that other programs don't execute the main