Skip to content

Instantly share code, notes, and snippets.

@delphym
Last active September 13, 2021 04:12
Show Gist options
  • Save delphym/a98a061b9196c39c9aa32ea3156166be to your computer and use it in GitHub Desktop.
Save delphym/a98a061b9196c39c9aa32ea3156166be to your computer and use it in GitHub Desktop.
Control openVPN client
#!/bin/bash
# https://www.aandcp.com/launchdaemons-and-mac-os-x-openvpn-as-an-example
### FURTHER TUNNELING ############################
# You can enable unreachable URLs by
# Pick the host from the URI you can't reach and find its IPs by running, e.g.
# dig vw-bellhop.test.bussys.trimble.com +noall +answer
# Then followed by for each IP address, add the route
# sudo route add 44.236.79.253/32 -interface utun0
#
# To see all your routes run:
# netstat -rn | grep utun0
#
# To delete a route, run, e.g.
# sudo route delete 45.63.3.250/32
##################################################
SWITCH="unload"
#Print acceptable params
helpme() {
echo
echo "`basename $0` help page"
echo "A parameter can be:...[on|ON|start|load] | [off|OFF|stop|unload] | status"
echo "-------------------------------------------------------------------------"
echo "Example calls:"
echo -e "`basename $0` off\n`basename $0` ON\n`basename $0` status"
echo
echo "Turning ON | OFF the Open VPN client requires elevated privileges."
exit 1
} #helpme()
getStatus() {
echo "Determining if Open VPN client is running...."
pgrep -l openvpn
if [ $? -eq 0 ]; then
echo -e "\nOpen VPN client is most likely running... Checking further..."
if [ `ifconfig | grep "inet " | wc -l` == 3 ]; then
echo "Service already loaded:"
else
echo "Service is not running:"
fi
else
echo "Service is not running:"
fi
ifconfig | grep "inet "
echo -e "\nRouting tables for the VPN interface:\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
netstat -rn | grep utun0
exit 0
}
#Parse various arguments
parse_args() {
echo "#########################################################################"
echo "############################### `basename $0` ##############################"
echo "#########################################################################"
case "$1" in
on|ON|start|load)
SWITCH=load ;;
off|OFF|stop|unload)
SWITCH=unload ;;
status)
getStatus ;;
-h|--help|-?|/?|/h)
helpme ;;
*)
echo -e "Invalid argument.\nPrinting HELP information:"; helpme ;;
esac
echo "ARG PARSING DONE."
} #parse_args()
[[ -z $1 ]] && helpme
parse_args $@
sudo -S launchctl $SWITCH /opt/local/openvpn/LaunchDaemons/com.trimblecorp.openvpn.plist
echo "SERVICE has been successfully: "$SWITCH"ed!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment