Mac VPN自动连接脚本(AnyConnect)

 3周前     27  

文章目录

公司要求连接机房服务器需要通过vpn进行访问,不同机房之间使用不同的vpn,每次切换都需要手动填写信息,有点啰嗦~

So,通过Mac的applescript实现自动化操作,再配合Alfred4,简直神了~

AppleScript代码如下:

--变量
set VPNServerList to {"1.XXX_VPN","2.XXX_VPN"}
set VPNServer to choose from list VPNServerList with title "服务器列表" with prompt "请选择需要连接的服务器." OK button name "连接" cancel button name "取消" default items {"1.vpn.gmtgo.com:3443"}
if VPNServer is not false then
  if VPNServer contains "1.XXX_VPN" then
    set VPNHost to "vpn.xxx.com"
    set VPNGroup to "ops"
    set VPNUsername to "cgq"
    set VPNPassword to "123456"
  else if VPNServer contains "2.XXX_VPN" then
    set VPNHost to "vpn.xxx.com"
    set VPNGroup to "dev"
    set VPNUsername to "root"
    set VPNPassword to "123456"
  end if

tell application "Cisco AnyConnect Secure Mobility Client"
    activate
end tell
  
set VPNRunning to false
repeat 10 times
    if application "Cisco AnyConnect Secure Mobility Client" is running then
        tell application "System Events"
            tell process "Cisco AnyConnect Secure Mobility Client"
                tell window 2
                    if exists button "Disconnect" then
                        click button "Disconnect"
                        delay 2
                    end if
                    if exists button "Connect" then
                        if exists combo box 1 then
                        tell combo box 1
                            set value to VPNHost
                        end tell
                        end if
                        click button "Connect"
                        set VPNRunning to true
                        delay 1
                        exit repeat
                    end if
                end tell
            end tell
        end tell
    end if
end repeat


if VPNRunning is true then
    set VPNRunning to false
    tell application "System Events"
        tell process "Cisco AnyConnect Secure Mobility Client"
            tell window 3
                tell pop up button 1
                    --your code
                    click
                    delay 0.25
                    tell menu 1
                        click menu item VPNGroup
                        set VPNRunning to true
                    end tell
                end tell
            end tell
        end tell
    end tell
end if

delay 1

if VPNRunning is true then
    set VPNRunning to false
    repeat 10 times
        tell application "System Events"
            tell process "Cisco AnyConnect Secure Mobility Client"
                tell window 3
                    if exists static text "Username:" then
                        tell text field 1
                            set value to VPNUsername
                        end tell
                        set VPNRunning to true
                        exit repeat
                    end if
                end tell
            end tell
        end tell
    end repeat
end if

if VPNRunning is true then
set VPNRunning to false
repeat 10 times
    tell application "System Events"
        tell process "Cisco AnyConnect Secure Mobility Client"
            tell window 3
            if exists static text "Password:" then
                tell text field 2
                set value to VPNPassword
                end tell
                key code 36 -- 按下回车
                set VPNRunning to true
                exit repeat
            end if
            end tell
        end tell
    end tell
    delay 1
end repeat
end if

if VPNRunning is true then
set VPNRunning to false
delay 1
tell application "System Events"
    tell process "Cisco AnyConnect Secure Mobility Client"
        tell window 2
            key code 36 -- 按下回车
            set VPNRunning to true
        end tell
    end tell
end tell
delay 1
end if


if VPNRunning is false then
    display dialog "VPN运行异常,请关闭后重试." with icon caution buttons {"确认"} giving up after 5
end if

end if
版权声明:大帅 发表于 3周前,共 2382 字。
转载请注明:Mac VPN自动连接脚本(AnyConnect) | 大帅同学

暂无评论

暂无评论...