|
@ -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(): |
|
|
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.") |
|
|
print("Please input the relevent information for generating your config file.") |
|
|
host = input("Nickname for Host: ") |
|
|
host = input("Nickname for Host: ") |
|
|
hostname = input("Hostname: ") |
|
|
hostname = input("Hostname: ") |
|
|
user = input("Username: ") |
|
|
user = input("Username: ") |
|
|
|
|
|
|
|
|
|
|
|
#Port is optional |
|
|
port = input("Would you like to specify a port?(y/N)") |
|
|
port = input("Would you like to specify a port?(y/N)") |
|
|
if (port == 'y' or port == 'Y' or port.lower() =='yes'): |
|
|
if (port == 'y' or port == 'Y' or port.lower() =='yes'): |
|
|
port = input("Port number: ") |
|
|
port = input("Port number: ") |
|
|
else: |
|
|
else: |
|
|
port = None |
|
|
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'): |
|
|
if (identityfile == 'y' or identityfile == 'Y' or identityfile.lower() =='yes'): |
|
|
identityfile = input("SSH Public key file location: ") |
|
|
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("Host {0}\n".format(host)) |
|
|
f.write("\tHostName {0}\n".format(hostname)) |
|
|
f.write("\tHostName {0}\n".format(hostname)) |
|
|
f.write("\tUser {0}\n".format(user)) |
|
|
f.write("\tUser {0}\n".format(user)) |
|
@ -23,5 +63,8 @@ def create_config(): |
|
|
f.write("\tPort {0}\n".format(port)) |
|
|
f.write("\tPort {0}\n".format(port)) |
|
|
if identityfile: |
|
|
if identityfile: |
|
|
f.write("\tIdentityFile {0}\n".format(identityfile)) |
|
|
f.write("\tIdentityFile {0}\n".format(identityfile)) |
|
|
|
|
|
f.close() |
|
|
|
|
|
|
|
|
create_config() |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
create_config() |
|
|
|
|
|
print(get_config()) |