Created
January 14, 2015 17:17
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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