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

Re: Completing from different ssh config file



Chris Johnson <cjohnson@xxxxxxxxxx> wrote:
> > I use the following function to log in to certain hosts:
> > 
> > foo() {
> >    ssh -i ~/.ssh/id_rsa.foo \
> >        -F ~/.ssh/config.foo \
> >        "$*"
> > }
> > 
> > How would I configure the completion system so
> > that the hostname is completed from
> > ~/.ssh/config.foo when I use this function?
> > Right now the hostname isn't completed at all
> > because the context isn't recognized as a call to
> > ssh.
> 
> For this last point, try:
> 
>    compdef foo=ssh
> 
> This causes zsh to employ ssh completion for foo.  This doesn't really
> solve your problem, but I thought I'd share.  (Unless the completion for
> ssh recognizes the -F option, in which case an alias may be more
> appropriate?  I dunno.)

_ssh doesn't recognize the -F option at the moment; this fixes that.
Then an alias certainly ought to do the trick.

alias foo='ssh -i ~/.ssh/id_rsa.foo -F ~/.ssh/config.foo'

I could add a style to pick the config file, but even that's not really
quite what's needed here, and isn't of much general use since ssh either
uses a fixed file or what's on the command line.

Index: Completion/Unix/Command/_ssh
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Unix/Command/_ssh,v
retrieving revision 1.29
diff -u -r1.29 _ssh
--- Completion/Unix/Command/_ssh	3 Oct 2006 16:24:51 -0000	1.29
+++ Completion/Unix/Command/_ssh	14 Nov 2006 11:53:15 -0000
@@ -323,6 +323,8 @@
 
 _ssh_hosts () {
   local -a config_hosts
+  local config
+  integer ind
 
   if [[ "$IPREFIX" == *@ ]]; then
     _combination -s '[:@]' my-accounts users-hosts "users=${IPREFIX/@}" hosts "$@"
@@ -330,7 +332,12 @@
     _combination -s '[:@]' my-accounts users-hosts \
       ${opt_args[-l]:+"users=${opt_args[-l]:q}"} hosts "$@"
   fi
-  if [[ -r "$HOME/.ssh/config" ]]; then
+  if (( ind = ${words[(I)-F]} )); then
+    config=$words[ind+1]
+  else
+    config="$HOME/.ssh/config"
+  fi
+  if [[ -r $config ]]; then
     local IFS=$'\t ' key hosts host
     while read key hosts; do
       if [[ "$key" == (#i)host ]]; then
@@ -341,7 +348,7 @@
 	    esac
 	 done
       fi
-    done < "$HOME/.ssh/config"
+    done < "$config"
     if (( ${#config_hosts} )); then
       _wanted hosts expl 'remote host name' \
 	compadd -M 'm:{a-zA-Z}={A-Za-z} r:|.=* r:|=*' "$@" $config_hosts



To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php



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