Browse Source

Merge pull request #5 from jzaia18/master

Add creation functionality for SSH Config management
pull/8/head
Jeffery Russell 6 years ago
committed by GitHub
parent
commit
a4f8b3f2d5
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 0 deletions
  1. +70
    -0
      src/config_manager.py
  2. +7
    -0
      src/module.py

+ 70
- 0
src/config_manager.py View File

@ -0,0 +1,70 @@
"""
Jake Zaia
2018-10-12
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
#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: ")
#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))
if port:
f.write("\tPort {0}\n".format(port))
if identityfile:
f.write("\tIdentityFile {0}\n".format(identityfile))
f.close()
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')) f.append(line.strip(' \t\n\r'))
return f 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): def append_file(file_name, append):
""" """

Loading…
Cancel
Save