AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 778219
Accepted
Server Programmer
Server Programmer
Asked: 2016-05-22 09:51:58 +0800 CST2016-05-22 09:51:58 +0800 CST 2016-05-22 09:51:58 +0800 CST

Bash 脚本不工作

  • 772

我希望这是一个简单的答案

问题:

  1. 我将以下名为 learn-address.sh 的 bash 脚本放在以下文件夹中:

vi /etc/openvpn/netem/learn-address.sh

  1. 在 .conf 文件中添加了以下 (2) 行:
script-security 3

learn-address /etc/openvpn/netem/learn-address.sh
  1. 并对 learn-address 脚本应用以下权限:
chmod 755 /etc/openvpn/netem/learn-address.sh
  1. 但是,该脚本确实会更新 tmp 文件中的文件($ip.classid 和 $ip.dev)并正确传递变量

  2. 但是 bash 脚本不执行 tc 类和过滤命令(qdisc 没有变化)

  3. 当用户连接到 OpenVPN 时调用 learn-address 脚本时,我将在脚本上使用什么权限来执行 tc 类和过滤命令,或者我错过了什么?

非常感谢

脚本名称:learn-address.sh

#!/bin/bash

statedir=/tmp/

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


# 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
openvpn bash bandwidth tc
  • 1 1 个回答
  • 354 Views

1 个回答

  • Voted
  1. Best Answer
    Archemar
    2016-05-22T10:24:02+08:002016-05-22T10:24:02+08:00

    $dev 在两次调用 tc 时未设置,

    # 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
    

    这决心

    tc qdisc add dev  root handle 1: htb
    

    很可能有错误通过管道传送到/dev/null

    将此行替换为

    # Make sure queueing discipline is enabled.
    dev=eth0
    tc qdisc add dev $dev root handle 1: htb 2>/tmp/tqa-root.err || /bin/true
    tc qdisc add dev $dev handle ffff: ingress 2>/tmp/tqa-handle.err || /bin/true
    
    • 1

相关问题

  • OpenVPN:在哪里生成私钥?

  • 将 iPhone 连接到 OpenVPN

  • OpenVPN 的 Linux IP 转发 - 正确的防火墙设置?

  • 最好的点对点 VPN?

  • 通过 VPN 使您的打印机可用的最佳方法是什么?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve