通过applescript连接后检查VPN连接是否成功
Checking if VPN connection was successful after connection through applescript
我有一个脚本可以将您连接到 VPN,但我无法判断用户是否已成功连接。当脚本运行时,它会提示用户输入密码。但是,如果用户点击取消,我的脚本仍然认为他们已连接。
tell application "System Events"
tell current location of network preferences
set VPNService to service "MyVPN"
if exists VPNService then
try
connect VPNService
set VPNFailed to "false"
on error
set VPNFailed to "true"
end try
else
end if
end tell
end tell
delay 11
if VPNFailed is "true" then
set image1 to current application's NSImage's imageNamed_("NSStatusUnavailable")
UIVPNStatus's setImage_(image1)
display dialog "Server is down, please try again later." buttons {"OK"}
else
set image1 to current application's NSImage's imageNamed_("NSStatusAvailable")
UIVPNStatus's setImage_(image1)
display notification "" with title "Connection Successful!" subtitle "You may now browse the internet privatley and securley"
set image2 to current application's NSImage's imageNamed_("VPNLocked.png")
UIVPNLock's setImage_(image2)
end if
else
display dialog "Cannot connect to server." buttons {"OK"} with icon 2
set volume 7
beep
tell VPNProgress to stopAnimation_(sender)
end if
tell VPNProgress to stopAnimation_(sender)
`
解决该问题的一种方法是建立一个计时器,该计时器会一直对连接状态执行 ping 操作,直到 returns 为真或超时。这是一个尝试(我简化了您的代码以切入问题的实质)。请注意,此代码假定您使用的是 Mac 的内置 VPN。
set myVPNServiceName to "Your VPN connection name" --update with real name
set timer to 20 -- update to whatever time (in seconds) you feel is acceptable
set isVPNConnected to false
tell application "System Events"
tell current location of network preferences
set VPNService to null
set vpnConnectionInProgress to false
-- Try to connect to VPN service
try
set VPNService to service myVPNServiceName
connect VPNService
set vpnConnectionInProgress to true
end try
if vpnConnectionInProgress is true then
-- keep checking until timer expires or connection is confirmed
repeat while timer > 0 and isVPNConnected is false
if current configuration of VPNService is connected then
set isVPNConnected to true
end if
log isVPNConnected
set timer to timer - 1
delay 1
end repeat
end if
end tell
end tell
if isVPNConnected is true then
display notification "" with title "Connection Successful!" subtitle "You may now browse the internet privately and securely"
else
display dialog "Cannot connect to " & myVPNServiceName buttons {"OK"}
end if
我有一个脚本可以将您连接到 VPN,但我无法判断用户是否已成功连接。当脚本运行时,它会提示用户输入密码。但是,如果用户点击取消,我的脚本仍然认为他们已连接。
tell application "System Events"
tell current location of network preferences
set VPNService to service "MyVPN"
if exists VPNService then
try
connect VPNService
set VPNFailed to "false"
on error
set VPNFailed to "true"
end try
else
end if
end tell
end tell
delay 11
if VPNFailed is "true" then
set image1 to current application's NSImage's imageNamed_("NSStatusUnavailable")
UIVPNStatus's setImage_(image1)
display dialog "Server is down, please try again later." buttons {"OK"}
else
set image1 to current application's NSImage's imageNamed_("NSStatusAvailable")
UIVPNStatus's setImage_(image1)
display notification "" with title "Connection Successful!" subtitle "You may now browse the internet privatley and securley"
set image2 to current application's NSImage's imageNamed_("VPNLocked.png")
UIVPNLock's setImage_(image2)
end if
else
display dialog "Cannot connect to server." buttons {"OK"} with icon 2
set volume 7
beep
tell VPNProgress to stopAnimation_(sender)
end if
tell VPNProgress to stopAnimation_(sender)
`
解决该问题的一种方法是建立一个计时器,该计时器会一直对连接状态执行 ping 操作,直到 returns 为真或超时。这是一个尝试(我简化了您的代码以切入问题的实质)。请注意,此代码假定您使用的是 Mac 的内置 VPN。
set myVPNServiceName to "Your VPN connection name" --update with real name
set timer to 20 -- update to whatever time (in seconds) you feel is acceptable
set isVPNConnected to false
tell application "System Events"
tell current location of network preferences
set VPNService to null
set vpnConnectionInProgress to false
-- Try to connect to VPN service
try
set VPNService to service myVPNServiceName
connect VPNService
set vpnConnectionInProgress to true
end try
if vpnConnectionInProgress is true then
-- keep checking until timer expires or connection is confirmed
repeat while timer > 0 and isVPNConnected is false
if current configuration of VPNService is connected then
set isVPNConnected to true
end if
log isVPNConnected
set timer to timer - 1
delay 1
end repeat
end if
end tell
end tell
if isVPNConnected is true then
display notification "" with title "Connection Successful!" subtitle "You may now browse the internet privately and securely"
else
display dialog "Cannot connect to " & myVPNServiceName buttons {"OK"}
end if