Last active
May 8, 2020 05:47
-
-
Save edrex/a1c1325ff469b2ad5c13 to your computer and use it in GitHub Desktop.
SSH Auto
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Auto-reconnect ssh screen session after sleep + wifi network change | |
# usage: ssh-auto user@server | |
# With these setting SSH will detect a dropped connection after 30 seconds. | |
# Script will attempt to reconnect every 5 seconds whenever connection is dropped. | |
# Use Ctrl-C to exit the reconnect loop. | |
# choose your remote session manager | |
# ON_CONNECT_CMD='screen -DR' | |
ON_CONNECT_CMD='tmux -2 attach || tmux -2 new' | |
while true; do | |
ssh -t -o "ServerAliveInterval 10" -o "ServerAliveCountMax 3" $1 $ON_CONNECT_CMD | |
# SSH exit status is 255 on error, 0 on normal exit | |
if [ $? -eq 0 ]; then | |
exit | |
fi | |
sleep 5 | |
echo "Attempting to reconnect to $1" | |
done |
glad to hear @pangyuteng :)
future version here: https://github.com/edrex/dotfiles/blob/master/bin/ssh-auto
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
super useful! Thank you!