01 logo

Connecting CiscoAnyConnectVPN via bash script in Windows

Cisco VPN

By vinoth kumar vPublished about a year ago Updated about a year ago 3 min read
Like
Connecting CiscoAnyConnectVPN via bash script in Windows
Photo by Petter Lagson on Unsplash

I hope everyone in the technical world would come across the word VPN and using it on the needed basics.

I have been using the Cisco AnyConnect as my primary VPN Client for more than a few months. The simple view of the client is really impressive and productive.

Whenever I want to connect to my VPN host following are the ideal steps that are followed/suggested as well

  1. Open CiscoAnyConnect VPN client
  2. Type VPN host address in the text of the VPN client and click connect.
  3. VPN Client will open the alert popup to select the type, username & password.
  4. Enter the details and press OK
  5. This will establish the VPN connection with the request address

The steps are easy when you are working with only one VPN connection but in most cases, it's not true. Earlier I had to work with 3 VPN connections for my work and later it had reduced to two, I need to switch between the VPN for my work.

I was checking this for a long time and I have got few leads based on that I have automated the connection.

Following are the steps to follow

  1. CiscoAnyConnect with CLI
  2. Create the simple batch script to connect
  3. Create Custom Run Command

CiscoAnyConnect with CLI

  1. Verify the CiscoAnyConnect CLI
  2. Navigate to the CiscoAnyConnect installation directory. The default directory would be C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client
  3. Open the Command Prompt in the current directory and execute vpncli.exe disconnect
  4. If you are seeing the following image then you are good to go, if not reinstall the CiscoAnyConnect

Create the simple batch script to connect

  1. Navigate to CiscoAnyConnect installation directory
  2. The default directory would be C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client
  3. Create a file based on the following template

Template File

connect <host>

[Optional: drop down selection]

<username>

<password>

Sample File

connect connect2vinoth.com

0

vinoth

openworld

4. Save the file in sample file format c2v.txt

5. Create the batch file connecting to connect2vinoth.com

Template

vpncli.exe disconnect > To disconnect the existing connection

vpncli.exe -s < filename.txt > To connect VPN via CMD

Sample

vpncli.exe disconnect

vpncli.exe -s < c2v.txt

Create Custom Run Command

  • Copy c2v.bat ctrl + C > To copy the file
  • Navigate to C:\Windows and paste as a shortcut
  • Rename the file as required, in my case I am naming it as c2v
  • Open Run Command using win + R
  • Type as c2v and press enter, It will open the command prompt and start to establish the command prompt.
  • Configure on System startup (If needed)

    The easiest way to trigger scripts to run at startup is to drop them inside the startup folder.

    To easily navigate to the startup folder, Windows has an alias available: shell:startup. Use shell:common startup

    You can get to the startup folder a couple of ways:

  • Open the Run dialog with WindowsKey+R and enter shell:startup.
  • In the command prompt, enter explorer shell:startup.
  • Simply copy the files that you want to run on startup into the folder.

    For example, in Windows 10, these paths work for me for the user and global:

    %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup

    %ProgramData%\Microsoft\Windows\Start Menu\Programs\StartUp

    I have tried these settings on my Windows 10 machine. It's working fine for me, you guys try it out and let me know your feedback in the comment section.

    Key Take aways

  • Connecting to VPN is made easy and no need to do selection, enter username, password and wait for connection establishment. connect via run command or configure in startup itself.
  • We have reduced atleast 5 minutes from our day-in and day-out activity and the time taken between switching the VPN.d
  • Happy Coding!

    Happy Working!!

    Happy innovation!!!

    Spread the Positivity around you

    how to
    Like

    About the Creator

    Reader insights

    Be the first to share your insights about this piece.

    How does it work?

    Add your insights

    Comments

    There are no comments for this story

    Be the first to respond and start the conversation.

    Sign in to comment

      Find us on social media

      Miscellaneous links

      • Explore
      • Contact
      • Privacy Policy
      • Terms of Use
      • Support

      © 2024 Creatd, Inc. All Rights Reserved.