Skip to content

Instantly share code, notes, and snippets.

@wwwins
Last active April 19, 2019 07:31
Show Gist options
  • Save wwwins/1d83eb1583fdfa524f745a60e1601618 to your computer and use it in GitHub Desktop.
Save wwwins/1d83eb1583fdfa524f745a60e1601618 to your computer and use it in GitHub Desktop.
setup reverse ssh tunnel with cron
#!/bin/bash
#
# add the following job into crontab
# */5 * * * * /usr/local/bin/autossh.sh
IP="1.1.1.1"
/bin/ping -c 3 $IP > /dev/null
if [ $? != 0 ]
then
exit 1
fi
#SSH_COMMAND="ssh -f -N -R 12322:localhost:22 -p 2222 [email protected]"
SSH_COMMAND="ssh -f -N -R 12322:localhost:22 -i $HOME/.ssh/id_rsa [email protected]"
cnt=$(ps -aux | grep -v "grep" | grep -c "$SSH_COMMAND")
if [[ $cnt < 1 ]]
then exec $SSH_COMMAND
fi
cat << EOF >> ~/.ssh/config
Host *
ServerAliveInterval 30
ServerAliveCountMax 5
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment