def move(name, entry_name, config=None): """ Move an entry to the sshconfig. """ storm_ = get_storm_instance(config) try: if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') storm_.clone_entry(name, entry_name, keep_original=False) print( get_formatted_message( '{0} moved in ssh config. you can ' 'connect it by typing "ssh {0}".'.format( entry_name ), 'success') ) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr) sys.exit(1)
def add(name, connection_uri, id_file="", o=[], config=None): """ Adds a new entry to sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') user, host, port = parse( connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults) ) storm_.add_entry(name, host, user, port, id_file, o) print(get_formatted_message('{0} added to your ssh config. you can connect it by typing "ssh {0}".'.format( name ), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def list(tags, config=None): """ Lists hosts from ssh config with a specific TAG or all tags. """ storm_ = get_storm_instance(config) try: result = "" input_tags = ["@" + tag for tag in tags] input_tags = set(input_tags) # remove duplicates all_tags = storm_.ssh_config.hosts_per_tag.keys() if len(input_tags) == 0: # if tags given, display all found_tags = all_tags else: found_tags = set() for tag in input_tags: found_tags = found_tags | set( filter(lambda existing_tag: tag in existing_tag, all_tags)) for tag in found_tags: result += colored( 'Listing entries for tag', 'white', attrs=[ "bold", ]) + " {0}".format(tag) + "\n\n" for host in storm_.ssh_config.hosts_per_tag[tag]: result += format_host(host, storm_.defaults, with_tags=True, compact_tags=True) if len(result) != 0: print(get_formatted_message(result, "")) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def add(name, connection_uri, id_file="", o=[], config=None): """ Adds a new entry to sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') user, host, port = parse(connection_uri, user=get_default("user", storm_.defaults), port=get_default("port", storm_.defaults)) storm_.add_entry(name, host, user, port, id_file, o) print( get_formatted_message( '{0} added to your ssh config. you can connect ' 'it by typing "ssh {0}".'.format(name), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def move(name, entry_name, config=None): """ Move an entry to the sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') storm_.clone_entry(name, entry_name, keep_original=False) print( get_formatted_message( '{0} moved in ssh config. you can ' 'connect it by typing "ssh {0}".'.format( entry_name ), 'success') ) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def update(name, connection_uri="", id_file="", o=None, config=None): """ Enhanced version of the edit command featuring multiple edits using regular expressions to match entries """ if o is None: o = [] storm_ = get_storm_instance(config) settings = {} if id_file != "": settings['identityfile'] = id_file for option in o: k, v = option.split("=") settings[k] = v try: storm_.update_entry(name, **settings) print( get_formatted_message('"{0}" updated successfully.'.format(name), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr) sys.exit(1)
def list(config=None): """ Lists all hosts from ssh config. """ storm_ = get_storm_instance(config) try: result = colored('Listing entries:', 'white', attrs=["bold", ]) + "\n\n" result_stack = "" for host in storm_.list_entries(True): if host.get("type") == 'entry': if not host.get("host") == "*": result += " {0} -> {1}@{2}:{3}".format( colored(host["host"], 'green', attrs=["bold", ]), host.get("options").get("user", get_default("user", storm_.defaults)), host.get("options").get("hostname", "[hostname_not_specified]"), host.get("options").get("port", get_default("port", storm_.defaults)) ) extra = False for key, value in six.iteritems(host.get("options")): if not key in ["user", "hostname", "port"]: if not extra: custom_options = colored('\n\t[custom options] ', 'white') result += " {0}".format(custom_options) extra = True if isinstance(value, collections.Sequence): if isinstance(value, builtins.list): value = ",".join(value) result += "{0}={1} ".format(key, value) if extra: result = result[0:-1] result += "\n\n" else: result_stack = colored(" (*) General options: \n", "green", attrs=["bold",]) for key, value in six.iteritems(host.get("options")): if isinstance(value, type([])): result_stack += "\t {0}: ".format(colored(key, "magenta")) result_stack += ', '.join(value) result_stack += "\n" else: result_stack += "\t {0}: {1}\n".format( colored(key, "magenta"), value, ) result_stack = result_stack[0:-1] + "\n" result += result_stack print(get_formatted_message(result, "")) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def delete_all(): """ Deletes all hosts from ssh config. """ storm_ = get_storm_instance(config) try: storm_.delete_all_entries() print(get_formatted_message('all entries deleted.', 'success')) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def delete(name, config=None): """ Deletes a single host. """ storm_ = get_storm_instance(config) try: storm_.delete_entry(name) print(get_formatted_message('hostname "{0}" deleted successfully.'.format(name), 'success')) except StormValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def delete(name, config=None): """ Deletes a single host. """ storm_ = get_storm_instance(config) try: storm_.delete_entry(name) print( get_formatted_message( 'hostname "{0}" deleted successfully.'.format(name), 'success')) except StormValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def list(with_tags=False, config=None): """ Lists all hosts from ssh config. """ storm_ = get_storm_instance(config) try: result = colored('Listing entries:', 'white', attrs=[ "bold", ]) + "\n\n" for host in storm_.list_entries(True): result += format_host(host, storm_.defaults, with_tags) if len(result) != 0: print(get_formatted_message(result, "")) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def backup(target_file, config=None): """ Backups the main ssh configuration into target file. """ storm_ = get_storm_instance(config) try: storm_.backup(target_file) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def list(config=None): """ Lists all hosts from ssh config. """ storm_ = get_storm_instance(config) try: result = colored('listing entries:\n\n', 'white') result_stack = "" for host in storm_.list_entries(True): if host.get("type") == 'entry': if not host.get("host") == "*": result += " {0} -> {1}@{2}:{3}".format( colored(host["host"], 'white'), host.get("options").get("user", default_user), host.get("options").get("hostname", "[hostname_not_specified]"), host.get("options").get("port", 22)) extra = False for key, value in six.iteritems(host.get("options")): if not key in ["user", "hostname", "port"]: if not extra: custom_options = colored( '\n\t[custom options] ', 'white') result += " {0}".format(custom_options) extra = True if isinstance(value, collections.Sequence): if isinstance(value, builtins.list): value = ",".join(value) result += "{0}={1} ".format(key, value) if extra: result = result[0:-1] result += "\n\n" else: result_stack = " (*) -> " for key, value in six.iteritems(host.get("options")): if isinstance(value, type([])): result_stack += "{0}:\n".format(key) for value_ in value: result_stack += " {0}\n".format(value_) else: result_stack += " {0}:{1}\n".format( key, value, ) result_stack = result_stack[0:-1] + "\n" result += result_stack print(result) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def edit(name, connection_uri, id_file="", o=[], config=None): """ Edits the related entry in ssh config. """ storm_ = get_storm_instance(config) try: if ',' in name: name = " ".join(name.split(",")) user, host, port = parse(connection_uri) storm_.edit_entry(name, host, user, port, id_file, o) print( get_formatted_message('"{0}" updated successfully.'.format(name), 'success')) except StormValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def edit(name, connection_uri, id_file="", o=[], config=None): """ Edits the related entry in ssh config. """ storm_ = get_storm_instance(config) try: if ',' in name: name = " ".join(name.split(",")) user, host, port = parse(connection_uri) storm_.edit_entry(name, host, user, port, id_file, o) print(get_formatted_message( '"{0}" updated successfully.'.format( name ), 'success')) except StormValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def clone(name, clone_name, config=None): """ Clone an entry to the sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') storm_.clone_entry(name, clone_name) print(get_formatted_message('{0} added to your ssh config. you can connect it by typing "ssh {0}".'.format( clone_name ), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def clone(name, clone_name, config=None): """ Clone an entry to the sshconfig. """ storm_ = get_storm_instance(config) try: # validate name if '@' in name: raise ValueError('invalid value: "@" cannot be used in name.') storm_.clone_entry(name, clone_name) print( get_formatted_message( '{0} added to your ssh config. you can connect ' 'it by typing "ssh {0}".'.format(clone_name), 'success')) except ValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def update(name, connection_uri="", id_file="", o=[], config=None): """ Enhanced version of the edit command featuring multiple edits using regular expressions to match entries """ storm_ = get_storm_instance(config) settings = {} if id_file != "": settings['identityfile'] = id_file for option in o: k, v = option.split("=") settings[k] = v try: storm_.update_entry(name, **settings) print(get_formatted_message( '"{0}" updated successfully.'.format( name ), 'success')) except StormValueError as error: print(get_formatted_message(error, 'error'), file=sys.stderr)
def get_ip(name, glob=False, con=False, config=None): """ Get hostname/ip by name in ssh config (use --glob for glob search) """ # TODO: add user@hostname connection output storm_ = get_storm_instance(config) hostname = storm_.get_hostname(name, glob=glob) if hostname: if isinstance(hostname, list): for host in hostname: print(get_formatted_message(host, 'success'), file=sys.stderr)
def search(search_text): """ Searches entries by given search text. """ storm_ = get_storm_instance(config) try: results = storm_.search_host(search_text) if len(results) == 0: print('no results found.') if len(results) > 0: message = 'Listing results for {0}:\n'.format(search_text) message += "".join(results) print(message) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def search(search_text): """ Searches entries by given search text. """ storm_ = get_storm_instance(config) try: results = storm_.search_host(search_text) if len(results) == 0: print ('no results found.') if len(results) > 0: message = 'Listing results for {0}:\n'.format(search_text) message += "".join(results) print(message) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr)
def list(config=None): """ Lists all hosts from ssh config. """ storm_ = get_storm_instance(config) try: result = colored('Listing entries:', 'white', attrs=["bold", ]) + "\n\n" result_stack = "" for host in storm_.list_entries(True): if host.get("type") == 'entry': if not host.get("host") == "*": result += " {0} -> {1}@{2}:{3}".format( colored(host["host"], 'green', attrs=["bold", ]), host.get("options").get( "user", get_default("user", storm_.defaults) ), host.get("options").get( "hostname", "[hostname_not_specified]" ), host.get("options").get( "port", get_default("port", storm_.defaults) ) ) extra = False for key, value in six.iteritems(host.get("options")): if not key in ["user", "hostname", "port"]: if not extra: custom_options = colored( '\n\t[custom options] ', 'white' ) result += " {0}".format(custom_options) extra = True if isinstance(value, collections.Sequence): if isinstance(value, builtins.list): value = ",".join(value) result += "{0}={1} ".format(key, value) if extra: result = result[0:-1] result += "\n\n" else: result_stack = colored( " (*) General options: \n", "green", attrs=["bold",] ) for key, value in six.iteritems(host.get("options")): if isinstance(value, type([])): result_stack += "\t {0}: ".format( colored(key, "magenta") ) result_stack += ', '.join(value) result_stack += "\n" else: result_stack += "\t {0}: {1}\n".format( colored(key, "magenta"), value, ) result_stack = result_stack[0:-1] + "\n" result += result_stack print(get_formatted_message(result, "")) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr) sys.exit(1)
def list(config=None): """ Lists all hosts from ssh config. """ storm_ = get_storm_instance(config) try: result = colored('Listing entries:', 'white', attrs=[ "bold", ]) + "\n\n" result_stack = "" for host in storm_.list_entries(True): if host.get("type") == 'entry': if not host.get("host") == "*": myhost = host["host"] user = host.get("options").get( "user", get_default("user", storm_.defaults)) hostname = host.get("options").get("hostname", myhost) port = host.get("options").get( "port", get_default("port", storm_.defaults)) proxy = colored(host.get("options").get("proxyjump", None), 'green', attrs=[ "bold", ]) myhost = colored(host["host"], 'green', attrs=[ "bold", ]) agent = '' control = '' if host.get("options").get("forwardagent", None): agent = colored('agent', 'red', attrs=[ "bold", ]) if host.get("options").get("controlmaster", None): control = colored('control', 'red', attrs=[ "bold", ]) #control = colored(host.get("options").get("controlmaster", None), 'red', attrs=["bold", ]) if host.get("options").get("proxyjump", None): result += " {0} -> {1}@{2}:{3} via {4} {5} {6}".format( myhost, user, hostname, port, proxy, agent, control) else: result += " {0} -> {1}@{2}:{3} {4} {5}".format( myhost, user, hostname, port, agent, control) extra = False for key, value in six.iteritems(host.get("options")): if not key in [ "user", "hostname", "port", "proxyjump" ]: if not extra: custom_options = colored( '\n\t[custom options] ', 'white') result += " {0}".format(custom_options) extra = True if isinstance(value, collections.Sequence): if isinstance(value, builtins.list): value = ",".join(value) result += "{0}={1} ".format(key, value) if extra: result = result[0:-1] result += "\n\n" else: result_stack = colored(" (*) General options: \n", "green", attrs=[ "bold", ]) for key, value in six.iteritems(host.get("options")): if isinstance(value, type([])): result_stack += "\t {0}: ".format( colored(key, "magenta")) result_stack += ', '.join(value) result_stack += "\n" else: result_stack += "\t {0}: {1}\n".format( colored(key, "magenta"), value, ) result_stack = result_stack[0:-1] + "\n" result += result_stack print(get_formatted_message(result, "")) except Exception as error: print(get_formatted_message(str(error), 'error'), file=sys.stderr) sys.exit(1)