Skip to content

Instantly share code, notes, and snippets.

@edrex
Last active May 8, 2020 05:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edrex/a1c1325ff469b2ad5c13 to your computer and use it in GitHub Desktop.
Save edrex/a1c1325ff469b2ad5c13 to your computer and use it in GitHub Desktop.
SSH Auto
#!/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
@pangyuteng
Copy link

super useful! Thank you!

@edrex
Copy link
Author

edrex commented May 8, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment