在MacOSX上使用Captive Network Assistant连接到VPN

Wol*_*olf 7 vpn autologin osx-lion osx-mountain-lion

Apple推出了MacOSX Lion(10.7)的新功能,称为Captive Network Assistant.在我看来,它只是一个无用而烦人的功能.它的目的是帮助您登录需要身份验证的网络(请参阅:Captive Portal),但该功能不存储cookie或保存密码.

在我的大学里,我们也使用这样一个网络,要求用户登录.登录不是通过浏览器而是通过VPN进行的,这使得强制网络助手完全无用且烦人.

所以我在这里发布了一个关于如何用实际有用的东西替换这个"功能"的指南,我不希望它丢失并对其他人有用

#1.创建名为"Captive Network Assistant"的bash脚本,将以下代码粘贴到内部,并在/ System/Library/CoreServices/Captive Network Assistant.app/Contents/MacOS/中替换您的文件.

#!/bin/bash
scriptlocation="/System/Library/CoreServices/Captive Network Assistant.app/Contents/MacOS/vpn.scpt"
osascript "$scriptlocation"
Run Code Online (Sandbox Code Playgroud)

#2.创建名为"vpn.scpt"的applescript,将其放在bash脚本中提到的路径下,并将以下代码放在:

set wlanssid to do shell script "networksetup -getairportnetwork en1 | cut -c 24-"
connectVPN(wlanssid)

on connectVPN(SSID)
    tell application "System Events"
        tell current location of network preferences
            local VPNService
            if (SSID = "XYZXYZ") then --WLANNAME
                set VPNService to service "XYZXYZ-VPN" --VPNNAME
                set isConnected to connected of current configuration of VPNService
                if not isConnected then
                    connect VPNService
                end if
            end if
        end tell
    end tell
end connectVPN
Run Code Online (Sandbox Code Playgroud)

每次计算机连接到"强制网络"时都会执行此脚本,如果WLAN的SSID称为"XYZXYZ",它将以名称XYZXYZ-VPN启动VPN连接

可以修改脚本以支持多个捕获网络.

还可以在脚本中添加Growl-Notifications.我的完整脚本如下所示:http://pastebin.com/Rtp9EqQR