Skip to content

Instantly share code, notes, and snippets.

@Groverkss
Last active October 19, 2021 11:05
Show Gist options
  • Save Groverkss/248c94c3655f8ab89f47bac5f0471f4c to your computer and use it in GitHub Desktop.
Save Groverkss/248c94c3655f8ab89f47bac5f0471f4c to your computer and use it in GitHub Desktop.
Script to connect to openvpn server
#!/bin/bash
# Change file names here
PRIMARY=primary.ovpn
BACKUP=backup.ovpn
# Trap signal handler
trap_end() {
sudo rm .tmp-pass
exit
}
# Ping server give as arguemtn to check for internet connectivity
check_connection() {
# $1 --> Server to ping
echo $host
if !(ping -q -c 1 -W 1 $1 >/dev/null); then
# If ICMP is blocked, try alternate ways
if !(nc -zw1 $1 443); then
return 1
fi
fi
return 0
}
# Check internet connection by pinging Google DNS server
check_internet_connection() {
if !(check_connection 8.8.8.8); then
echo "Internet connection is down"
trap_end
fi
echo
}
# Check iiit connection by pinging intranet and mess
# mess --> 10.4.20.179
# intranet --> 10.4.21.84
# Replaced with ip's incase dns fails/takes too much time
check_iiit_connection() {
if !(check_connection 10.4.21.84); then
if !(check_connection 10.4.20.179); then
return 1
fi
fi
return 0
}
# Check connection at start
check_internet_connection
# Ask for super user access to script from start
# so as to not prompt if primary server fails
# and temporary server needs to be restarted
if [[ $(whoami) != "root" ]]; then
echo "Try again with sudo access"
exit
fi
# Kill script on signal by user
trap trap_end SIGINT
trap trap_end SIGTERM
# Read username and password
read -p "Username: " username
read -s -p "Password: " password
# Newline after entering password
echo
# Store username and pass to a file so as to be not prompted
# incase primary server fails
echo $username > .tmp-pass
echo $password >> .tmp-pass
chmod 400 .tmp-pass
# If the user presses ^C, the script exits by the trap handler
# If the user didnt press ^C and a command fails, the command next
# in line will be executed
# Launch Primary Server
echo "Connecting to Primary Server"
openvpn --config $PRIMARY --auth-user-pass .tmp-pass > /dev/null &
primary_pid=$!
# Poll every 20 sec to check if can still connect to iiit network
while true; do
sleep 20
# Check if still connected to internet
check_connection
# Check if connected to iiit network
if !(check_iiit_connection); then
echo "Cannot connect to Primary link"
kill -9 $primary_pid
break
fi
done
# Primary Server failed. Use Temporary Server
echo "Connecting to Backup Server"
openvpn --config $BACKUP --auth-user-pass .tmp-pass > /dev/null &
backup_pid=$!
# Same as previous check
while true; do
sleep 20
# Check if still connected to internet
check_connection
# Check if connected to iiit network
if !(check_iiit_connection); then
echo "Cannot connect to Temporary Link"
kill -9 $backup_pid
break
fi
done
# Inform of failure
echo "Both Servers failed"
quit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment