zsh-workers
 help / color / mirror / code / Atom feed
* zsh does not offer ssh host from config
@ 2022-12-09  1:06 Jan Palus
  2022-12-10 12:01 ` Daniel Shahaf
  2022-12-10 12:08 ` Roman Perepelitsa
  0 siblings, 2 replies; 6+ messages in thread
From: Jan Palus @ 2022-12-09  1:06 UTC (permalink / raw)
  To: zsh-workers

Hi,

I've got a single real hostname starting with 'build' prefix which is present
in my known_hosts file. I also have a couple Host specifications in ssh config
file having names starting with 'build' but real HostName does not resolve to
name starting with 'build'. When doing `ssh build<tab>` I'd expect to be
offered both real hostname as well as Host specification names, however only
real hostname is immediately completed. That's because of _ssh_hosts returning
on successful hosts completion:

# 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 # <--- HERE
fi

Is this intentional that config file parsing is skipped entirely if host was
completed successfully?


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

* Re: zsh does not offer ssh host from config
  2022-12-09  1:06 zsh does not offer ssh host from config Jan Palus
@ 2022-12-10 12:01 ` Daniel Shahaf
  2022-12-10 12:08 ` Roman Perepelitsa
  1 sibling, 0 replies; 6+ messages in thread
From: Daniel Shahaf @ 2022-12-10 12:01 UTC (permalink / raw)
  To: Jan Palus; +Cc: zsh-workers

Jan Palus wrote on Fri, Dec 09, 2022 at 02:06:50 +0100:
> Hi,
> 
> I've got a single real hostname starting with 'build' prefix which is present
> in my known_hosts file. I also have a couple Host specifications in ssh config
> file having names starting with 'build' but real HostName does not resolve to
> name starting with 'build'. When doing `ssh build<tab>` I'd expect to be
> offered both real hostname as well as Host specification names, however only
> real hostname is immediately completed. That's because of _ssh_hosts returning
> on successful hosts completion:
> 
> # 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 # <--- HERE
> fi
> 
> Is this intentional that config file parsing is skipped entirely if host was
> completed successfully?

That «return» was added in https://www.zsh.org/users/11333 with the
intention of skipping config file parsing if a match was found /in the
users-hosts style/.  However, in a quick test with these fictional
values:

[[[
% zstyle -L \* users-hosts; echo $?
1
% tail -2 ~/.ssh/config
Host completion-test1.zsh.org
        HostName 2001:db8::zsh
% tail -1 ~/.ssh/known_hosts
completion-test2.zsh.org,2001:db8::zsh x-not-a-real-identifier abcdef01
% 
]]]

I observe that «ssh completion<TAB>» only offers completion-test2.zsh.org,
not completion-test1.zsh.org.  I take it both should be offered (since
users-hosts is unset).


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

* Re: zsh does not offer ssh host from config
  2022-12-09  1:06 zsh does not offer ssh host from config Jan Palus
  2022-12-10 12:01 ` Daniel Shahaf
@ 2022-12-10 12:08 ` Roman Perepelitsa
  2022-12-10 20:06   ` Jan Palus
  1 sibling, 1 reply; 6+ messages in thread
From: Roman Perepelitsa @ 2022-12-10 12:08 UTC (permalink / raw)
  To: Jan Palus; +Cc: zsh-workers

On Fri, Dec 9, 2022 at 2:07 AM Jan Palus <jpalus@fastmail.com> wrote:
>
> Hi,
>
> I've got a single real hostname starting with 'build' prefix which is present
> in my known_hosts file. I also have a couple Host specifications in ssh config
> file having names starting with 'build' but real HostName does not resolve to
> name starting with 'build'. When doing `ssh build<tab>` I'd expect to be
> offered both real hostname as well as Host specification names, however only
> real hostname is immediately completed.

If you only ever ssh to hosts listed in the ssh config, you can
configure ssh completions to respect that:

    zstyle ':completion:*:(ssh|scp):*:hosts' hosts

Listing all remote machines in the ssh config has other advantages
besides making completions more predictable.

Roman.


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

* Re: zsh does not offer ssh host from config
  2022-12-10 12:08 ` Roman Perepelitsa
@ 2022-12-10 20:06   ` Jan Palus
  2022-12-10 20:31     ` Roman Perepelitsa
  0 siblings, 1 reply; 6+ messages in thread
From: Jan Palus @ 2022-12-10 20:06 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh-workers

On 10.12.2022 13:08, Roman Perepelitsa wrote:
> On Fri, Dec 9, 2022 at 2:07 AM Jan Palus <jpalus@fastmail.com> wrote:
> >
> > Hi,
> >
> > I've got a single real hostname starting with 'build' prefix which is present
> > in my known_hosts file. I also have a couple Host specifications in ssh config
> > file having names starting with 'build' but real HostName does not resolve to
> > name starting with 'build'. When doing `ssh build<tab>` I'd expect to be
> > offered both real hostname as well as Host specification names, however only
> > real hostname is immediately completed.
> 
> If you only ever ssh to hosts listed in the ssh config, you can
> configure ssh completions to respect that:
> 
>     zstyle ':completion:*:(ssh|scp):*:hosts' hosts
> 
> Listing all remote machines in the ssh config has other advantages
> besides making completions more predictable.

In my case it's much more practical to ie use pattern in ssh config for
all the test envs instead of listing plenty of them explicitly.

It's nice to have a possibility to customize behavior but I'd say more
sensible default would be to offer both. Or in other words current
default behavior is somewhat counter-intuitive. Doing "build<tab>"
offers single completion, while doing more specific "builder<tab>"
offers 3. Intuitively with every char completion options should be
narrowed (or remain the same) but should not be broadened.


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

* Re: zsh does not offer ssh host from config
  2022-12-10 20:06   ` Jan Palus
@ 2022-12-10 20:31     ` Roman Perepelitsa
  2022-12-10 20:49       ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Roman Perepelitsa @ 2022-12-10 20:31 UTC (permalink / raw)
  To: Jan Palus; +Cc: zsh-workers

On Sat, Dec 10, 2022 at 9:06 PM Jan Palus <jpalus@fastmail.com> wrote:
>
> On 10.12.2022 13:08, Roman Perepelitsa wrote:
> >
> > If you only ever ssh to hosts listed in the ssh config, you can
> > configure ssh completions to respect that:
> >
> >     zstyle ':completion:*:(ssh|scp):*:hosts' hosts
> >
> > Listing all remote machines in the ssh config has other advantages
> > besides making completions more predictable.
>
>
> It's nice to have a possibility to customize behavior but I'd say more
> sensible default would be to offer both.

Oh, I'm not arguing in favor of the current implementation. I just
thought it might be useful to mention the setup I've made for myself
that works decently well.

> In my case it's much more practical to ie use pattern in ssh config for
> all the test envs instead of listing plenty of them explicitly.

I used to do that but then switched to my current setup where the ssh
config contains all remote machines (I generate it with a script). I
really like the ability to list all machines with a TAB. That said, I
can imagine environments where this kind of setup isn't possible.

Roman.


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

* Re: zsh does not offer ssh host from config
  2022-12-10 20:31     ` Roman Perepelitsa
@ 2022-12-10 20:49       ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2022-12-10 20:49 UTC (permalink / raw)
  To: zsh-workers; +Cc: Jan Palus

On Sat, Dec 10, 2022 at 12:32 PM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> [...] switched to my current setup where the ssh
> config contains all remote machines (I generate it with a script). I
> really like the ability to list all machines with a TAB.

I've started using this:

zstyle -e ':completion:*:(ssh|scp):*' hosts 'reply=(
    ${(Mu)${(M)=history:#ssh*}:#[^/]##.*} \
    ${=${${(f)"$(cat {/etc/ssh_,~/.ssh/known_}hosts(|2)(N) \
                     /dev/null)"}%%[#| ]*}//,/ }
    ${=${(f)"$(cat /etc/hosts(|)(N) <<(ypcat hosts 2>/dev/null))"}%%\#*}
    )'

That retrieves
 - all hosts mentioned in ssh commands in the current shell history
 - all hosts in known_hosts and known_hosts2 (ignoring hashed hostnames)
 - anything available from the local and network host configurations

My current complaint is that the hosts style is used in preference to
the my-accounts style, so I can't complete user names that are
prefixes of host names.


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

end of thread, other threads:[~2022-12-10 20:49 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-09  1:06 zsh does not offer ssh host from config Jan Palus
2022-12-10 12:01 ` Daniel Shahaf
2022-12-10 12:08 ` Roman Perepelitsa
2022-12-10 20:06   ` Jan Palus
2022-12-10 20:31     ` Roman Perepelitsa
2022-12-10 20:49       ` Bart Schaefer

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