From c81d7204fed95f13ef973a66517c81a62122b2b8 Mon Sep 17 00:00:00 2001 From: Jake Zaia Date: Fri, 12 Oct 2018 17:44:36 -0400 Subject: [PATCH] Add more functions for usability, document code --- src/config_manager.py | 53 +++++++++++++++++++++++++++++++++++++++---- src/module.py | 7 ++++++ 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/config_manager.py b/src/config_manager.py index 5abe86c..842d99b 100644 --- a/src/config_manager.py +++ b/src/config_manager.py @@ -1,21 +1,61 @@ -#This file manages SSH config files +""" +Jake Zaia +2018-10-12 -SSH_CONFIG_FILE_LOC = 'config' +Manages SSH config files, and supports a more user-friendly terminal based way of creating them +""" +import os + +#This is the location of where the SSH +SSH_CONFIG_FILE_LOC = '~/.ssh/config' + + +def config_exists(): + """ + Checks if an SSH config file exists at the desired path + """ + return os.path.exists(SSH_CONFIG_FILE_LOC) + +def get_config(): + """ + Returns the contents of the SSH config file as a string + """ + if not config_exists(): + return '' + f = open(SSH_CONFIG_FILE_LOC, 'r') + ret = f.read() + f.close() + return ret + +#TODO: check for duplicates, ensure no duplicate entries def create_config(): + """ + Creates an SSH config file with information taken from a user + """ + print("Please input the relevent information for generating your config file.") host = input("Nickname for Host: ") hostname = input("Hostname: ") user = input("Username: ") + + #Port is optional port = input("Would you like to specify a port?(y/N)") if (port == 'y' or port == 'Y' or port.lower() =='yes'): port = input("Port number: ") else: port = None - identityfile = input("Would you like to specify an ssh key location?(y/N)") + + #SSH public key location is optional + identityfile = input("Would you like to specify an ssh public key location?(y/N)") if (identityfile == 'y' or identityfile == 'Y' or identityfile.lower() =='yes'): identityfile = input("SSH Public key file location: ") - f = open(SSH_CONFIG_FILE_LOC, 'w') + + #Write info to config file + if config_exists(): + f = open(SSH_CONFIG_FILE_LOC, 'a') + else: + f = open(SSH_CONFIG_FILE_LOC, 'w') f.write("Host {0}\n".format(host)) f.write("\tHostName {0}\n".format(hostname)) f.write("\tUser {0}\n".format(user)) @@ -23,5 +63,8 @@ def create_config(): f.write("\tPort {0}\n".format(port)) if identityfile: f.write("\tIdentityFile {0}\n".format(identityfile)) + f.close() -create_config() \ No newline at end of file +if __name__ == '__main__': + create_config() + print(get_config()) diff --git a/src/module.py b/src/module.py index bb107c6..7bbfd1e 100644 --- a/src/module.py +++ b/src/module.py @@ -18,6 +18,13 @@ def input_file(file_name): f.append(line.strip(' \t\n\r')) return f +def check_file_exists(fileloc): + """ + Function which checks to see if a file exists + :return: whether file exists + """ + return os.path.exists(fileloc) + def append_file(file_name, append): """