diff options
author | Fabian Homborg <FHomborg@gmail.com> | 2016-02-27 00:31:21 +0100 |
---|---|---|
committer | Fabian Homborg <FHomborg@gmail.com> | 2016-03-02 15:16:52 +0100 |
commit | fbd53f2da1af69058b16320265364257a11571df (patch) | |
tree | 6bb0a54a0a8430df6f3847c62b28d6d48b0333b2 /share/functions/__fish_print_hostnames.fish | |
parent | 65aeaab0545cef840daee0c64de8ac763ae2e4a8 (diff) |
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
Diffstat (limited to 'share/functions/__fish_print_hostnames.fish')
-rw-r--r-- | share/functions/__fish_print_hostnames.fish | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/share/functions/__fish_print_hostnames.fish b/share/functions/__fish_print_hostnames.fish index 4910d3f8..492954a4 100644 --- a/share/functions/__fish_print_hostnames.fish +++ b/share/functions/__fish_print_hostnames.fish @@ -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' - - # 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 + # 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 + # 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 - - # Print hosts from ssh configuration file - if [ -e ~/.ssh/config ] - awk -v FS="[ =]+" -v OFS='\n' 'tolower($0) ~ /^ *host[^*?!]*$/{ $1=""; print }' ~/.ssh/config + 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 |