5

I have the following host definition in my ~/.ssh/config file:

Host testbox
  User abc
  Port 12345
  HostKeyAlias=hello
  HostName 123.123.123.123

The problem is, this host changes IP all the time. The IP is refreshed and stored in this text file ~/ip.txt as it's contents.

Is there a way to configure SSH such that when I run

ssh testbox

I automatically connect to the newest IP read from ~/ip.txt?

(I would prefer not to use dynamic DNS if possible.)

3
  • Instead of witing to ip.txt, you could change your /etc/hosts file and ssh by hostname. – stark Dec 18 '20 at 14:15
  • @stark Thanks for responding, but would prefer a solution that doesn't require root privileges. – Jim Dec 18 '20 at 15:11
  • @Jim: How do you receive new IP address? Instead of storing it in ~/ip.txt you could edit ~/.ssh/config instead. – Arkadiusz Drabczyk Dec 18 '20 at 18:52
6

if you ~/ip.txt can be made to contain

hostname 1.2.3.4

then you can

include ~/ip.txt

at the appropriate place in the ~/.ssh/config.

3

IIUC, you could use ProxyCommand like that:

Host test
Port 12345
ProxyCommand ssh -p %p "$(cat ~/ip.txt)" nc localhost %p
StrictHostKeyChecking no
6
  • This looks promising. But it's not working. I tried: ``` Host testbox User abc Port 12345 ProxyCommand ssh "$(cat ~/ip.txt)" nc localhost %p ``` StrictHostKeyChecking makes no difference in whether it works or not. I tried using the IP directly instead of the $(), in case it was something wrong with the cat part and it doesn't work either. Can you explain the nc localhost %p part? – Jim Dec 20 '20 at 16:04
  • @Jim: see updated answer. nc is used to forward SSH traffic as explained at en.wikibooks.org/wiki/OpenSSH/Cookbook/Proxies_and_Jump_Hosts. Notice that it has to be installed on the server. – Arkadiusz Drabczyk Dec 20 '20 at 18:45
  • I needed to add %r to the command as well in order to use the right username. But it's still not working: "Warning: untrusted X11 forwarding setup failed: xauth key data not generated kex_exchange_identification: Connection closed by remote host" This error occurs even if I use "ForwardX11 no" – Jim Dec 21 '20 at 5:28
  • @Jim: can you try ProxyCommand ssh -l %r -p %p "$(cat ~/ip.txt)" nc localhost 22? – Arkadiusz Drabczyk Dec 21 '20 at 11:00
  • This works! A couple of questions: 1) I don't think I need StrictHostKeyChecking=no if I use HostKeyAlias? 2) Why port 22 specifically? I also tried a different port and it didn't work. – Jim Dec 22 '20 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.