Bug 3143

Summary: Add the ability to capture wildcard portion of Host keyword in ssh_config
Product: Portable OpenSSH Reporter: Lavneet <lavneetpuri>
Component: sshAssignee: Assigned to nobody <unassigned-bugs>
Status: NEW ---    
Severity: enhancement CC: dtucker
Priority: P5    
Version: -current   
Hardware: Other   
OS: Linux   

Description Lavneet 2020-03-31 06:10:46 AEDT
Use Case:
`ssh pc-1` would connect to remote-cpu-1.domain.com

Currently we can use `%h` token but it captures the whole argument to Host keyword (i.e pc-1). This feature would enable capturing just the pc number (ie. 1) by using a new `%m` token for example. It is useful to simplify connection to complex ssh hostnames

For the above use-case, the ssh config can be specified as follows:
Host pc-*
  Hostname remote-cpu-%m.domain.com
Comment 1 Darren Tucker 2020-03-31 12:18:37 AEDT
There's a couple of problems with this:
 - the current matching code doesn't lend itself to supporting this kind of backreference
 - it does however currently support multiple wildcards (eg "*-*-?.domain.*") which don't map well onto the token you suggested.  It'd probably need to be regex-style digits %1 - %9.
 - "Host" specifically supports multiple matches.  You could have, eg, "Host pc-? *-* *.*.*", at which point you could have 1, 2 or 3 backrefs.  In that case, what does "%3" evaluate to and when?

I'm not sure it's useful enough to be worth the extra complexity in the parser, which is already pretty complex.
Comment 2 Lavneet 2020-04-02 01:04:14 AEDT
I ended up writting a wrapper script to account for this special case - one that is not very neat - so I realize the complexity this may add to the parser. However, in terms of usefulness, ssh configs are among the best features that boost productivity. I wonder if this logic (and more advanced parsing feature to follow) could be offloaded from the main build - as a plugin of sorts. I'm not aware if this has been done before for any other components of the program.