Automatic Reconnect Cisco AnyConnect VPN in Case of an Interruption

Due to the COVID-19 pandemic, all students and employees of my university are forced to work from home. This led to a significant increase in VPN traffic and forced the IT department to disconnect all clients every 10 hours. Since I carry out model optimizations with Comsol at home, I need a constant connection to the license server at the university. If the VPN connection is disconnected, Comsol stops working and displays a license error message, which in turn causes my optimization routine to crash.

Automatic Reconnect Cisco AnyConnect VPN

AnyConnect from Cisco is used to connect to the university network. Normally the connection is established from the Comsol Workstation with the AnyConnect GUI. Fortunately, there is also a command line interface, which can be found under /opt/cisco/anyconnect/bin/vpn. By calling this command, the user is prompted to enter the VPN URL, user name and password. This process is automated with an expect script as shown below. Because the computer is at home, the password is written directly into the script. This should always be avoided for safety reasons. In case of a timeout the script will be aborted within 25 seconds.

#!/usr/bin/expect

set timeout 25

spawn "/opt/cisco/anyconnect/bin/vpn"
expect "state: Disconnected" { send "connect <URL>\r" }
expect "*sername*" { send "<username>\r" }
expect "*assword*" { send "<password>\r" }
expect "state: Connected" { send "exit\r" }

A second, standard bash script is written, which calls the expect script every half minute. If the VPN connection is interrupted, a new connection is automatically established.

#!/bin/bash

while `true`
do
   now=$(date)
   echo -e "\n\nConnect to BFH VPN Server"
   echo "Current Time: $now"
   ./connect_to_VPN_expect
   sleep 30
done

With this method the license error did not occur, although the VPN connection is interrupted from time to time for a few seconds.

Borrow Comsol License (Update)

Another solution to avoid the license error when the connection to the license server is interrupted is to borrow the license. The license for most modules can be borrowed from the Comsol GUI. Unfortunately the LiveLink module for Matlab is not listed. As long as Matlab is installed on the same computer as Comsol, the borrowing method works. But in my setup I run Comsol on a dedicated workstation and connect with Matlab installed on another computer via LiveLink over TCP.

In this case, the borrowing procedure must be carried out via the command line.

/usr/local/comsol55/multiphysics/license/glnxa64/lmborrow 27-mar-2020 23:59

With the following line it can be verified which modules are borrowed.

/usr/local/comsol55/multiphysics/license/glnxa64/lmborrow -status

And finally borrowing can be stopped with this command.

/usr/local/comsol55/multiphysics/license/glnxa64/lmborrow -clear