Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using only part of a pattern in SSH Config Hostname

Tags:

unix

ssh

openssh

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

ssh b21
ssh 23
ssh s267

Example .ssh/config (hostname passed is slotted in at %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'd like to include the username in the host:

ssh b21ex

which would ssh to:

[email protected]

but instead will:

atvts21ex.example.com

is their any way to cut/modify %h as it's passed and perhaps have the connection match more patterns to get a username along the way?

like image 511
dueyfinster Avatar asked Jun 18 '13 12:06

dueyfinster


1 Answers

You can do what you describe in the examples with the match specification instead of host. It's another way to specify a host, or a set of hosts.

For example:

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

This will match against a user pattern and a target host pattern.

The command line will then be something like ssh u@t, resulting in this substitution: [email protected].

Here's a snippet from the 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.
...
[email protected]'s password:

match can match against a couple of other things (and there's an option to execute a shell command) but otherwise it's just like host. The ssh client options that go in there are the same ones (e.g. you can specify and IdentityFile that will be used with the matching spec)

You can read more in the man page: ssh_config

like image 74
kln Avatar answered Oct 22 '22 02:10

kln