Skip to content

Instantly share code, notes, and snippets.

@rodrigo-lima
Forked from halocaridina/anyconnect.scpt
Last active August 19, 2016 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigo-lima/2fb38c42fb348b26dcd927242528a259 to your computer and use it in GitHub Desktop.
Save rodrigo-lima/2fb38c42fb348b26dcd927242528a259 to your computer and use it in GitHub Desktop.
update to allow VPN to be restarted and to email the new IP
-- 1. Create a new generic password entry in Keychain Access called "WHATEVER_AnyConnect_VPN" (the name in Keychain access must match that in line 39 below) with your password for the Cisco AnyConnect VPN server.
-- 2. Open this script in Script Editor (both this and the above are in the Applications->Utilities folder) and "Save as.." an Application (.app) with desired name.
-- 3. Open Security & Privacy System Preferences, go to Privacy, Accessibility.
-- 4. Enable the above .app so it can access Accessibility
-- 5. Copy and paste a nice icon on the generic Applescript icon (I used a copy of the default AnyConnect one)
-- 6. Add the new .app to /Users/[yourshortname]/Applications with a shortcut to your Dock
-- 7. Enjoy the fast connection with no need to enter password and increased security of not having a sensitive password stored as plain text
-- 8. Run script again to close connection
-- Some Constants - change as needed
set targetApp to "Cisco AnyConnect Secure Mobility Client"
set inString to "WHATEVER_AnyConnect_VPN"
set recipientName to "First Last"
set recipientAddress to "first.last@something.com"
set theSubject to "Fresh new VPN IP for you..."
-- Determine if AnyConnect is currently running
tell application "System Events"
set processExists to exists process targetApp
end tell
-- disconnect if running; else start connection and fill in password
if processExists is true then
tell application targetApp
activate
try
quit
end try
delay 2
end tell
end if
-- Re-Connect
tell application targetApp
activate
end tell
tell application "System Events"
-- Wait for first window to open. Do nothing.
repeat until (window 1 of process targetApp exists)
delay 1
end repeat
-- You may need to uncomment below if your OpenConnect implementation requires a keystroke to accept the default VPN
-- tell process targetApp
-- keystroke return
-- end tell
-- Wait for second window to open and then automatically enter password extracted from your Keychain
repeat until (window 2 of process targetApp exists)
delay 2
end repeat
tell process targetApp
-- This is where the the password in the Keychain is accessed for use as input rather than being hardcoded as plain text in other versions of this script out in the wild
set PSWD to do shell script "/usr/bin/security find-generic-password -wl " & quoted form of inString
keystroke PSWD as text
keystroke return
end tell
-- Autoclick on "Accept" of AnyConnect Banner window. If you have no welcome banner that needs acceptance, comment out these lines to the first "end tell" below
-- repeat until (window "Cisco AnyConnect - Banner" of process targetApp exists)
-- delay 2
-- end repeat
-- tell process targetApp
-- keystroke return
-- end tell
end tell
-- wait a bit
delay 5
-- find new IP
set vpn_ip to do shell script "ifconfig | grep 'inet ' | tail -1 | cut -d ' ' -f 2"
-- email stuff
set theContent to "New IP is: " & vpn_ip
tell application "Mail"
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
send
end tell
end tell
-- DONE
@rodrigo-lima
Copy link
Author

To run it periodically to restart VPN, just add the following PLIST

  • save applescript above as ~/vpn.applescript
  • save the PLIST XML below as ~/vpn.plist
  • make appropriate changes to the times when the connection should restart
  • launchctl load vpn.plist or unload to remove it
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.oraclecorp.internal.vpn-reconnect</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/Users/__MY_USER__/vpn.applescript</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>16</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>

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