通过脚本传递的 OpenVPN 变量

通过脚本传递的 OpenVPN 变量

有人可以解释和/或指导我了解客户端连接时可传递给 OpenVPN 的变量摘要吗?

例如,以下 $1、$2、$3、$4 值会为变量产生什么结果:

ip=$1
user=$2
?=$3
?=$4
?=$5

ETC

需要澄清的是:当用户连接到 OpenVPN 时,将调用以下学习地址脚本(请参阅下文)

我想知道用户连接后可以传递给这个 bash 脚本的变量是什么

这是学习地址脚本和前 (2) 个变量(在脚本顶部)$1 和 $2 - 还有其他我们可以捕获的变量吗(eth0 vs dev1,等等)?

#!/bin/bash

statedir=/tmp/

function bwlimit-enable() {
    ip=$1
    user=$2

    # Disable if already enabled.
    bwlimit-disable $ip

    # Find unique classid.
    if [ -f $statedir/$ip.classid ]; then
        # Reuse this IP's classid
        classid=`cat $statedir/$ip.classid`
    else
        if [ -f $statedir/last_classid ]; then
            classid=`cat $statedir/last_classid`
            classid=$((classid+1))
        else
            classid=1
        fi
        echo $classid > $statedir/last_classid
    fi

    # Find this user's bandwidth limit
    # downrate: from VPN server to the client
    # uprate: from client to the VPN server
    if [ "$user" == "myuser" ]; then
        downrate=10mbit
        uprate=10mbit
    elif [ "$user" == "anotheruser"]; then
        downrate=2mbit
        uprate=2mbit
    else
        downrate=5mbit
        uprate=5mbit
    fi

    # Limit traffic from VPN server to client
    tc class add dev $dev parent 1: classid 1:$classid htb rate $downrate
    tc filter add dev $dev protocol all parent 1:0 prio 1 u32 match ip dst $ip/32 flowid 1:$classid

    # Limit traffic from client to VPN server
    tc filter add dev $dev parent ffff: protocol all prio 1 u32 match ip src $ip/32 police rate $uprate burst 80k drop flowid :$classid

    # Store classid and dev for further use.
    echo $classid > $statedir/$ip.classid
    echo $dev > $statedir/$ip.dev
}

function bwlimit-disable() {
    ip=$1

    if [ ! -f $statedir/$ip.classid ]; then
        return
    fi
    if [ ! -f $statedir/$ip.dev ]; then
        return
    fi

    classid=`cat $statedir/$ip.classid`
    dev=`cat $statedir/$ip.dev`

    tc filter del dev $dev protocol all parent 1:0 prio 1 u32 match ip dst $ip/32
    tc class del dev $dev classid 1:$classid

    tc filter del dev $dev parent ffff: protocol all prio 1 u32 match ip src $ip/32

    # Remove .dev but keep .classid so it can be reused.
    rm $statedir/$ip.dev
}

# Make sure queueing discipline is enabled.
tc qdisc add dev $dev root handle 1: htb 2>/dev/null || /bin/true
tc qdisc add dev $dev handle ffff: ingress 2>/dev/null || /bin/true

case "$1" in
    add|update)
        bwlimit-enable $2 $3
        ;;
    delete)
        bwlimit-disable $2
        ;;
    *)
        echo "$0: unknown operation [$1]" >&2
        exit 1
        ;;
esac

exit 0

答案1

当用户连接到 OpenVPN 时,将调用以下 learn-address 脚本

$1、$2 和 $3 是传递给脚本的参数,在手册页中有记录。

--learn-address cmd

...

Three arguments will be appended to any arguments in cmd as follows:

[1] operation -- "add", "update", or "delete" based on whether or not 
    the address is being added to, modified, or deleted from OpenVPN's
    internal routing table. 
[2] address -- The address being learned or unlearned. This can be an IPv4 
    address such as "198.162.10.14", an IPv4 subnet such as "198.162.10.0/24", 
    or an ethernet MAC address (when --dev tap is being used) such 
    as "00:FF:01:02:03:04". 
[3] common name -- The common name on the certificate associated with the 
    client linked to this address. Only present for "add" or "update" 
    operations, not "delete".

答案2

您提到的变量与 OpenVPN 无关。

相反,它们是 bash 变量,指的是传递给脚本的命令行参数。$1指的是第一个参数,$2指的是第二个参数,等等。

相关内容