zsh-users
 help / color / mirror / code / Atom feed
* ssh host completion
@ 2018-02-28  2:13 John Benninghoff
  2018-03-22  0:26 ` Oliver Kiddle
  0 siblings, 1 reply; 4+ messages in thread
From: John Benninghoff @ 2018-02-28  2:13 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 3199 bytes --]

I have tried to customize ssh completion using zstyle so that it uses only
the host names configured in my ~/.ssh/config file.  I use the following
section in my .zshrc file to try to get the desired behavior:

      if [[ -r ~/.ssh/config ]]; then
         sconf=( $(sed -n '/^Host[=\t ]/{s///;s/#.*//;p;}' ~/.ssh/config) )
         zstyle ':completion::complete:ssh:argument-1:' tag-order !users
         zstyle ':completion::*:ssh:*:' hosts $sconf
         #zstyle -e ':completion::*:ssh:*:' hosts REPLY=$sconf
         #zstyle -e ':completion::*:ssh:*:' hosts reply=$sconf
         # Use ^Xh to see context and tags applicable in CLI position
      fi

Some of my failed zstyle attempts are commented out.  I used the first
zstyle command to eliminate the built-in users completion I didn't
want(!users).  The 2nd remaining zstyle command attempts to give me
hostname completion based on hostnames in ~/.ssh/config.  I know there is a
built-in ssh hostname completion based on /etc/hosts.  I want to remove
that and replace it with the $sconf setting.  I'm not sure how to remove
the built-in hostname completion.

*jbenninghoff@jb-mbpro 1: zstyle*
completer
        :completion:* _complete _ignored
filename
        :compinstall '$HOME/.zshrc'
hosts
        :completion::*:ssh:*: kubeproxy pslab psnode184 ds-cluster-01
aws-ds rs1 rs-sles01 psedge ps135 psnode135 ansible dellclient dellnvme hec
lenovo-sr630 s8 l8s lenovo b143-l8seth0 t1 t2 sles4 sles3 sles2 sles medge
aws-p1 philips phil irs-edge irs aws-ara aws-araceli aws-jb aws-e2 o-prod
oprod odev bhte bhtestedge bht bhtestn1 bhe bhprodedge bh bhnode1
ciscopocedge pocedge ciscopoc poc aws-es aws-m1 aws-e3 aws-mesos mesos
aws-vgutta vg aws-deborah aws-e1 aws-juniper-poc juni aws-indexer labgw
jump cjbox3 ciscojbox2 cjbox2 ciscojbox1 marco marcoaws sandboxsko sandboxd
tron cobbler procurve sandbox4 ps1 ds1 pslab2 kddi pslab1 gracenote gr
jbimpala0 rubi rubicon sandbox3 atsqa trig triggit lab-hdp msftp qa2 qa1
sftp cvprod cvqa cvdev qa3 return dglab1 ibm cent centVM edge vm1 edge1 sm1
edge2 esxv1 esxv2 esxv3 esxv4 esxv5 esxvm1 stackiq hp hpold plat bolt hp2
mbpro firefly cisco16 dell cisco sapporo
tag-order
        :completion::complete:ssh:argument-1: !users

Strangely, zstyle shows the correct list of hostnames in hosts section but
those do not get used when I attempt completion by starting with 'ps' then
tab for example.
I get:

*jbenninghoff@jb-mbpro 1: ssh psnode4*
psnode40     psnode41     psnode42     psnode43     psnode44
psnode40.ps  psnode41.ps  psnode42.ps  psnode43.ps  psnode44.ps

Which are hostnames in /etc/hosts but not in ~/.ssh/config.

When I check the ssh completion in effect right after I type ssh by using
^Xh, I see the below output:

*jbenninghoff@jb-mbpro 1: ssh*
tags in context :completion::complete:ssh::
    argument-1 options  (_arguments _ssh _ssh)
tags in context :completion::complete:ssh:argument-1:
    hosts users  (_ssh _ssh)
    hosts        (_hosts _combination _ssh_hosts _ssh _ssh)
    users        (_users _combination _ssh_users _ssh _ssh)

jbenninghoff@jb-mbpro 1: zsh --version
zsh 5.3 (x86_64-apple-darwin17.0)
jbenninghoff@jb-mbpro 1: type -a zsh
zsh is /bin/zsh


/John

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ssh host completion
  2018-02-28  2:13 ssh host completion John Benninghoff
@ 2018-03-22  0:26 ` Oliver Kiddle
  2018-03-25  4:05   ` John Benninghoff
  0 siblings, 1 reply; 4+ messages in thread
From: Oliver Kiddle @ 2018-03-22  0:26 UTC (permalink / raw)
  To: John Benninghoff; +Cc: zsh-users

On 27 Feb, John Benninghoff wrote:
> the host names configured in my ~/.ssh/config file.  I use the following
> section in my .zshrc file to try to get the desired behavior:
>
>       if [[ -r ~/.ssh/config ]]; then
>          sconf=( $(sed -n '/^Host[=\t ]/{s///;s/#.*//;p;}' ~/.ssh/config) )
>          zstyle ':completion::complete:ssh:argument-1:' tag-order !users
>          zstyle ':completion::*:ssh:*:' hosts $sconf

I think the context for this zstyle won't match. Try something like:
  zstyle ':completion::*:ssh:*:hosts' hosts $sconf
or perhaps just:
  zstyle ':completion::*:ssh:*' hosts $sconf

If that doesn't help, try experimenting with the users-hosts style. That
needs a context like ':completion:*:ssh:*:my-accounts'. It also expects
usernames in the style values.

>          # Use ^Xh to see context and tags applicable in CLI position

Also useful is ^X? to do a full trace. If you view the trace with less,
filtering it with & can be very useful.

>         :completion::complete:ssh:argument-1: !users

A similar but slightly different option is to just hide the users:
  zstyle ':completion:*:ssh:*:users' hidden true

Sorry that your message didn't get answered sooner.

Oliver


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ssh host completion
  2018-03-22  0:26 ` Oliver Kiddle
@ 2018-03-25  4:05   ` John Benninghoff
  2018-03-25  5:18     ` Vadim A. Misbakh-Soloviov
  0 siblings, 1 reply; 4+ messages in thread
From: John Benninghoff @ 2018-03-25  4:05 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1392 bytes --]

Thanks, the first context suggestion worked.

/John

On Wed, Mar 21, 2018 at 5:26 PM, Oliver Kiddle <okiddle@yahoo.co.uk> wrote:

> On 27 Feb, John Benninghoff wrote:
> > the host names configured in my ~/.ssh/config file.  I use the following
> > section in my .zshrc file to try to get the desired behavior:
> >
> >       if [[ -r ~/.ssh/config ]]; then
> >          sconf=( $(sed -n '/^Host[=\t ]/{s///;s/#.*//;p;}'
> ~/.ssh/config) )
> >          zstyle ':completion::complete:ssh:argument-1:' tag-order !users
> >          zstyle ':completion::*:ssh:*:' hosts $sconf
>
> I think the context for this zstyle won't match. Try something like:
>   zstyle ':completion::*:ssh:*:hosts' hosts $sconf
> or perhaps just:
>   zstyle ':completion::*:ssh:*' hosts $sconf
>
> If that doesn't help, try experimenting with the users-hosts style. That
> needs a context like ':completion:*:ssh:*:my-accounts'. It also expects
> usernames in the style values.
>
> >          # Use ^Xh to see context and tags applicable in CLI position
>
> Also useful is ^X? to do a full trace. If you view the trace with less,
> filtering it with & can be very useful.
>
> >         :completion::complete:ssh:argument-1: !users
>
> A similar but slightly different option is to just hide the users:
>   zstyle ':completion:*:ssh:*:users' hidden true
>
> Sorry that your message didn't get answered sooner.
>
> Oliver
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: ssh host completion
  2018-03-25  4:05   ` John Benninghoff
@ 2018-03-25  5:18     ` Vadim A. Misbakh-Soloviov
  0 siblings, 0 replies; 4+ messages in thread
From: Vadim A. Misbakh-Soloviov @ 2018-03-25  5:18 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 647 bytes --]

> Thanks, the first context suggestion worked.

Just in case, here is what I use for hosts completion:

  zstyle -e ':completion:*:hosts' hosts 'reply=(
    ${${${${${(f)"$(cat ${HOME}/.ssh/known_hosts 2>/dev/null)"//\[/}//\]:/ 
}:#[\|]*}%%\ *}%%,*}
    ${${${(z)${(@M)${(f)"$(cat ${HOME}/.ssh/config 2>/dev/null)"}:#Host *}
#Host }:#*[*?]*}}
    ${(s: :)${(ps:\t:)${${(f)~~"$(cat /etc/hosts 2>/dev/null)"}%%\#*}
#*[[:blank:]]}}
  )'


(I hope, you'll be able to restore lines after "78-col breakage")

Also, it is possible to avoid using "cats" by using $(<file), but it'll throw 
errors if there is no such file (and I forgot how to hide them).

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 228 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-25  5:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-28  2:13 ssh host completion John Benninghoff
2018-03-22  0:26 ` Oliver Kiddle
2018-03-25  4:05   ` John Benninghoff
2018-03-25  5:18     ` Vadim A. Misbakh-Soloviov

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).