美文网首页
Linux下使用FortiClient客户端连接VPN

Linux下使用FortiClient客户端连接VPN

作者: 风吹路过的云 | 来源:发表于2019-05-06 16:14 被阅读0次

基于ubuntu18.04.2

1 下载客户端forticlient-sslvpn_4.4.2333-1_amd64.deb (64位的)
下载地址为:

https://hadler.me/files/forticlient-sslvpn_4.4.2333-1_amd64.deb

2 安装

sudo apt-get install <DOWNLOAD_LOCATION>/forticlient-sslvpn_4.4.2333-1_amd64.deb

安装依赖

sudo apt-get install ppp expect

setup

sudo /opt/forticlient-sslvpn/64bit/helper/setup

3 一个方便的sh脚本

#!/bin/bash
# init only
CONNECT_PID=""
RUNNING=""
# Provide required parameters
FORTICLIENT_PATH="/opt/forticlient-sslvpn/64bit/forticlientsslvpn_cli"
VPN_HOST="<HOST:PORT>"
VPN_USER="<USER_NAME>"
VPN_PASS="<PASSWORD>"
# Checks whether vpn is connected
function checkConnect {
    ps -p $CONNECT_PID &> /dev/null
    RUNNING=$?
}
# Initiates connection
function startConnect {
    # start vpn connection and grab its pid (expect script returns spawned vpn conn pid)
    CONNECT_PID="connect"
    eval $CONNECT_PID
}
# Creates an expect script to complete automated vpn connection
function connect {
    # write expect script to tmp location
    cat <<-EOF > /tmp/expect
        #!/usr/bin/expect -f
        match_max 1000000
        set timeout -1
        spawn $FORTICLIENT_PATH --server $VPN_HOST --vpnuser $VPN_USER --keepalive
        puts [exp_pid]
        expect "Password for VPN:"
        send -- "$VPN_PASS"
        send -- "\r"
        expect "Would you like to connect to this server? (Y/N)"
        send -- "Y"
        send -- "\r"
        expect "Clean up..."
        close
EOF
     
    #IMPORTANT!: the "EOF" just above must be preceded by a TAB character (not spaces)
    # lock down and execute expect script
    chmod 500 /tmp/expect
    /usr/bin/expect -f /tmp/expect
    # when expect script is finished (closes) clean up
    rm -f /tmp/expect
}
startConnect
# note this will not continuously loop, it will only loop if the spawned vpn connection drops
# i.e. will only hit this code when expect closes
while true
do
    # sleep a bit of time (why not, everyone needs sleep)
    sleep 1
    checkConnect
    [ $RUNNING -ne 0 ] && startConnect
done

4 脚本的使用

sudo chown root:root ~/forti-vpn.sh
sudo chmod 600 ~/forti-vpn.sh
sudo chmod +x ~/forti-vpn.sh

sudo ./forti-vpn.sh &

5 停止

sudo pkill forti

参考资料:

https://confluence.jaytaala.com/display/TKB/Continuous+and+automated+VPN+connection+with+FortiClient+%28CLI+only%29+using+bash+and+expect+scripting

https://hadler.me/linux/forticlient-sslvpn-deb-packages/

相关文章

网友评论

      本文标题:Linux下使用FortiClient客户端连接VPN

      本文链接:https://www.haomeiwen.com/subject/srkdoqtx.html