OpenConnect:设置默认路由


13

我正在尝试使用OpenConnect连接到我公司的Cisco VPN(AnyConnect)

连接似乎工作正常,我不了解的是如何设置路由。我是从命令行执行此操作的。

我使用默认的VPN脚本进行如下连接:

openconnect -u MyUserName --script path_to_vpnc_script myvpngateway.example.com

我输入了密码,并且连接正常,但是我的默认路由已更改为强制所有流量通过VPN链接,而我只希望公司流量通过VPN链接。

我需要在vpnc脚本中添加一些变量吗?目前尚不清楚该如何完成。

Answers:


16

答案如下:

使用以下bash包装器脚本来调用vpnc脚本。在包装脚本中,可通过ROUTES变量指定用于VPN连接的路由。

#!/bin/bash
#

# Routes that we want to be used by the VPN link
ROUTES="162.73.0.0/16"

# Helpers to create dotted-quad netmask strings.
MASKS[1]="128.0.0.0"
MASKS[2]="192.0.0.0"
MASKS[3]="224.0.0.0"
MASKS[4]="240.0.0.0"
MASKS[5]="248.0.0.0"
MASKS[6]="252.0.0.0"
MASKS[7]="254.0.0.0"
MASKS[8]="255.0.0.0"
MASKS[9]="255.128.0.0"
MASKS[10]="255.192.0.0"
MASKS[11]="255.224.0.0"
MASKS[12]="255.240.0.0"
MASKS[13]="255.248.0.0"
MASKS[14]="255.252.0.0"
MASKS[15]="255.254.0.0"
MASKS[16]="255.255.0.0"
MASKS[17]="255.255.128.0"
MASKS[18]="255.255.192.0"
MASKS[19]="255.255.224.0"
MASKS[20]="255.255.240.0"
MASKS[21]="255.255.248.0"
MASKS[22]="255.255.252.0"
MASKS[23]="255.255.254.0"
MASKS[24]="255.255.255.0"
MASKS[25]="255.255.255.128"
MASKS[26]="255.255.255.192"
MASKS[27]="255.255.255.224"
MASKS[28]="255.255.255.240"
MASKS[29]="255.255.255.248"
MASKS[30]="255.255.255.252"
MASKS[31]="255.255.255.254"

export CISCO_SPLIT_INC=0

# Create environment variables that vpnc-script uses to configure network
function addroute()
{
    local ROUTE="$1"
    export CISCO_SPLIT_INC_${CISCO_SPLIT_INC}_ADDR=${ROUTE%%/*}
    export CISCO_SPLIT_INC_${CISCO_SPLIT_INC}_MASKLEN=${ROUTE##*/}
    export CISCO_SPLIT_INC_${CISCO_SPLIT_INC}_MASK=${MASKS[${ROUTE##*/}]}
    export CISCO_SPLIT_INC=$((${CISCO_SPLIT_INC}+1))
}

# Old function for generating NetworkManager 0.8 GConf keys 
function translateroute ()
{
    local IPADDR="${1%%/*}"
    local MASKLEN="${1##*/}"
    local OCTET1="$(echo $IPADDR | cut -f1 -d.)"
    local OCTET2="$(echo $IPADDR | cut -f2 -d.)"
    local OCTET3="$(echo $IPADDR | cut -f3 -d.)"
    local OCTET4="$(echo $IPADDR | cut -f4 -d.)"

    local NUMADDR=$(($OCTET1*16581375 + $OCTET2*65536 + $OCTET3*256 + $OCTET4))
    local NUMADDR=$(($OCTET4*16581375 + $OCTET3*65536 + $OCTET2*256 + $OCTET1))
    if [ "$ROUTESKEY" = "" ]; then
        ROUTESKEY="$NUMADDR,$MASKLEN,0,0"
    else
        ROUTESKEY="$ROUTESKEY,$NUMADDR,$MASKLEN,0,0"
    fi
}

if [ "$reason" = "make-nm-config" ]; then
    echo "Put the following into the [ipv4] section in your NetworkManager config:"
    echo "method=auto"
    COUNT=1
    for r in $ROUTES; do
        echo "routes${COUNT}=${r%%/*};${r##*/};0.0.0.0;0;"
        COUNT=$(($COUNT+1))
    done
    exit 0
fi

for r in $ROUTES; do
    addroute $r
done

exec /etc/openconnect/vpnc-script

然后按以下方式连接:

openconnect -u myusername --script wrapper-script -b vpngateway.example.com

5
对于想知道如何将多个前缀/路由添加到ROUTES变量的任何人,您都可以通过使用空格分隔每个条目来实现。例如:ROUTES =“ 162.73.0.0/16 162.74.0.0/16”
Elliot B.

6
在Debian 8上,Ubuntu 16.04和17.04的默认vpnc-script位于/usr/share/vpnc-scripts/vpnc-script,因此在这些OS上,我需要适当地更改包装的最后一个字符串。谢谢您的解决方案!
Envek '17

我的工作VPN是10.0.xx,所以我设置ROUTES = 10.0.0.0/16并且我正在使用ubuntu,所以我将exec设置为/ usr / share / vpnc-scripts / vpnc-script-但我无法进入外面的世界。有任何想法吗?
mmaceachran

我该如何实现相反的目标,即从VPN中排除某些路由?
HappyFace

2

这是一个脚本,它在IPv4和IPv6网络上进行分割隧道(基于提供的脚本Aditya K,该脚本仍然允许将所有IPv6流量路由到VPN)。另请注意,不再需要设置CISCO_SPLIT_INC _ $ {N} _MASK变量):

#!/bin/bash
#

# Routes that we want to be used by the VPN link
ROUTES="162.73.0.0/16"
ROUTES6="1ef2:23e:5a4::0/48"

export CISCO_SPLIT_INC=0

# Create environment variables that vpnc-script uses to configure network
function addroute()
{
    local ROUTE="$1"
    export CISCO_SPLIT_INC_${CISCO_SPLIT_INC}_ADDR=${ROUTE%%/*}
    export CISCO_SPLIT_INC_${CISCO_SPLIT_INC}_MASKLEN=${ROUTE##*/}
    export CISCO_SPLIT_INC=$((${CISCO_SPLIT_INC}+1))
}

export CISCO_IPV6_SPLIT_INC=0
# Do the same for IPv6
function addroute6()
{
    local ROUTE="$1"
    local NET="${ROUTE%%/*}"
    local MASKLEN="${ROUTE##*/}"
    export CISCO_IPV6_SPLIT_INC_${CISCO_IPV6_SPLIT_INC}_ADDR=$NET
    export CISCO_IPV6_SPLIT_INC_${CISCO_IPV6_SPLIT_INC}_MASKLEN=$MASKLEN
    export CISCO_IPV6_SPLIT_INC=$((${CISCO_IPV6_SPLIT_INC}+1))
}

# Old function for generating NetworkManager 0.8 GConf keys 
function translateroute ()
{
    local IPADDR="${1%%/*}"
    local MASKLEN="${1##*/}"
    local OCTET1="$(echo $IPADDR | cut -f1 -d.)"
    local OCTET2="$(echo $IPADDR | cut -f2 -d.)"
    local OCTET3="$(echo $IPADDR | cut -f3 -d.)"
    local OCTET4="$(echo $IPADDR | cut -f4 -d.)"

    local NUMADDR=$(($OCTET1*16581375 + $OCTET2*65536 + $OCTET3*256 + $OCTET4))
    local NUMADDR=$(($OCTET4*16581375 + $OCTET3*65536 + $OCTET2*256 + $OCTET1))
    if [ "$ROUTESKEY" = "" ]; then
        ROUTESKEY="$NUMADDR,$MASKLEN,0,0"
    else
        ROUTESKEY="$ROUTESKEY,$NUMADDR,$MASKLEN,0,0"
    fi
}

if [ "$reason" = "make-nm-config" ]; then
    echo "Put the following into the [ipv4] section in your NetworkManager config:"
    echo "method=auto"
    COUNT=1
    for r in $ROUTES; do
        echo "routes${COUNT}=${r%%/*};${r##*/};0.0.0.0;0;"
        COUNT=$(($COUNT+1))
    done
    exit 0
fi

for r in $ROUTES; do
    addroute $r
done

for r in $ROUTES6; do
    addroute6 $r
done

exec /etc/openconnect/vpnc-script

1

我发现实现拆分隧道的最佳方法是使用vpn-slice。您基本上可以说出所需的路由,并且由vpn脚本执行路由,而不是由服务器提供什么配置:

openconnect --script 'vpn-slice 10.0.0.0/16' -b vpngateway.example.com

尽管默认情况下它不会将您的dns服务器覆盖到该服务器提供的服务器,但您必须手动列出所有域,它将解析并将其添加到您的主机文件中:

openconnect --script 'vpn-slice 10.0.0.0/16 host1' -b vpngateway.example.com
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.