Kleine Pimmel Scripts


#!/bin/bash
#
##
# Find current default gateway via: ip route | grep default
#
# Note: VPN interface will be prefixed with 'ovpn'!
#
##
# Settings:
##
_vpn_gw=192.168.199.1
_vpn_iface=ovpn
_vpn_conf=/root/Client.ovpn
##
# Functions
##
_vpn_cmd="openvpn --config ${_vpn_conf} --redirect-private --dev ${_vpn_iface} --daemon"

function _ipdetails {
  echo "External IPv4 is: $(curl -s -4 ifconfig.co/)"
  echo "External IPv6 is: $(curl -s -6 ifconfig.co/)"
  echo "${_vpn_iface} IP is: $(ip -f inet addr show ${_vpn_iface} | sed -En -e 's/.*inet ([0-9.]+).*/\1/p')"
}
##
# Main
##
if ping -W 1 -c 1 ${_vpn_gw} &> /dev/null
then
  echo "Already connected."
  _ipdetails
else
  pkill -f "${_vpn_cmd}"
  sleep 2
  set -e
  echo "Connecting..."
  ${_vpn_cmd}
  _ipdetails
fi