-
-
Save FrankGrimm/461311 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_sshm() | |
{ | |
local cur prev opts | |
COMPREPLY=() | |
cur="${COMP_WORDS[COMP_CWORD]}" | |
prev="${COMP_WORDS[COMP_CWORD-1]}" | |
ADD_OPTS=$(sshm --complete) | |
opts="--help --version --add --list --del --show --complete $ADD_OPTS" | |
if [[ ${cur} == * ]] ; then | |
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) | |
return 0 | |
fi | |
} | |
complete -F _sshm sshm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import logging | |
import sys, os, getpass | |
import pickle | |
import shlex, subprocess | |
VERSION="0.1" | |
logging.basicConfig(level=logging.ERROR,stream=sys.stderr) | |
logger = logging.getLogger("Main") | |
class SingleServer(object): | |
""" Structure holding server data """ | |
alias = "" | |
hostname = "" | |
parameters = "" | |
username = "" | |
port = "22" | |
def __str__(self): | |
return self.alias + "\t" + self.username + "@" + self.hostname + " (" + self.port + ")" | |
class ServerList(object): | |
""" List of servers """ | |
logger = logging.getLogger("ServerList") | |
file_name = os.path.expanduser("~/.sshm_list") | |
__data = [] | |
def __init__(self): | |
self.logger.debug("Initializing") | |
self.open_or_create_file() | |
def __str__(self): | |
# TODO implement ;) | |
return "TODO" | |
def load_list(self): | |
# TODO Exceptionhandler | |
self.logger.debug("Unpickling " + self.file_name) | |
up = pickle.Unpickler(open(self.file_name, "r")) | |
try: | |
self.__data = up.load() | |
except EOFError: | |
self.logger.info("Server list file is empty.") | |
def add_server(self, server): | |
if server.alias != "": | |
# TODO check if alias already exists and replace it eventually | |
self.__data.append(server) | |
self.logger.debug("Added " + server.alias + " to server list. Total items: " + str(len(self.__data))) | |
self.save_list() | |
else: | |
self.logger.warning("Not adding server. Alias field is empty") | |
def get_server(self, alias): | |
for item in self.__data: | |
if item.alias == alias: | |
return item | |
return None | |
def del_server(self, alias): | |
match = None | |
for item in self.__data: | |
if item.alias == alias: | |
match = item | |
if not match is None: | |
self.__data.remove(match) | |
self.save_list() | |
return True | |
else: | |
return False | |
def save_list(self): | |
# TODO Exceptionhandler | |
self.logger.debug("Pickling " + str(len(self.__data)) + " objects to " + self.file_name) | |
pp = pickle.Pickler(open(self.file_name, "w")) | |
pp.dump(self.__data) | |
def open_or_create_file(self): | |
if os.path.exists(self.file_name): | |
self.logger.debug("Server list file exists.") | |
else: | |
self.logger.info("Server list file does not exist. Creating.") | |
try: | |
touch_handle = open(self.file_name, 'w') | |
touch_handle.close() | |
except Exception: | |
pass | |
self.load_list() | |
def print_list(self, filter=""): | |
filtered_list = [] | |
for current_server in self.__data: | |
if filter == "" or current_server.alias.find(filter) != -1: | |
filtered_list.append(current_server) | |
if len(filtered_list) != 0: | |
print "Listing " + str(len(filtered_list)) + " servers:" | |
print "#num\tAlias\tuser@host (port)" | |
i = 0 | |
for current_server in filtered_list: | |
i += 1 | |
print "#" + str(i) + "\t" + str(current_server) | |
else: | |
print "No servers match this search." | |
def get_servercount(self): | |
return len(self.__data) | |
def print_info(self, alias): | |
match = None | |
for current_server in self.__data: | |
if current_server.alias == alias: | |
match = current_server | |
if not match is None: | |
print "\"" + match.alias + "\"" | |
print "---------------------------" | |
print "Hostname: " + match.hostname | |
print "Username: " + match.username | |
print "Port: " + match.port | |
print "Parameters: " + match.parameters | |
else: | |
print "Not found" | |
def print_aliases(self): | |
for server in self.__data: | |
print server.alias, | |
def __del__(self): | |
self.logger.debug("Delete") | |
CL_HANDLER = [['--help', 'print_usage'], ['--add', 'add_server'], ['--list', 'list_servers'], ['--del', 'del_server'], ['--show', 'show_server_info'], ['--version', 'print_version'], ['--complete', 'generate_completion']] | |
def generate_completion(): | |
sl = ServerList() | |
sl.print_aliases() | |
def print_version(): | |
print "sshm-ng Version " + VERSION | |
def read_mode(): | |
if len(sys.argv) > 1: | |
for current_mode in CL_HANDLER: | |
if sys.argv[1] == current_mode[0]: | |
return current_mode | |
return None | |
def show_server_info(): | |
if len(sys.argv) > 2: | |
sl = ServerList() | |
sl.print_info(sys.argv[2]) | |
else: | |
print "--show requires one argument: alias" | |
def list_servers(): | |
filter = "" | |
if len(sys.argv) > 2: | |
filter = sys.argv[2] | |
sl = ServerList() | |
sl.print_list(filter) | |
def del_server(): | |
if len(sys.argv) > 2: | |
sl = ServerList() | |
if sl.del_server(sys.argv[2]): | |
print "Server removed" | |
else: | |
print "An error occured or alias not found" | |
else: | |
print "--del requires one argument: alias" | |
def add_server(): | |
if len(sys.argv) > 2: | |
server_n = SingleServer() | |
server_n.alias = sys.argv[2] | |
print "Adding server \"" + server_n.alias + "\"" | |
print "Please enter connection data:" | |
server_n.hostname = raw_input("Hostname / IP: ").strip() | |
server_n.username = raw_input("User [" + getpass.getuser() + "]: ").strip() | |
server_n.port = raw_input("Port [22]: ").strip() | |
server_n.parameters = raw_input("Parameters []: ").strip() | |
if server_n.hostname != "": | |
if server_n.username == "": | |
server_n.username = getpass.getuser() | |
if server_n.port == "": | |
server_n.port = "22" | |
sl = ServerList() | |
sl.add_server(server_n) | |
else: | |
print "A hostname or IP is required." | |
else: | |
print "--add requires one argument: alias" | |
def print_usage(): | |
print "sshm-ng usage information " | |
print "--help\t\t\tShow this text" | |
print "--version\t\tShow version number" | |
print "--add [alias]\t\tAdd a server" | |
print "--list (filter)\t\tList servers" | |
print "--del [alias]\t\tDelete server from list" | |
print "--show [alias]\t\tShow detailed information on a server" | |
print "--complete\t\tGenerate bash completion" | |
print "(filter) reduces resultset to servers aliases that contain the filter expression" | |
print "[alias] is a non-optional pseudo-name for the server" | |
def main(): | |
selected_mode = read_mode() | |
if not selected_mode is None: | |
logger.info("Starting with mode: " + selected_mode[0]) | |
globals()[selected_mode[1]]() | |
else: | |
extra_parameters = "" | |
alias = "" | |
if len(sys.argv) > 1: | |
alias = sys.argv[len(sys.argv)-1] | |
for idx in range(1,len(sys.argv)-1): | |
extra_parameters += " " + sys.argv[idx] | |
extra_parameters = extra_parameters.lstrip() | |
sl = ServerList() | |
server = sl.get_server(alias) | |
if server is None: | |
print "Alias not found." | |
else: | |
print "Connecting to : " + server.alias + " (" + server.username + "@" + server.hostname + ":" + server.port + ")" | |
if server.parameters != "": | |
print "Parameters: " + server.parameters | |
if extra_parameters != "": | |
print "Using extra parameters: " + extra_parameters | |
params = server.parameters.strip() + " " + extra_parameters | |
if server.port != "22": | |
params = params.strip() + " -p " + server.port | |
params = params.strip() | |
callstack = ['ssh'] | |
if params != "": | |
callstack.extend(shlex.split(params)) | |
callstack.append(server.username + "@" + server.hostname) | |
subprocess.call(callstack) | |
else: | |
print "Alias missing." | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment