Connecting CiscoAnyConnectVPN via bash script in Windows

vinoth kumar v
4 min readAug 17, 2021

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.

Think

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
  4. Configure on System startup(If needed)

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
    The default directory would be C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client
  2. 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

  1. Copy c2v.bat ctrl + C > To copy the file
  2. Navigate to C:\Windows and paste as a shortcut
  3. Rename the file as required, in my case I am naming it as c2v
  4. Open Run Command using win + R
  5. 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:

  1. Open the Run dialog with WindowsKey+R and enter shell:startup.
  2. 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 out and let me know your feedback in the comment section.

Happy Coding!
Happy Working!!
Happy innovation!!!
Spread the Positivity around you

Thanks for reading, this is the first article and I dedicate this post to my MOM. I am in the initial phase & thanks to everyone who was encouraging me. If you like this post, please click the clap icon (you can do it more than once if you wish) and click follow button below to get more updates.

--

--