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 email@example.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.