Skip to content

Instantly share code, notes, and snippets.

@zhongl
Created January 14, 2015 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zhongl/932b6c8af9f9ede8a783 to your computer and use it in GitHub Desktop.
Save zhongl/932b6c8af9f9ede8a783 to your computer and use it in GitHub Desktop.
vpn tools for mac osx, origin from http://hongjiang.info/mac-vpn-connection-script/
#!/bin/bash
PROG_NAME=$0
ACTION=$1
DOMAIN="vpnplease.com"
declare -a OPTION=("jp" "us" "sg")
usage() {
echo "Usage: $PROG_NAME {test|conn|close|status}"
exit 1
}
vpn_test() {
for c in "${OPTION[@]}";do
for i in `seq 1 3`; do
proxy="$c$i.$DOMAIN"
ping -c3 $proxy 2>/dev/null | tail -1 | cut -d'=' -f2 | cut -d'/' -f2 | xargs -I {} echo "$proxy " {} 2>/dev/null &
done
done
children=$(jobs -p | xargs)
trap 'kill $children >/dev/null 2>&1' SIGINT SIGTERM
wait
}
get_fastest() {
vpn_test 2>/dev/null | sort -nk2 | head -1 | awk '{print $1}'
}
vpn_status() {
for c in "${OPTION[@]}";do
for i in `seq 1 3`; do
proxy="$c$i.$DOMAIN"
scutil --nc status $proxy 2>/dev/null | head -1 | xargs -I {} echo "$proxy " {}
done
done
}
vpn_connect() {
local name=$1
/usr/bin/env osascript <<-EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "$name" -- your VPN name here
if exists VPN then connect VPN
repeat while (current configuration of VPN is not connected)
delay 1
end repeat
end tell
end tell
EOF
}
vpn_disconnect() {
local name=$(vpn_status | awk '$2~/Connected/{print $1}')
[ -z $name ] && return 0
/usr/bin/env osascript <<-EOF
tell application "System Events"
tell current location of network preferences
set VPN to service "$name" -- your VPN name here
if exists VPN then disconnect VPN
end tell
end tell
return
EOF
}
case "$ACTION" in
test)
vpn_test
;;
conn)
vpn_connect $(get_fastest)
;;
close)
vpn_disconnect
;;
status)
vpn_status
;;
*)
usage
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment