One-Click Connection to Cisco AnyConnect Secure Mobility Client

Oct 6, 2019 · 2 min read · 338 Words · -Views -Comments

For reasons we all know, I couldn’t access the external internet recently, so I used the company VPN — but connecting was time‑consuming with many steps. I looked for a better way.

An internal email helped a lot. I tested it successfully, so I’m summarizing it here.

The tedious manual flow

  1. Start Cisco AnyConnect Secure Mobility Client
  2. Enter account and password
  3. Choose SMS verification
  4. Check the SMS code on your phone
  5. Enter the code in the client
  6. Click confirm

How to automate it

  1. Install oath-toolkit

    $ brew install oath-toolkit
    
  2. Write a shell script

    Call it vpn.sh for example.

    #!/bin/bash
    
    killall 'Cisco AnyConnect Secure Mobility Client' 2>/dev/	null
    /opt/cisco/anyconnect/bin/vpn disconnect >/dev/null
    
    code=`oathtool --totp -b **secret_key**`
    
    /opt/cisco/anyconnect/bin/vpn -s connect $1.company.vpn.com << EOF | sed 			's/Password: .*/Password: ********/g'
    **username**
    **password**
    **second_authentication_method_index**
    $code
    EOF
    open -g '/Applications/Cisco/Cisco AnyConnect Secure Mobility Client.app'
    


3. Fill in variables

	- `secret_key`: your VPN TOTP secret. For Okta Verify: change password  Extra Verification  Okta Verify Mobile App  Setup  Next  Problems scanning barcode  copy the Secret Key.
	- `username`: VPN username
	- `password`: VPN password
	- `second_authentication_method_index`: the index for the selected secondfactor method; if using the secret key TOTP, provide its index.

	At this point, the most painful SMS verification code can be replaced by the secret key.

4. Make the script executable

	```bash
chmod +x vpn.sh
	```

5. Run the script
	
	```bash
	./vpn.sh bj

	``` 
Here, the `bj` variable exists because our VPN has multiple node regions. The script uses `$1.company.vpn.com`. If you do not need it, remove the variable.

The script will automatically start the Cisco client and connect. This removes the tedious manual steps and saves at least 2 minutes each time. It also kills other client processes at startup, and re-running the script while the client is running is fine.

## Nice to have
Automation helps, but opening Terminal each time isnt elegant. Create an Alfred workflow so typing vpn runs the script automatically.

## Final Thoughts

Repetitive steps are manual labor. Use tools to eliminate toil.

	
Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover