标签归档:vpn

vpn脚本的改进

感谢巨石的验证和建议,之前小看了云梯vpn的规模,以为他们只有那么几台服务器,所以在脚本里把域名写死了;后来发现不同用户的服务域名还是不一样的,改进了一下脚本,不局限于云梯的vpn,适用所有的vpn;代码在这里

安装方式:

$ mkdir -p ~/bin
$ curl -s http://hongjiang.info/vpn.sh > ~/bin/vpn

选项:

$ vpn
Usage: /Users/hongjiang/bin/vpn {list|ping|optimal|conn|close|status}

自动连接:

$ vpn conn
ok, jp3.vpnplease.com connected.

可以指定连接协议:

$ vpn conn L2TP #或PPTP
ok, jp2.vpnplease.com connected.

断开连接:

$ vpn close
ok, jp3.vpnplease.com disconnected.

查看vpn列表(名称:地址):

$ vpn list
VPN (PPTP): jp2.vpnplease.com
jp3.vpnplease.com: jp3.vpnplease.com
us1.vpnplease.com: us1.vpnplease.com
...

查看所有vpn的状态:

$ vpn status

* (Disconnected)   2599A4CB-7D76-4878-8FAF-7496E5F6C58F PPP --> L2TP       "us2.vpnplease.com"              [PPP:L2TP]
* (Disconnected)   57A3276C-505F-4E13-B402-0CB6EC5C9AD7 PPP --> PPTP       "VPN (PPTP)"                     [PPP:PPTP]
* (Connected)      90784ECA-DCFA-4197-B2E5-FBE84AFB9C00 PPP --> L2TP       "jp3.vpnplease.com"              [PPP:L2TP]
...     

获取最优的服务名称(协议参数可选):

$ vpn optimal pptp  #或 l2tp
VPN (PPTP)

测试连接速度:

$ vpn ping
jp2.vpnplease.com  52.190
jp1.vpnplease.com  53.681
sg2.vpnplease.com  171.482
...

选择网速最快的vpn进行连接的脚本

下午github上下载速度只有几K,用了代理也不到10K,后来发现所用的vpn服务器选择了一个慢的。写了这段脚本,用来从可用的vpn选出最快的那个。脚本只适用于mac,并且前提是你已创建好了所有可选的vpn,如图:

脚本内容:

#!/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

使用方式:

1) 自动选择最优的vpn进行连接

$ vpn conn

2) 查看连接状态

$ vpn status

jp1.vpnplease.com  Connected
jp2.vpnplease.com  Disconnected
jp3.vpnplease.com  Disconnected
us1.vpnplease.com  Disconnected
us2.vpnplease.com  Disconnected
us3.vpnplease.com  Disconnected
sg1.vpnplease.com  Disconnected
sg2.vpnplease.com  Disconnected

3) 测试网速,时间最小的最优

$ vpn test

jp2.vpnplease.com  76.314
jp1.vpnplease.com  75.793
jp3.vpnplease.com  136.161
sg1.vpnplease.com  169.494
us3.vpnplease.com  242.685
us2.vpnplease.com  249.032
us1.vpnplease.com  266.427
sg2.vpnplease.com  168.750

4) 断开连接

$ vpn close