function __fish_print_hostnames -d "Print a list of known hostnames" # Print all hosts from /etc/hosts # use 'getent hosts' on OSes that support it (OpenBSD and Cygwin do not) if type -q getent; and getent hosts >/dev/null 2>&1 # test if 'getent hosts' works and redirect output so errors don't print # Ignore zero ips getent hosts | string match --regex --invert '^0.0.0.0' \ # Remove left addresses column | string replace --regex '^\s*\S+\s+' '' \ # Tokenize remaining hostnames and aliases columnss | string split ' ' else if test -r /etc/hosts # Ignore commented lines and functionally empty lines string match --regex --invert '^\s*0.0.0.0|^\s*#|^\s*$' /dev/null set paths[$path_index] $relative_path/$paths[$path_index] end echo $paths[$path_index] end _recursive $paths end end _recursive $ssh_config end set -l ssh_configs (_ssh_include /etc/ssh/ssh_config) (_ssh_include $ssh_config) for file in $ssh_configs if test -r $file # Print hosts from system wide ssh configuration file string match --regex --ignore-case '^\s*Host\s+\S+' <$file \ # We only want the value(s) | string replace --regex --ignore-case '^\s*Host\s+' '' \ # Print one per line | string trim | string replace --regex '\s+' ' ' | string split ' ' \ # Ignore hosts with a glob | string match --invert '*\**' # Extract known_host paths set known_hosts $known_hosts (string match -ri '^\s*UserKnownHostsFile|^\s*GlobalKnownHostsFile' <$file \ | string replace -ri '.*KnownHostsFile\s*' '') end end for file in $known_hosts # Ignore hosts that are hashed, commented or have custom ports (like [localhost]:2200) test -r $file and string replace -ra '(\S+) .*' '$1' <$file | string match -r '^[^#|[=]+$' | string split "," end return 0 end