I'm using ssh as a SOCKS5 proxy, connecting to my personal server. When my system is suspended or I lose internet connection, the ssh will eventually time out, forcing me to manually reconnect. How can I configure my system to automatically reconnect as soon as an internet connection is established?
I used to have a "reverse ssh" script like
laddr=*:0
host=some_user@some.host.com
port=some_port
while :; do
t0=$(date +%s)
ssh -R "$laddr:localhost:22" \
-o BatchMode=yes \
-o CheckHostIP=no \
-o ExitOnForwardFailure=yes \
-o ServerAliveInterval=15 \
-o ConnectTimeout=15 \
-p "$port" "$host"
t1=$(date +%s)
sleep "$((60 / (t1 - t0 + 1)))"
done
which was supposed to try to connect immediately after the connection was lost, but wait a minute if ssh was terminating immediately, in order not to hammer the server or run a wild loop.
But I have long time ago replaced it with something better -- "service"-like.
Systemd & co are probably offering similar features.
while ping -c 1 remotehost; do ssh remoteuser@remotehost; done
does not work? – Jetchisel Mar 8 '20 at 5:45autossh
... – 0xC0000022L Mar 8 '20 at 10:04