How to set SSH config to grab part of ssh hostname and use as variable?
I add a fake suffix to the hostname with different proxy hosts
Host *.dc1
ProxyCommand ssh -q %r@dc1 -W %h:%p
Host *.dc2
ProxyCommand ssh -q %r@dc2 -W %h:%p
Then do something like ssh server1.dc1 and it will use the proxy host.
You can add Host entries for custom settings like this:
Host server1.*
User jeff
You can run a script as a custom ProxyCommand and do your works before the real ProxyCommand:
.ssh/config
:
Host *-maria
Hostname maria
User jeff
ProxyCommand /bin/datacenter_ssh.sh %h %p
datacenter_ssh.sh
:
#!/bin/bash
COMBINED=$1
DATACENTER=$(echo $COMBINED | cut -d'-' -f1)
SERVER=$(echo $COMBINED | cut -d'-' -f2)
PORT=$2
ssh -q -W $SERVER:$PORT $DATACENTER
I'm in a similar situation and doing something like this:
# configuration for the datacenter, can add entries for each datacenter
Host datacenter_name
HostName <datacenter_ip>
User <datacenter_user>
IdentityFile <datacenter_keyfile>
# usage match: datacenter_name-server_behind_jumpbox
Host *-*
User <server_behind_jumpbox user>
IdentityFile <server_behind_jumpbox keyfile>
Port <server_behind_jumpbox port>
ProxyCommand ssh $(echo %h | cut -d- -f1) nc $(echo %h | cut -d- -f2) %p"
Of course you could also do something more like you described
# usage match: datacenter_name-maria, specify one for each server
Host *-maria
User <maria user>
IdentityFile <maria keyfile>
Port <maria port>
ProxyCommand ssh $(echo %h | cut -d- -f1) nc $(echo %h | cut -d- -f2) %p"