网络限速脚本

 

在android中wifi限速主要是用到linux 的tc iptables ip 等命令,需要root 权限

下面是android wifi 限速的基本过程:

                                          1, 下载附件tc_bash.sh

                                          2, adb remount

                                          3 , adb push xx/tc_bash.sh system/

                                          4,  chmod a+x /system/tc_bash.sh

                                          5, cd /system/

                                          6, sh ./tc_bash.sh start  //开启限速现在下行200kbit 上行100kbit

                                          7, sh ./tc_bash.sh stop  //删除限速规则。

 

sh ./tc_bash.sh show 查看规则,以及具体效果的命令解图:

注意 1,由于android 提供tc 命令功能有限,所以有些在linux 上的rule 不能用,如有其他需求,具体请修改shell 脚本。2,本例子是针对播放器限速的定义的规则是针对lo设备

 

 

附件:

tc_bash.sh

 

#!/bin/bash
#
#  tc uses the following units when passed as a parameter.
#  kbps: Kilobytes per second
#  mbps: Megabytes per second
#  kbit: Kilobits per second
#  mbit: Megabits per second
#  bps: Bytes per second
#       Amounts of data can be specified in:
#       kb or k: Kilobytes
#       mb or m: Megabytes
#       mbit: Megabits
#       kbit: Kilobits
#  To get the byte figure from bits, divide the number by 8 bit
#
#
# Name of the traffic control command.
TC=/system/bin/tc
# Name of the iptables command
IPTAB=/system/bin/iptables
# Name of ifconfig
IFCONFIG=/system/bin/ifconfig
# Name of awk
AWK=/sbin/awk
# The network interface we're planning on limiting bandwidth.
#IF=wlan0             # Interface
IF=lo            # Interface
# Download limit (in mega bits)
DNLD=200kbit         # DOWNLOAD Limit
# Upload limit (in mega bits)
UPLD=100kbit         # UPLOAD Limit
IP=127.0.0.1
IP=$($IFCONFIG $IF | $AWK '{print $3}')
# IP address of the machine we are controlling
#IP=10.58.27.32 # Host IP
echo"$IP"
# Filter options for limiting the intended interface.
U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"
start() {
# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.
    $TC qdisc add dev $IF root handle 1: htb default 30
    $TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD ceil $DNLD
    $TC class add dev $IF parent 1: classid 1:2 htb rate $UPLD ceil $UPLD
    $U32 match ip dst $IP/32flowid 1:1
    $U32 match ip src $IP/32flowid 1:2
# The first line creates the root qdisc, and the next two lines
# create two child qdisc that are to be used to shape download
# and upload bandwidth.
#
# The 4th and 5th line creates the filter to match the interface.
# The 'dst' IP address is used to limit download speed, and the
# 'src' IP address is used to limit upload speed.
}
start_limit_dw() {
#删除原来的tc规则队列
#$TC  qdisc del dev $IF root
#添加tc规则队列
$TC  qdisc add dev $IF root handle 10: htb default 256
#生成根类
$TC class add dev $IF parent 10: classid 10:1 htb rate 500kbit ceil 500kbit
#支类列表用于限制速度
#这里的rate指的是保证带宽,ceil是最大带宽。
$TC  class add dev $IF  parent 10:1 classid 10:10 htb rate $DNLD ceil $DNLD prio 1
#添加支类规则队列
#采用sfq伪随机队列,并且10秒重置一次散列函数。
#android not support sfq
#$TC qdisc add dev $IF parent 10:10 handle 101: sfq perturb 10
#建立网络包过滤器,设置fw。android not support fw
#$TC filter add dev $IF parent 10: protocol ip prio 10 handle 1 fw classid 10:10
#在iptables里面设定mark值,与上面的handle值对应。
$IPTAB -t mangle -A POSTROUTING -d $IP -j MARK --set-mark 1
##删除原来的tc规则队列
#TC qdisc del dev eth0 root
#
##添加tc规则队列
#TC qdisc add dev eth0 root handle 20: htb default 256
#
##生成根类
#TC class add dev eth0 parent 20: classid 20:1 htb rate 1mbit ceil 1mbit
#
##支类列表用于限制速度
#TC class add dev eth0 parent 20:1 classid 20:10 htb rate 40kbps ceil 40kbps prio 1
#
##添加支类规则队列
#TC qdisc add dev eth0 parent 20:10 handle 201: sfq perturb 10
#
##建立网络包过滤器
#TC filter add dev eth0 parent 20: protocol ip prio 100 handle 2 fw classid 20:10
#IPTAB -t mangle -A PREROUTING -s IP -j MARK --set-mark 2
}
stop() {
# Stop the bandwidth shaping.
    $TC qdisc del dev $IF root
}
restart() {
# Self-explanatory.
    stop
    sleep1
    start
}
show() {
# Display status of traffic control status.
    echo"tc -s qdisc ls dev ${IF}"
    $TC -s qdisclsdev $IF
    echo"tc -s class ls dev ${IF}"
    $TC -s classlsdev $IF
    echo"tc -s filter ls dev ${IF}"
    $TC -s filterlsdev $IF
}
case"$1"in
  start)
    echo-n"Starting bandwidth shaping: "
    start
    echo"done"
    ;;
  stop)
    echo-n"Stopping bandwidth shaping: "
    stop
    echo"done"
    ;;
  restart)
    echo-n"Restarting bandwidth shaping: "
    restart
    echo"done"
    ;;
  show)
    echo"Bandwidth shaping status for $IF:"
    show
    echo""
    ;;
  start_limit_dw)
    echo"start ext shaping  $IF:"
    start_limit_dw
    echo"done"
    ;;
  *)
    pwd=$(pwd)
    echo"Usage: tc_bash.sh {start|start_limit_dw|stop|restart|show}"
    ;;
esac
exit0

转载于:https://my.oschina.net/u/269082/blog/633086

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值