#570951 zsh: ssh completes to non-resolvable hostname from .ssh/known_hosts

Package:
zsh
Source:
zsh
Description:
shell with lots of features
Submitter:
Vincent Lefevre
Date:
2010-02-28 21:39:08 UTC
Severity:
normal
#570951#5
Date:
2010-02-22 13:09:15 UTC
From:
To:
When I do "ssh pru[TAB]", ssh completes to a non-resolvable hostname
from the .ssh/known_hosts file: prunille.vinc17.org

In fact this machine is resolvable only from my local network, and
it is an error to assume that a host key in .ssh/known_hosts is
necessarily resolvable (it may be there just to check the key thanks
to a HostKeyAlias declaration). IMHO, if the key is listed as an alias
from .ssh/config but not as a Host identifier, it should be discarded.
Hosts from .ssh/config should be used instead. The algorithm would be:

1. Take all host identifiers after Host declarations from .ssh/config
   (strings with '?' and/or '*' should not be taken into account).
2. Add all hosts from .ssh/known_hosts that are not an argument of
   a HostKeyAlias declaration (if hosts are in fact hashes, due to
   HashKnownHosts, they should not be taken into account).

For instance, if .ssh/config has:

Host myhost
  HostKeyAlias my-real-host-name.mydomain
  Hostname gateway.mydomain
  Port 12345

then "ssh my[TAB]" should complete to myhost only.

Of course, as a workaround to the current behavior, the user could
write:

Host myhost my-real-host-name.mydomain
  HostKeyAlias my-real-host-name.mydomain
  Hostname gateway.mydomain
  Port 12345

but this can make maintenance of the config file less easy.

#570951#10
Date:
2010-02-28 21:37:24 UTC
From:
To:
gaining syntax in the form of

[localhost]:2222,[127.0.0.1]:2222 ssh-rsa keyfingerprintblahblah id

resulting in tab completion of \[localhost\]:2222 as a host.

I'm sure there is a better solution, but dropping entries with brackets
seems like an improvement over the status quo.

Index: Completion/Unix/Type/_hosts
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Type/_hosts,v
retrieving revision 1.10
diff -u -r1.10 _hosts
--- Completion/Unix/Type/_hosts	5 Apr 2006 10:04:32 -0000	1.10
+++ Completion/Unix/Type/_hosts	28 Feb 2010 21:35:11 -0000
@@ -41,9 +41,9 @@

     for khostfile in $khostfiles; do
       if [[ -r $khostfile ]]; then
-        khosts=(${(s:,:)${(j:,:)${(u)${(f)"$(<$khostfile)"}%%[ |#]*}}})
+        khosts=(${${(s:,:)${(j:,:)${(u)${(f)"$(<$khostfile)"}%%[ |#]*}}}:#*[\[\]]*})
         if [[ -z $useip ]]; then
-	  khosts=(${${khosts:#(#s)[0-9]##.[0-9]##.[0-9]##.[0-9]##(#e)}:#(#s)[0-9a-f:]##(#e)})
+	  khosts=(${${${khosts:#(#s)[0-9]##.[0-9]##.[0-9]##.[0-9]##(#e)}:#(#s)[0-9a-f:]##(#e)}:#*[\[\]]*})
         fi
         _cache_hosts+=($khosts)
       fi