def search_host(self, search_string): results = self.ssh_config.search_host(search_string) formatted_results = [] for host_entry in results: agent = '' control = '' host = colored(host_entry.get("host"), 'green', attrs=[ "bold", ]) options = host_entry.get("options") hostname = options.get('hostname', 'no-hostname') user = options.get('user', 'root') port = options.get('port', 22) proxy = options.get('proxyjump') if options.get('forwardagent'): agent = colored('agent', 'red', attrs=[ "bold", ]) if options.get('controlmaster'): control = colored('control', 'red', attrs=[ "bold", ]) if proxy: proxy = colored(proxy, 'green', attrs=[ "bold", ]) formatted_results.append( " {0} -> {1}@{2}:{3} via {4} {5} {6}\n".format( host, user, hostname, port, proxy, agent, control)) else: formatted_results.append( " {0} -> {1}@{2}:{3} {4} {5}\n".format( host, user, hostname, port, agent, control)) return formatted_results
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 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 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 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 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)
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 format_host(host, defaults, with_tags=False, compact_tags=False): result = "" result_stack = "" 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", defaults)), host.get("options").get("hostname", "[hostname_not_specified]"), host.get("options").get("port", get_default("port", 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] if with_tags: if len(host.get('tags')) > 0: if compact_tags: result += " {0}".format('') value = " ".join( map( lambda tag: colored( tag, 'green', attrs=[ "bold", ]), host.get('tags'))) result += "{0} ".format(value) else: tags = colored('\n\t[tags] ', 'white') result += " {0}".format(tags) value = ", ".join(host.get('tags')) result += "{0} ".format(value) result += "\n\n" elif host.get("options") != {}: 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 return result