Automatic login using cisco vpn client

Discussion in 'Cisco' started by dmcguire56, Jul 6, 2012.

  1. dmcguire56

    dmcguire56

    Joined:
    Jul 6, 2012
    Messages:
    1
    Likes Received:
    0
    I currently use cisco vpn client and want to automate the process of connecting and disconnecting inside a script. The line I use to start the cisco client is as follows:
    "c:\Program Files\Cisco Systems\VPN Client\vpnclient" connect cliauth "myProfile" user "myName" pwd "myPassword"
    When this is ran, I have to hit enter 3 times to get thru the username, password and domain prompts. Is there a way to have the script "hit enter" or is there a better way to automate the connection?
     
    dmcguire56, Jul 6, 2012
    #1
    1. Advertisements

  2. dmcguire56

    vsivak

    Joined:
    Aug 31, 2012
    Messages:
    1
    Likes Received:
    0

    Hello this is a powershell script "Cisco AnyConnect Auto Login" I have created to automatically connect and log me in or auto reconect with Cisco AnyConnect Secure Mobility Client version 3.0.5080.
    It works quite nice on Windows 7 and Windows 8.
    1. Create file for example c:\test\CiscoVPNAutoLogin.ps1
    2. Paste the code below into that file. Do not forget to change CiscoVPNHost, Login and Password variables.
    3. Run the script. You can run it for example using following command line: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe "C:\test\CiscoVPNAutoLogin.ps1"

    Script code is following:

    Code:
    #Source www.cze.cz
    #This script is tested with "Cisco AnyConnect Secure Mobility Client version 3.0.5080"
    #Please change following variables
    
    #IP address or host name of cisco vpn
    [string]$CiscoVPNHost = "192.168.0.50"
    [string]$Login = "LOGIN"
    [string]$Password = "PASSWORD"
    
    #Please check if file exists on following paths
    [string]$vpncliAbsolutePath = 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe'
    [string]$vpnuiAbsolutePath  = 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe'
    
    #****************************************************************************
    #**** Please do not modify code below unless you know what you are doing ****
    #****************************************************************************
    
    Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
    
    #Set foreground window function
    #This function is called in VPNConnect
    Add-Type @'
      using System;
      using System.Runtime.InteropServices;
      public class Win {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    '@ -ErrorAction Stop
    
    #quickly start VPN
    #This function is called later in the code
    Function VPNConnect()
    {
        Start-Process -FilePath $vpncliAbsolutePath -ArgumentList "connect $CiscoVPNHost"
        $counter = 0; $h = 0;
        while($counter++ -lt 1000 -and $h -eq 0)
        {
            sleep -m 10
            $h = (Get-Process vpncli).MainWindowHandle
        }
        #if it takes more than 10 seconds then display message
        if($h -eq 0){echo "Could not start VPNUI it takes too long."}
        else{[void] [Win]::SetForegroundWindow($h)}
    }
    
    #Terminate all vpnui processes.
    Get-Process | ForEach-Object {if($_.ProcessName.ToLower() -eq "vpnui")
    {$Id = $_.Id; Stop-Process $Id; echo "Process vpnui with id: $Id was stopped"}}
    #Terminate all vpncli processes.
    Get-Process | ForEach-Object {if($_.ProcessName.ToLower() -eq "vpncli")
    {$Id = $_.Id; Stop-Process $Id; echo "Process vpncli with id: $Id was stopped"}}
    
    
    #Disconnect from VPN
    echo "Trying to terminate remaining vpn connections"
    start-Process -FilePath $vpncliAbsolutePath -ArgumentList 'disconnect' -wait
    #Connect to VPN
    echo "Connecting to VPN address '$CiscoVPNHost' as user '$Login'."
    VPNConnect
    
    #Write login and password
    [System.Windows.Forms.SendKeys]::SendWait("$Login{Enter}")
    [System.Windows.Forms.SendKeys]::SendWait("$Password{Enter}")
    
    #Start vpnui
    start-Process -FilePath $vpnuiAbsolutePath
    #Wait for keydown
    echo "Press any key to continue ..."
    try{$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")}catch{}
    
     
    Last edited: Sep 1, 2012
    vsivak, Aug 31, 2012
    #2
    1. Advertisements

  3. dmcguire56

    virtualpal

    Joined:
    Oct 4, 2012
    Messages:
    1
    Likes Received:
    0
    Connect not available.

    Thank you so much for the script. Unfortunately, when I try to run the script, it opens the Cisco AnyConnect GUI application and then I get a pop-up message "Connect not available. Another AnyConnect application is running or the functionality is not requested by this application."

    Any suggestions on what could be causing this? I have tried to restart the PC to ensure that there is nothing running before executing the script. Any suggestions will be highly appreciated. Thank you.
     
    virtualpal, Oct 4, 2012
    #3
  4. dmcguire56

    tecnolord

    Joined:
    Jul 2, 2013
    Messages:
    1
    Likes Received:
    0
    to prevent the popup i simply added a delay to anable the connection to start before the UI is activated.

    #Start vpnui
    start-sleep 5
     
    tecnolord, Jul 2, 2013
    #4
  5. dmcguire56

    bhoomi

    Joined:
    Aug 15, 2013
    Messages:
    1
    Likes Received:
    0
    powershell script to connect/disconnect VPN

    VSivac, Thanks alot. This is very much helpful. But I have a question here .

    I finally get to see the status and other details in CMD window , how do I get that value of "Status: connected/disconnected" in roder to return the value in the end of my whole program. I have to further put this work on sharepoint in terms of red/green circles with respect to the status: disconnected/connected.

    Thanks alot!
    Waiting for your reply.
     
    bhoomi, Aug 15, 2013
    #5
  6. dmcguire56

    tiwana

    Joined:
    Jan 17, 2014
    Messages:
    1
    Likes Received:
    0
    Little change i had to make in the orignale awesome script

    Awesome job with the script man :congrats:but I had to change it a little to fit my environment. I have post the change below in your original script.

    Script code is following:

    Code:
    #
    #This script is tested with "Cisco AnyConnect Secure Mobility Client version 3.0.5080"
    #Please change following variables
    
    #IP address or host name of cisco vpn
    [string]$CiscoVPNHost = "192.168.0.50"
    [string]$Login = "LOGIN"
    [string]$Password = "PASSWORD"
    
    #Please check if file exists on following paths
    [string]$vpncliAbsolutePath = 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe'
    #[string]$vpnuiAbsolutePath  = 'C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpnui.exe'
    
    #****************************************************************************
    #**** Please do not modify code below unless you know what you are doing ****
    #****************************************************************************
    
    Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop
    
    #Set foreground window function
    #This function is called in VPNConnect
    Add-Type @'
      using System;
      using System.Runtime.InteropServices;
      public class Win {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    '@ -ErrorAction Stop
    
    #quickly start VPN
    #This function is called later in the code
    Function VPNConnect()
    {
        Start-Process -FilePath $vpncliAbsolutePath -ArgumentList "connect $CiscoVPNHost"
        $counter = 0; $h = 0;
        while($counter++ -lt 1000 -and $h -eq 0)
        {
            sleep -m 10
            $h = (Get-Process vpncli).MainWindowHandle
        }
        #if it takes more than 10 seconds then display message
        if($h -eq 0){echo "Could not start VPNUI it takes too long."}
        else{[void] [Win]::SetForegroundWindow($h)}
    }
    
    #Terminate all vpnui processes.
    Get-Process | ForEach-Object {if($_.ProcessName.ToLower() -eq "vpnui")
    {$Id = $_.Id; Stop-Process $Id; echo "Process vpnui with id: $Id was stopped"}}
    #Terminate all vpncli processes.
    Get-Process | ForEach-Object {if($_.ProcessName.ToLower() -eq "vpncli")
    {$Id = $_.Id; Stop-Process $Id; echo "Process vpncli with id: $Id was stopped"}}
    
    
    #Disconnect from VPN
    echo "Trying to terminate remaining vpn connections"
    start-Process -FilePath $vpncliAbsolutePath -ArgumentList 'disconnect' -wait
    #Connect to VPN
    echo "Connecting to VPN address '$CiscoVPNHost' as user '$Login'."
    VPNConnect
    
    #Write login and password
    [System.Windows.Forms.SendKeys]::SendWait("y{Enter}")
    [System.Windows.Forms.SendKeys]::SendWait("$Login{Enter}")
    [System.Windows.Forms.SendKeys]::SendWait("$Password{Enter}")
    [System.Windows.Forms.SendKeys]::SendWait("y{Enter}")
    
    #Start vpnui
    #start-Process -FilePath $vpnuiAbsolutePath
    #Wait for keydown
    echo "Press any key to continue ..."
    try{$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")}catch{}
     
    tiwana, Jan 17, 2014
    #6
  7. dmcguire56

    nyx

    Joined:
    May 25, 2016
    Messages:
    1
    Likes Received:
    0
    Hi, i also want to use this script for vpn connection... but after login in CMD im getting
    >> Login Denied , unauthorized connection mechanism , contact your administrator" ..

    Im new in ASA/Cisco, and my administrator is not available soo dont know if i should change something in Asa configuration...

    Im read about this:
    vpn-tunnel-protocol svc webvpn


    Thanks
     
    nyx, May 25, 2016
    #7
  8. dmcguire56

    mm_ashraf

    Joined:
    Jan 4, 2018
    Messages:
    1
    Likes Received:
    0
    Dear users ,
    about cisco automation code is very useful. I have simple vpn box on which our users can connect network using cisco vpn client version 5.0.7.xxx. I have modified code for users which are using vpnclient for connectivity to network.
    ps1 file is also attached so you can avoid typo mistakes and I believe it will be useful for you as well.
    I have build youtube vide for this automation as well, it will helpful for you and your team for any connectivity.
    https://www.youtube.com/channel/UCKZX-hYq4N7q1AE_juWpRiw
     

    Attached Files:

    Last edited: Jan 4, 2018
    mm_ashraf, Jan 4, 2018
    #8
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.