Check ssh's KnownhostsFiles in print_hostnames

These are an additional source of information

Thanks to @sysbot (#2313) for the inspiration.

This also stringifies the ssh_config reading
This commit is contained in:
Fabian Homborg 2016-02-27 00:31:21 +01:00
parent 65aeaab054
commit fbd53f2da1

View file

@ -20,17 +20,20 @@ function __fish_print_hostnames -d "Print a list of known hostnames"
string match -r '^\s*[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3]:|^[a-zA-Z\.]*:' </etc/fstab | string replace -r ':.*' ''
end
# Print hosts with known ssh keys
# Does not match hostnames with @directives specified
__fish_sgrep -Eoh '^[^#@|, ]*' ~/.ssh/known_hosts{,2} ^/dev/null | string replace -r '^\[([^]]+)\]:[0-9]+$' '$1'
# Check hosts known to ssh
set -l known_hosts ~/.ssh/known_hosts{,2} /etc/ssh/known_hosts{,2} # Yes, seriously - the default specifies both with and without "2"
for file in /etc/ssh/ssh_config ~/.ssh/config
if test -r $file
# Print hosts from system wide ssh configuration file
if [ -e /etc/ssh/ssh_config ]
awk -v FS="[ =]+" -v OFS='\n' 'tolower($0) ~ /^ *host[^*?!]*$/{ $1=""; print }' /etc/ssh/ssh_config
end
# Print hosts from ssh configuration file
if [ -e ~/.ssh/config ]
awk -v FS="[ =]+" -v OFS='\n' 'tolower($0) ~ /^ *host[^*?!]*$/{ $1=""; print }' ~/.ssh/config
# Note the non-capturing group to avoid printing "name"
string match -r '\s*Host(?:name)? \w.*' < $file | string replace -r '^\s*Host(?:name)?\s*(\S+)' '$1'
set known_hosts $known_hosts (string match -r '^\s*UserKnownHostsFile|^\s*GlobalKnownHostsFile' <$file \
| string replace -r '.*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