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 # (apparently just Cygwin does not). # # Test if 'getent hosts' works and redirect output so errors don't print. # # This is all done under the assumption that `getent` *might* print more hosts than the static /etc/hosts. type -q getent # Ignore zero IPs. and getent hosts 2>/dev/null | string match -r -v '^0.0.0.0' | string replace -r '^\s*\S+\s+' '' | string split ' ' # We care about _getent_s status, not `string split`s. if test $pipestatus[1] -ne 0; and test -r /etc/hosts # Ignore commented lines and functionally empty lines. string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' /test/file1 /home//test/file2), # and `set` will prevent "No matches for wildcard" messages eval set expanded_path $path for path in $expanded_path # Skip unusable paths. test -r "$path" -a -f "$path" or continue echo $path set new_paths $new_paths $path end end if test -n "$new_paths" _recursive $new_paths end end _recursive $ssh_config end set -l ssh_configs /etc/ssh/ssh_config (_ssh_include /etc/ssh/ssh_config) $ssh_config (_ssh_include $ssh_config) for file in $ssh_configs if test -r $file # Print hosts from system wide ssh configuration file string replace -rfi '^\s*Host\s+' '' <$file | string trim | string replace -r '\s+' ' ' | string split ' ' | string match -v '*\**' # Extract known_host paths. set known_hosts $known_hosts (string replace -rfi '.*KnownHostsFile\s*' '' <$file) end end for file in $known_hosts if test -r $file # Ignore hosts that are hashed, commented or @-marked and strip the key. awk '$1 !~ /[|#@]/ { n=split($1, entries, ",") for (i=1; i<=n; i++) { # Ignore negated/wildcarded hosts. if (!match(entry=entries[i], "[!*?]")) { # Extract hosts with custom port. if (substr(entry, 1, 1) == "[") { if (pos=match(entry, "]:.*$")) { entry=substr(entry, 2, pos-2) } } print entry } } }' $file end end return 0 end