Discussion:
[PATCH] Expand %n in User parameters
Tobias Umbach
2018-09-29 22:41:28 UTC
Permalink
This patch adds expansion of the %n token to "User"-parameters in ssh_config
files, allowing configurations like:

  Host service1 service2
      HostName services.example.com
  Host service1
      User service1
  Host service2
      User service2

... to be expressed more tersely:

  Host service1 service2
    HostName services.example.com
    User %n
---
 ssh.c        | 7 ++++++-
 ssh_config.5 | 3 +++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/ssh.c b/ssh.c
index 849fae35..3202697d 100644
--- a/ssh.c
+++ b/ssh.c
@@ -1266,8 +1266,13 @@ main(int ac, char **av)
        seed_rng();
-       if (options.user == NULL)
+       if (options.user == NULL) {
                options.user = xstrdup(pw->pw_name);
+       } else {
+               cp = options.user;
+               options.user = percent_expand(cp, "n", host_arg, (char
*)NULL);
+               free(cp);
+       }
        /* Set up strings used to percent_expand() arguments */
        if (gethostname(thishost, sizeof(thishost)) == -1)
diff --git a/ssh_config.5 b/ssh_config.5
index 27136dbd..1f0c368c 100644
--- a/ssh_config.5
+++ b/ssh_config.5
@@ -1761,6 +1761,9 @@ accepts the tokens %%, %C, %h, %i, %L, %l, %n, %p,
%r, and %u.
 .Cm HostName
 accepts the tokens %% and %h.
 .Pp
+.Cm User
+accepts the tokens %% and %n.
+.Pp
 .Cm IdentityAgent
 and
 .Cm IdentityFile

Loading...