1

I need to make an bash script on Linux that connects into an VPN, processes some tasks, and then disconnects.

To connect to the VPN I use the forticlient ssl vpn (like fortinet but for VPN instead). For example:

./forticlientsslvpn_cli --server 172.17.97.85:10443 --vpnuser forti

Does any know the how to then disconnect from a script afterwards?

2 Answers 2

1

In your case, simply record a PID of the forticlientsslvpn_cli process and send it SIGHUP, SIGQUIT or SIGTERM. The preferred signal is the one that makes a graceful disconnect. A bit dirty solution (although maybe good enough for you) is to use just killall -s SIG... forticlientsslvpn_cli.

Fortigate devices also support Cisco-style IPsec connections and there're multiple software clients available for Linux, so replacing SSL VPN client may be another way to go.

1

On this page someone has created an expect script around the client to handle the disconnect http://euer.krebsco.de/scripting-the-fortigate-vpn-client.html

#!/usr/bin/expect -f
# cd into the 64 bit folder of the client
# usage: efort.exp

spawn ./forticlientsslvpn_cli --server <VPNIP>:<VPNPORT> --vpnuser <VPNUSER> 2>&1
log_user 0
send_user "Logging in\n"
expect "Password for VPN:"
send "<VPNPASSWORD>\n"

# i needed ths for 'certificate error'
expect "Would you like to connect to this server"
send "Y\n"
send_user "Beginning to connect\n"
expect "STATUS::Tunnel running"
send_user "Tunnel running!\n"

# this is how long the next expect waits for pattern match, in seconds
set timeout 90001
expect "STATUS::Tunnel closed"
send_user "Tunnel closed!\n"
send_user "Dying\n"
close
exit

At the end, enterprise-loop the script and we are done!

#!/bin/sh
cd "$(dirname "$(readlink -f "$0")")"
while sleep 1;do
    expect efort.exp
    echo "Restarting forticlient !"
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .