Cisco VPN Client automatic login

10,976

Solution 1

Run vpnclient.exe /?:

enter image description here That way just run

vpnclient.exe connect MyVPNConnection -s < file.txt

file.txt

username
password

Solution 2

Below worked for me Cisco AnyConnect Secure Mobility Client:

  1. Try to connect to VPN for the first time using vpncli.exe and note every keystroke i.e every command, every enter( \n ) you press, username & password you enter.
  2. Copy each command sequentially in .login_info file.

Sample .login_info:

connect unkbown.data-protect.com
\n  
\n
KC23452
\n

Note: Replace \n with normal enter, these are the exact steps that I followed while connecting via vpncli.exe. Username and group-name were saved automatically that's the reason the 2nd and 3rd lines are \n ( enter ). Also, the last \n is required.

  1. Go to C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client
  2. Open CMD here
  3. vpncli.exe -s < .login_info

Solution 3

First, we need to use the vpncli.exe command line approach with the -s switch. It works from command line or script. If you were looking for a solution in C#:

//file = @"C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe"
var file = vpnInfo.ExecutablePath;
var host = vpnInfo.Host;
var profile = vpnInfo.ProfileName;
var user = vpnInfo.User;
var pass = vpnInfo.Password;
var confirm = "y";

var proc = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = file,
        Arguments = string.Format("-s"),
        UseShellExecute = false,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
    }
};

proc.OutputDataReceived += (s, a) => stdOut.AppendLine(a.Data);
proc.ErrorDataReceived += (s, a) => stdOut.AppendLine(a.Data);

//make sure it is not running, otherwise connection will fail
var procFilter = new HashSet<string>() { "vpnui", "vpncli" };
var existingProcs = Process.GetProcesses().Where(p => procFilter.Contains(p.ProcessName));
if (existingProcs.Any())
{
    foreach (var p in existingProcs)
    {
        p.Kill();
    }
}

proc.Start();
proc.BeginOutputReadLine();

//simulate profile file
var simProfile = string.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}"
    , Environment.NewLine
    , string.Format("connect {0}", host)
    , profile
    , user
    , pass
    , confirm
    );

proc.StandardInput.Write(simProfile);
proc.StandardInput.Flush();

//todo: these should be a configurable value
var waitTime = 500; //in ms
var maxWait = 10;

var count = 0;
var output = stdOut.ToString();
while (!output.Contains("state: Connected"))
{
    output = stdOut.ToString();

    if (count > maxWait)
        throw new Exception("Unable to connect to VPN.");

    count++;
    Thread.Sleep(waitTime);
}
stdOut.Append("VPN connection established! ...");

(This might have extra stuff which is not required for you specific case.)

Share:
10,976
Nicola
Author by

Nicola

Updated on June 04, 2022

Comments

  • Nicola
    Nicola almost 2 years

    I need to automate the login process of a Cisco VPN Client version 5.0.07.0440. I've tried using a command line like this but there is something wrong:

    vpnclient.exe connect MyVPNConnection user username pwd password
    

    This starts the connection but then a User Authentication dialog is shown, asking for username, password and domain. Username and password are already filled, domain is not necessary.

    To continue I must press the OK button.

    Is there a way to not show the dialog and automatically login into the vpn?