Skip to content

Instantly share code, notes, and snippets.

@edendekker
Last active September 6, 2019 01:16
Show Gist options
  • Save edendekker/3b96a8df9dae9aa1da2609d5b16b3720 to your computer and use it in GitHub Desktop.
Save edendekker/3b96a8df9dae9aa1da2609d5b16b3720 to your computer and use it in GitHub Desktop.
Checks if ipsec connection is established. Restarts it if neccessary.
#!/usr/bin/bash
# Add this script to /etc/crontab
# Set permissions to chmod 100 owner to execute only
#
echo "Keeping alive..."
IPSEC_CONN="iceland-vpn"
IS_UP_IPSEC=$(sudo ipsec status|grep ESTABLISHED|wc -l)
IS_CONNECTING=$(sudo ipsec status|grep "CONNECTING,"|wc -l)
if [[ $IS_CONNECTING -gt 0 ]]
then
echo "IPSEC is connecting."
exit 0
fi
if [[ $IS_UP_IPSEC -eq 1 ]]
then
echo "VPN is UP. Do not need restart VPN tunnel."
else
sudo ipsec down $IPSEC_CONN
sudo ipsec up $IPSEC_CONN
IS_UP_IPSEC=$(sudo ipsec status|grep ESTABLISHED|wc -l)
if [[ $IS_UP_IPSEC -eq 1 ]]
then
echo "VPN is now UP."
else
sudo ipsec down $IPSEC_CONN
sudo ipsec up $IPSEC_CONN
IS_UP_IPSEC=$(sudo ipsec status|grep ESTABLISHED|wc -l)
if [[ $IS_UP_IPSEC -eq 1 ]]
then
echo "VPN is now UP."
else
echo "Failed to restart VPN tunnel."
exit 1
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment