Using Only Part Of The Template In SSH Config Host Name

I have an SSH config like the one below that works great for:

ssh b21 ssh 23 ssh s267 

Example .ssh / config (hostname is numbered in% h):

 host s* HostName atrcu%h.example.com User example Port 22 host b* HostName atrcx%h.example.com User example Port 22 host ??* HostName atvts%h.example.com User example Port 2205 

but I would like to specify the username in the host:

 ssh b21ex 

which will be ssh:

 example@atvts21.example.com 

but instead will be:

 atvts21ex.example.com 

Is this any way to cut / modify% h as it goes through, and possibly link the connection to more templates to get a username along the way?

+8
unix ssh openssh
source share
2 answers

I think you will need to create separate HostName/User entries for every possible abbreviation match. Then you can use %r to access separate authentication files for each user. This will allow you to skip using user@host to log in by creating a more complex configuration file.

You may be lucky to write a script or shell alias that destroys your shortcuts and pulls them to ssh ready to go.

0
source share

You can do what you describe in the examples using the match spec instead of host . This is another way to specify a host or set of hosts.

For example:

 Match user u* host t* Hostname %hest.dev User %r 

This will match the user pattern and the target host pattern.

In this case, the command line will look something like ssh u@t , which will lead to such a replacement: u@test.dev .

Here is a snippet from ssh debug output:

 debug2: checking match for 'user u* host t*' host t originally t debug3: /Users/_/.ssh/config line 2: matched 'user "u"' debug3: /Users/_/.ssh/config line 2: matched 'host "t"' debug2: match found ... debug1: Connecting to test.dev port 22. debug1: Connection established. ... u@test.dev password: 

match may coincide with a couple of other things (and it is possible to execute a shell command), but otherwise it is the same as host . The ssh client parameters specified there are the same (for example, you can specify the IdentityFile , which will be used with the corresponding specification)

You can read more on the manual page: ssh_config

0
source share

All Articles