Skip to content

Instantly share code, notes, and snippets.

@nullne
Created October 9, 2015 03:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nullne/2c536ee95295a85a33f9 to your computer and use it in GitHub Desktop.
Save nullne/2c536ee95295a85a33f9 to your computer and use it in GitHub Desktop.
自动选择最佳vpn线路并连接
#!/bin/bash
PROG_NAME=$0
ACTION=$1
usage() {
echo "Usage: $PROG_NAME {list|ping|optimal|conn|close|status}"
exit 1
}
get_services() {
local protocol=${1:-"L2TP|PPTP|IPSec"}
scutil --nc list | grep -iE "$protocol" | awk -F'"' '{print $2}'
}
get_service_addr() {
local service=$1
scutil --nc show "$service" | awk '$1~/RemoteAddress/{print $3}'
}
vpn_list() {
local protocol=$1
IFS=$'\n' && declare -a services=( `get_services $protocol` )
for service in "${services[@]}";do
addr=$(get_service_addr $service)
echo "$service: $addr"
done
}
vpn_test() {
local protocol=$1
declare -a hosts=()
IFS=$'\n' && declare -a services=( `get_services $protocol` )
for service in "${services[@]}";do
host=$(scutil --nc show "$service" | awk '$1~/RemoteAddress/{print $3}')
[[ "${hosts[*]}" =~ "$host" ]] || hosts+=("$host")
done
for h in "${hosts[@]}";do
ping -c3 $h 2>/dev/null | tail -1 | cut -d'=' -f2 | cut -d'/' -f2 | xargs -I {} echo "$h " {} 2>/dev/null &
done
children=$(jobs -p | xargs)
trap 'kill $children >/dev/null 2>&1' SIGINT SIGTERM
wait
}
get_protocol() {
local service=$1
scutil --nc show "$service" | awk -F':|]' 'NR==1{print $(NF-1)}'
}
get_fastest_service() {
local protocol=$1
local host=$(vpn_test $protocol 2>/dev/null | sort -nk2 | head -1 | awk '{print $1}')
if [ -z "$protocol" ];then
vpn_list | grep "$host" | cut -d':' -f1 | head -1
else
vpn_list $protocol | grep "$host" | cut -d':' -f1
fi
}
to_upper() {
echo ${@^^}
}
vpn_status() {
which ack >/dev/null 2>&1
if [ $? -eq 0 ];then
scutil --nc list | grep -E "L2TP|PPTP|IPSec" | ack --passthru Connected
else
scutil --nc list | grep -E "L2TP|PPTP|IPSec"
fi
}
vpn_connect() {
local service=$1
scutil --nc start "$service"
# /usr/bin/env osascript <<-EOF
# tell application "System Events"
# tell current location of network preferences
# set VPN to service "$service" -- 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
}
get_connected_service() {
vpn_status | awk '$2~/Connected/' | awk -F'"' '{print $2}'
}
vpn_disconnect() {
local service=$1
scutil --nc stop "$service"
# /usr/bin/env osascript <<-EOF
# tell application "System Events"
# tell current location of network preferences
# set VPN to service "$service" -- your VPN name here
# if exists VPN then disconnect VPN
# end tell
# end tell
# return
# EOF
}
case "$ACTION" in
ping|test)
vpn_test
;;
optimal)
shift
get_fastest_service $@
;;
list)
vpn_list
;;
conn)
shift && service="$(get_fastest_service $@)"
vpn_connect "$service"
[ $? -eq 0 ] && echo "ok, $service connected."
;;
close)
service=$(get_connected_service)
[ -z "$service" ] && echo "not connected to any service." && exit 0
vpn_disconnect "$service"
[ $? -eq 0 ] && echo "ok, $service disconnected."
;;
status)
vpn_status
;;
*)
usage
;;
esac
@nullne
Copy link
Author

nullne commented Oct 9, 2015

完善 选择网速最快的vpn进行连接的脚本,他的脚本写的非常好
不过系统更新到MAC OSX EI Cpitan 之后出了点问题,稍做更改。支持IPSec
因为对这些语言不是很熟悉,所以可能会有一些bug,请指出

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment