zsh-users
 help / color / mirror / code / Atom feed
* Ssh completion from ~/.ssh/config
@ 2015-04-07 13:14 Jesper Nygårds
  2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
  0 siblings, 1 reply; 8+ messages in thread
From: Jesper Nygårds @ 2015-04-07 13:14 UTC (permalink / raw)
  To: zsh-users

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

I would like to have ssh complete with hosts from my ~/.ssh/config, but I
can't get it to work. I don't know if I'm missing something obvious.

Suppose I have the following in my ssh-config:

Host myhost
  HostName 192.168.1.1
  User jesper

Typing "ssh <tab>" I would like it to supply "myhost".

Looking through the standard completion script _ssh, I see the following
lines:

  # If users-hosts matches, we shouldn't complete anything else.
  if [[ "$IPREFIX" == *@ ]]; then
      _combination -s '[:@]' my-accounts users-hosts "users=${IPREFIX/@}"
hosts "$@" && return
   else
       _combination -s '[:@]' my-accounts users-hosts \
       ${opt_args[-l]:+"users=${opt_args[-l]:q}"} hosts "$@" && return
  fi

In this case the else statement is executed. Since I have not defined any
"my-accounts", the completion file _hosts is auto-loaded. This script reads
several files to find possible completions (among others /etc/hosts and
~/.ssh/known_hosts).

Since this fulfills _combination, the lines in _ssh which parse
~/.ssh/config are never executed.

If I comment out the lines I quoted above it works as I would like it to
work, but with the obvious drawback that I have modified a file that is
part of the zsh distribution, and I need to keep track of this file every
time I upgrade.

I could also specify "my-accounts" or "hosts" in my .zshrc, but then I need
to keep it in sync with my ~/.ssh/config.

Is there no way to get this to work the way I like through some
configuration that I have missed?

Jesper

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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 13:14 Ssh completion from ~/.ssh/config Jesper Nygårds
@ 2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
  2015-04-07 15:53   ` Mikael Magnusson
                     ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Vadim A. Misbakh-Soloviov @ 2015-04-07 14:27 UTC (permalink / raw)
  To: zsh-users

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

```
  # Hostnames completion.
  zstyle -e ':completion:*:hosts' hosts 'reply=(
    ${${${${${(f)"$(<${HOME}/.ssh/known_hosts)"//\[/}//\]:/ }:#[\|]*}%%\ 
*}%%,*}
    ${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*}
    ${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}#*[[:blank:]]}}
  )'
  zstyle ':completion:*:*:*:hosts' ignored-patterns 'ip6*' 'localhost*'


```

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

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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
@ 2015-04-07 15:53   ` Mikael Magnusson
  2015-04-07 18:37   ` Jesper Nygårds
  2015-04-08  6:42   ` Artur Penttinen
  2 siblings, 0 replies; 8+ messages in thread
From: Mikael Magnusson @ 2015-04-07 15:53 UTC (permalink / raw)
  To: Vadim A. Misbakh-Soloviov; +Cc: Zsh Users

On Tue, Apr 7, 2015 at 4:27 PM, Vadim A. Misbakh-Soloviov <mva@mva.name> wrote:
> ```
>   # Hostnames completion.
>   zstyle -e ':completion:*:hosts' hosts 'reply=(
>     ${${${${${(f)"$(<${HOME}/.ssh/known_hosts)"//\[/}//\]:/ }:#[\|]*}%%\
> *}%%,*}
>     ${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*}
>     ${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}#*[[:blank:]]}}
>   )'
>   zstyle ':completion:*:*:*:hosts' ignored-patterns 'ip6*' 'localhost*'
>
>
> ```

You'll probably want these lines inside an if test -f ~/.ssh/config,
as old versions of zsh will abort execution if $(< fails to find the
file. (This bit me once a few years ago).

-- 
Mikael Magnusson


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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
  2015-04-07 15:53   ` Mikael Magnusson
@ 2015-04-07 18:37   ` Jesper Nygårds
  2015-04-08  7:24     ` shawn wilson
  2015-04-08 16:47     ` Bart Schaefer
  2015-04-08  6:42   ` Artur Penttinen
  2 siblings, 2 replies; 8+ messages in thread
From: Jesper Nygårds @ 2015-04-07 18:37 UTC (permalink / raw)
  To: zsh-users

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

Thank you for the tip, Vadim. This solves my problem, in a way.

However, the existing _ssh file already has working code for parsing the
.ssh/config file. Code which is far more readable, and doesn't require me
to parse it myself with that hosts definition. For some reason though,
someone has decided that it is not to be used. I would still prefer it if
there was some way of configuring that I want to use the code that is
already written, and that I prefer .ssh/config over .ssh/known_hosts.


On Tue, Apr 7, 2015 at 4:27 PM, Vadim A. Misbakh-Soloviov <mva@mva.name>
wrote:

> ```
>   # Hostnames completion.
>   zstyle -e ':completion:*:hosts' hosts 'reply=(
>     ${${${${${(f)"$(<${HOME}/.ssh/known_hosts)"//\[/}//\]:/ }:#[\|]*}%%\
> *}%%,*}
>     ${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*}
>     ${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}#*[[:blank:]]}}
>   )'
>   zstyle ':completion:*:*:*:hosts' ignored-patterns 'ip6*' 'localhost*'
>
>
> ```

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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
  2015-04-07 15:53   ` Mikael Magnusson
  2015-04-07 18:37   ` Jesper Nygårds
@ 2015-04-08  6:42   ` Artur Penttinen
  2 siblings, 0 replies; 8+ messages in thread
From: Artur Penttinen @ 2015-04-08  6:42 UTC (permalink / raw)
  To: zsh-users

07.04.2015, 17:34, "Vadim A. Misbakh-Soloviov" <mva@mva.name>:
> ```
>   # Hostnames completion.
>   zstyle -e ':completion:*:hosts' hosts 'reply=(
>     ${${${${${(f)"$(<${HOME}/.ssh/known_hosts)"//\[/}//\]:/ }:#[\|]*}%%\
> *}%%,*}
>     ${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*}
>     ${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}#*[[:blank:]]}}
>   )'
>   zstyle ':completion:*:*:*:hosts' ignored-patterns 'ip6*' 'localhost*'
>
> ```

  ssh_config Host statement can contains many names separated by whitespaces.
  So ${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*} should be
  ${(s. .)${${${(@M)${(f)"$(<${HOME}/.ssh/config)"}:#Host *}#Host }:#*[*?]*}}, i suppose.


-- 
wbw, artur


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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 18:37   ` Jesper Nygårds
@ 2015-04-08  7:24     ` shawn wilson
  2015-04-08 16:47     ` Bart Schaefer
  1 sibling, 0 replies; 8+ messages in thread
From: shawn wilson @ 2015-04-08  7:24 UTC (permalink / raw)
  To: Jesper Nygårds; +Cc: Zsh Users

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

On Apr 7, 2015 2:37 PM, "Jesper Nygårds" <jesper.nygards@gmail.com> wrote:
>

Just an FYI

> I prefer .ssh/config over .ssh/known_hosts.
>

Host *foo
  Identityfile a
Host*bar*
  Identityfile b
Host baz*
  Identityfile c

ssh barfoo
Identityfile would be a, b
ssh bazfoo
Identityfile would be a, c
ssh bazbarfoo
Identityfile would be a, b, c

So if you parse the config for completion date, if I type :
ssh bazbar
You'll need to have both bazbar, and bazbarfoo. The is along with multiple
names being allowed per host.

(The example is obviously useless and whereas most options are first come,
Identityfile is additive which is why I used it here - for simplicity of
the example)

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

* Re: Ssh completion from ~/.ssh/config
  2015-04-07 18:37   ` Jesper Nygårds
  2015-04-08  7:24     ` shawn wilson
@ 2015-04-08 16:47     ` Bart Schaefer
  2015-04-08 17:59       ` Jesper Nygårds
  1 sibling, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2015-04-08 16:47 UTC (permalink / raw)
  To: zsh-users, zsh-users

On Apr 7,  8:37pm, Jesper Nygards wrote:
}
} However, the existing _ssh file already has working code for parsing the
} .ssh/config file. Code which is far more readable, and doesn't require me
} to parse it myself with that hosts definition. For some reason though,
} someone has decided that it is not to be used. I would still prefer it if
} there was some way of configuring that I want to use the code that is
} already written, and that I prefer .ssh/config over .ssh/known_hosts.

Try this:

    zstyle -e ':completion:*:*:ssh:*:my-accounts' users-hosts \
	'[[ -f ~/.ssh/config && $key = hosts ]] && key=my_hosts reply=()'

You can then either define the function _my_hosts which will be called to
add the host completions, or _ssh_hosts will fall through to the code that
reads the .ssh/config files.


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

* Re: Ssh completion from ~/.ssh/config
  2015-04-08 16:47     ` Bart Schaefer
@ 2015-04-08 17:59       ` Jesper Nygårds
  0 siblings, 0 replies; 8+ messages in thread
From: Jesper Nygårds @ 2015-04-08 17:59 UTC (permalink / raw)
  To: zsh-users

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

Thank you Bart, that works perfectly!


On Wed, Apr 8, 2015 at 6:47 PM, Bart Schaefer <schaefer@brasslantern.com>
wrote:

> On Apr 7,  8:37pm, Jesper Nygards wrote:
> }
> } However, the existing _ssh file already has working code for parsing the
> } .ssh/config file. Code which is far more readable, and doesn't require me
> } to parse it myself with that hosts definition. For some reason though,
> } someone has decided that it is not to be used. I would still prefer it if
> } there was some way of configuring that I want to use the code that is
> } already written, and that I prefer .ssh/config over .ssh/known_hosts.
>
> Try this:
>
>     zstyle -e ':completion:*:*:ssh:*:my-accounts' users-hosts \
>         '[[ -f ~/.ssh/config && $key = hosts ]] && key=my_hosts reply=()'
>
> You can then either define the function _my_hosts which will be called to
> add the host completions, or _ssh_hosts will fall through to the code that
> reads the .ssh/config files.
>

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

end of thread, other threads:[~2015-04-08 17:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-04-07 13:14 Ssh completion from ~/.ssh/config Jesper Nygårds
2015-04-07 14:27 ` Vadim A. Misbakh-Soloviov
2015-04-07 15:53   ` Mikael Magnusson
2015-04-07 18:37   ` Jesper Nygårds
2015-04-08  7:24     ` shawn wilson
2015-04-08 16:47     ` Bart Schaefer
2015-04-08 17:59       ` Jesper Nygårds
2015-04-08  6:42   ` Artur Penttinen

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).