Skip to content

Instantly share code, notes, and snippets.

@marteinn
Last active August 29, 2024 12:05
Show Gist options
  • Save marteinn/a409be4bd3dcea240a3d9a67d641dcdf to your computer and use it in GitHub Desktop.
Save marteinn/a409be4bd3dcea240a3d9a67d641dcdf to your computer and use it in GitHub Desktop.

How to use AnyConnect with the keychain on MacOS

1. Store VPN details in keychain

  • Open Keychain Access
  • Navigate to "Login" in the left menu
  • Cmd+N (New Password item)
  • Fill in these details:
    • Keychain Item Name: anyconnect
    • Account name: Your username (firstname.lastname)
    • Password

2. Create command for opening AnyConnect

  • Create new command file (you will use this to open AnyConnect in the future)

    touch /Applications/AnyConnect.command
  • Add this to the file (remember to change EXAMPLE.COM to your VPN domain)

    #! /bin/zsh
    /opt/cisco/anyconnect/bin/vpn connect EXAMPLE.COM -s << EOM
    $(security find-generic-password -s anyconnect | grep acct | awk -F\" '/acct"<blob>/ {print $4}')
    $(security find-generic-password -ws anyconnect)
    EOM
    
    osascript -e 'tell application "Terminal" to close (every window whose name contains "AnyConnect.command")'
  • Add execute permissions

    chmod u+x /Applications/AnyConnect.command
  • Now verify by opening AnyConnect.command

    open /Applications/AnyConnect.command
  • If everything goes right a new terminal will open up that will start AnyConnect in the background

    ...
      >> state: Connecting
      >> notice: Establishing
      >> notice: Establishing
      >> state: Connected
    
    
    Saving session...complete
    
    [Process completed]
    
  • To close AnyConnect you need to open the Cisco AnyConnect Secure Mobility Client.app  to activate it's GUI and from there close the connect. Another way is to setup another command following the instruction below.

3. Create command for disconnecting AnyConnect

  • Create another command file (you will use this to close AnyConnect)
    touch /Applications/AnyConnect\ Disconnect.command
  • Add this to the file
    #! /bin/zsh
    /opt/cisco/anyconnect/bin/vpn disconnect
    
    osascript -e 'tell application "Terminal" to close (every window whose name contains "AnyConnect Disconnect.command")'
  • Add execute permissions
    chmod u+x /Applications/AnyConnect\ Disconnect.command
  • Now verify by opening AnyConnect Disconnect.command
    open /Applications/AnyConnect\ Disconnect.command
  • This will open a new terminal and output something like this:
    ...
    >> state: Disconnecting
    >> notice: Disconnect in
    >> state: Disconnecting
    >> state: Disconnected
    
    Saving session...completed
    
    [Process completed]
    

Credits

#! /bin/zsh
/opt/cisco/anyconnect/bin/vpn disconnect
osascript -e 'tell application "Terminal" to close (every window whose name contains "AnyConnect Disconnect.command")'
#! /bin/zsh
/opt/cisco/anyconnect/bin/vpn connect frojd.qlosr.net -s << EOM
$(security find-generic-password -s anyconnect | grep acct | awk -F\" '/acct"<blob>/ {print $4}')
$(security find-generic-password -ws anyconnect)
EOM
osascript -e 'tell application "Terminal" to close (every window whose name contains "AnyConnect.command")'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment