如何用linux实现流量限制

如何用linux实现流量限制,第1张

概述我正在帮忙组织一次小型会议.没有互联网连接,因此我们仅限于体积有限的移动LTE连接. 我们有一个基于Ubuntu的服务器,充当路由器,提供DHCP和DNS服务器,并从其子网192.168.1.0/24路由到LTE连接(USB Stick). 即使我们从内部网络到基于LTE的互联网的NAT配置有效,我们也希望防止客户端使用过多的有价值的卷,并将每个客户端(MAC地址?)限制为一定数量的数据,例如: 我正在帮忙组织一次小型会议.没有互联网连接,因此我们仅限于体积有限的移动LTE连接.

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

即使我们从内部网络到基于LTE的互联网的NAT配置有效,我们也希望防止客户端使用过多的有价值的卷,并将每个客户端(MAC地址?)限制为一定数量的数据,例如: 100MB.如果客户端达到该限制(上行和下载的总和),我们希望被通知(日志条目足够),他应该受到限制(如果可能)或切断互联网连接(但他应该仍然是能够在本地网络中进行通信).

我们可以在这种情况下使用任何机制或软件吗?

解决方法 以下只是一个想法,因为我是流量整形的新手.它不是一个工作或完整的脚本,并且缺少tc部分或类似物品以及许多其他必需品……它只是作为一种好奇心呈现,我现在没有时间完成……

cron脚本每分钟运行一次

cron * * * * * sh /path/to/bitshaper.sh /path/to/whiteList /path/to/blackList

bitshaper.sh

#!/bin/sh## limit 1MBlimit=1000000## ip addresses that are unrestrictedWHITEList=`cat ""`## ip addresses that are throttled immediatelyBLACKList=`cat ""`## chain...when routing it'll be FORWARD,otherwise use input for playingCHAIN='input'## working directoryWD=/var/tmp/bitshapermkdir "$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_IDRulE_ID='BITSHAPER'iptables -N $RulE_ID 2> /dev/null## get byte count statsSTATS=`iptables -L "$CHAIN" -vn | tail -n +3`## get dhcpd leasesHOSTS=`grep -E '^lease ' /var/lib/dhcp/dhcpd.leases | tr -d '[a-z {]' | 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  fidone## blackList host here or somewhere
总结

以上是内存溢出为你收集整理的如何用linux实现流量限制全部内容,希望文章能够帮你解决如何用linux实现流量限制所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1035964.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存