如何用linux实现stream量限制

我正在帮忙组织一个小会议。 没有互联网连接,所以我们仅限于有限的移动LTE连接。

我们有一个基于Ubuntu的服务器,作为一个路由器,提供一个DHCP和DNS服务器,并从其子网192.168.1.0/24路由到LTE连接(USB棒)。

即使我们从内部networking到基于LTE的互联网的NATconfiguration工作,我们想要防止客户端使用太多的有价值的数量,并且限制每个客户端(MAC地址?)到一定数量的数据,例如100MB。 如果一个客户端达到这个限制(上传和下载的总和),我们希望被告知(一个日志条目足够),他应该被扼杀(如果可能的话)或从互联网连接切断(但他仍然应该能够在本地networking中进行通信)。

有什么机制或软件可以用于这种情况?

以下只是一个想法,因为我是stream量整形的新手。 这不是一个工作或完整的脚本,是缺less的tc部分或类似的,还有许多其他的必需品…它只是作为一个好奇心,我没有时间现在完成…

cron脚本每分钟运行

 cron * * * * * sh /path/to/bitshaper.sh /path/to/whitelist /path/to/blacklist 

bitshaper.sh

 #!/bin/sh ## limit 1MB limit=1000000 ## ip addresses that are unrestricted WHITELIST=`cat "$1"` ## ip addresses that are throttled immediately BLACKLIST=`cat "$2"` ## chain...when routing it'll be FORWARD, otherwise use INPUT for playing CHAIN='INPUT' ## working directory WD=/var/tmp/bitshaper mkdir "$WD" 2> /dev/null && cd "$WD" ## create unique CHAIN name so we can easily identify with iptables -L ## rules for monitoring bytes now should have a target of -j $RULE_ID RULE_ID='BITSHAPER' iptables -N $RULE_ID 2> /dev/null ## get byte count stats STATS=`iptables -L "$CHAIN" -vn | tail -n +3` ## get dhcpd leases HOSTS=`grep -E '^lease ' /var/lib/dhcp/dhcpd.leases | tr -d '[az {]' | sort -u` for host in $HOSTS; do case $WHITELIST in *$host*) continue;; esac success=false for stat in "$STATS"; do ## $RULE_ID has to be specific enough to not match anything else case $stat in *${RULE_ID}*${host}*) success=true tmp=${stat#*[0-9] } bytes=${tmp%% *} [ $bytes -gt $limit ] && { # use tc to shape traffic } break ;; esac done if [ $success = 'false' ]; then ## have host but no firewall rule, add one to track usage iptables -t filter -A $CHAIN -s $host -j $RULE_ID fi done ## blacklist host here or somewhere