compsys: Hostnames

Add the hostnames from ~/.ssh/known_hosts

local _myhosts
if [[ -f $HOME/.ssh/known_hosts ]]; then
  _myhosts=( ${${${${(f)"$(<$HOME/.ssh/known_hosts)"}:#[0-9]*}%%\ *}%%,*} )
  zstyle ':completion:*' hosts $_myhosts
fi

Adding from various ssh files

Add the hostnames from all the files ssh uses for storing hosts and /etc/hosts based on /usr/share/doc/zsh/examples/ssh_completion2.gz. This will remove any entries that are negated and not give an error for files not present. Author not listed in ssh_completion2.gz.

local _myhosts;
_myhosts=( ${${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) /dev/null)"}%%[# ]*}//,/ }:#\!*}
${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2>/dev/null))"}%%\#*} );
zstyle ':completion:*' hosts $_myhosts;

Using ~/.ssh/config

[ -f ~/.ssh/config ] && : ${(A)ssh_config_hosts:=${${${${(@M)${(f)"$(<~/.ssh/config)"}:#Host *}#Host }:#*\**}:#*\?*}}
[ -f ~/.ssh/known_hosts ] && : ${(A)ssh_known_hosts:=${${${(f)"$(<$HOME/.ssh/known_hosts)"}%%\ *}%%,*}}

zstyle ':completion:*:*:*' hosts $ssh_config_hosts $ssh_known_hosts

(from http://busy-bee.net/files/zsh/zshrc)

 
examples/compsys/hostnames.txt · Last modified: 2010/01/05 09:20 (external edit)