Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: hostname completion for ssh / scp



> On 2016-11-05 18:24, Bart Schaefer wrote:
> Well, yes.  The syntax of the /etc/hosts file is first an IP and then
> a hostname.  Somebody will possibly correct me, but I don't recall
> any valid use of /etc/hosts that has lines of host names without IPs.

Not true. IP address is not mandatory in /etc/hosts. You can leave it
out, and only have hostnames. This is useful for host autocompletion.
You don't have to type a long hostname, such as:

  ssh long-or-complicated-host.my-subdomain.my-domain.com

instead, you just type

  ssh long<TAB>

> } > If getent isn't returning anything useful, you can replace it:
> } > 
> } > zstyle ':completion:*:hosts' command 'cat /etc/hosts'
> } 
> } I would like to keep the completion from ~/.ssh/config and add
> } completion from /etc/hosts.
> 
> This shouldn't change the behavior with respect to ~/.ssh/config
> because that's read in _ssh_hosts rather than in _hosts where the
> style above applies.
> _hosts reads ~/.ssh/known_hosts whether or not the program named by
> the command style returns anything.
> 
> However, ~/.ssh/config will be skipped if the users-hosts tag finds
> any matching hosts, which is likely to happen if you are starting
> from a blank word, because users-hosts calls _hosts which will look
> in known_hosts.  If you start with a prefix on the line that matches
> a host in ~/.ssh/config and does NOT match one in ~/.ssh/known_hosts,
> you should get a completion.

yes that is correct.
But this is a terrible feature. The completion jumps to first matching
hostname, completely ignoring other matching hostnames.

  ssh s<TAB>

will complete to

  ssh server1.mydomain.com

and completely disregard

  system1.mydomain.com
  system2.mydomain.com

> 
> } > Note there have been several other fixes/improvements to ssh host-gathering
> } > since zsh 5.0.
> } 
> } can I simply copy Unix/_ssh from zsh 5.0. and use it on zsh 4.3.17 ?
> 
> I don't think so (you'd need at least _hosts as well) but in any case
> it wouldn't change anything because even 5.0+ is not expecting you to
> have just dumped a bunch of bare host names into /etc/hosts like that.
> 



Messages sorted by: Reverse Date, Date, Thread, Author