1

I have a crapboat of less-than-new computers that I've been hooking up to a small compute cluster because why not. I have a single master node that has a public IP address that these computers can all talk to. The worker nodes are behind a NAT. So, in order to converse with them all, I have them all create a reverse ssh tunnel into the master node on boot. Thus, they are each connected to the master at a different port. Each machine chooses a port n+1 where n is the previous machine's port number. In my ssh config on my laptop, I have the following:

Host worker*
    ProxyCommand ssh master -W %h:%p
    HostName localhost

Host worker001
    Port 7001
Host worker002
    Port 7002
....
#Host workerX
#    Port 7000 + X

What I would like to do, is to have a single entry for my workers, so that workerX uses port 7000 + X. Is this possible with ssh config? If not, is there some other workaround to have this happen? Alternatively, can I "split" my ssh config, so that my ~/.ssh/config file doesn't have a hundred lines of workerX, and I can confine all of that mess?

Him
  • 366
  • 3
  • 14

1 Answers1

2

You're using ProxyCommand, so just write a custom command that does this:

ProxyCommand ~/bin/worker-ssh %h

Where the script itself might look like:

#!/usr/bin/env bash
host=$1
if [[ $host =~ ^worker0*([0-9]+)$ ]]; then
    port=${BASH_REMATCH[1]}
    port=$(( port + 7000 ))
    ssh master -W "localhost:$port"
else
    echo "error: $0: host '$host' is not a worker" >&2
    exit 1
fi

(Do not use HostName with this; the proxy command will obviously need to know the original host name.)

u1686_grawity
  • 452,512
  • Consider mentioning that ssh_config supports the Include directive to answer the "Alternatively" part of the question. – Ginnungagap Aug 20 '19 at 21:11