Browse Source

Add more functions for usability, document code

pull/5/head
Jake Zaia 5 years ago
parent
commit
c81d7204fe
2 changed files with 55 additions and 5 deletions
  1. +48
    -5
      src/config_manager.py
  2. +7
    -0
      src/module.py

+ 48
- 5
src/config_manager.py View File

@ -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()
if __name__ == '__main__':
create_config()
print(get_config())

+ 7
- 0
src/module.py View File

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

Loading…
Cancel
Save