免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2581 | 回复: 7
打印 上一主题 下一主题

如何把文本文件内容,结合ipfw命令写入带宽限制表中去? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-17 17:35 |只看该作者 |倒序浏览
现在有文本bandwidth.conf内容:
11.0.0.2 512 256
11.0.0.3 256 128
。。。。。。
11.0.0.2为限制的ip,512为下载值,256为上传值。

现在想通过shell脚本,结合ipfw带宽限制命令,批量把文本内的:ip、下载值和上传值写入带宽限制表

中去。通过这几个命令:
ipfw pipe 1 config bw 下载值Kbit/s
ipfw pipe 2 config bw 上传值Kbit/s
ipfw add 9000 pipe 1 ip from any to ip地址 in
ipfw add 9001 pipe 2 ip from ip地址 to any out
ipfw add 9002 permit ip from any to ip地址
ipfw add 9003 permit ip from ip地址 to any

请教这个脚本如何写啊

论坛徽章:
0
2 [报告]
发表于 2009-02-17 17:52 |只看该作者
awk '{
printf "ipfw pipe 1 config bw %dKbit/s\n",$2;
printf "ipfw pipe 2 config bw %dKbit/s\n",$3;
printf "ipfw add 9000 pipe 1 %s from any to ip地址 in\n",$1
}' bandwidth.conf

论坛徽章:
0
3 [报告]
发表于 2009-02-17 18:02 |只看该作者
不太明白啊

论坛徽章:
0
4 [报告]
发表于 2009-02-17 19:58 |只看该作者
while read line
do
IP=`echo "$line"|awk '{print $1}'`
DOWNLOAD=`echo "$line"|awk '{print $2}'`
UPLOAD=`echo "$line"|awk '{print $3}'`

ipfw pipe 1 config bw ${DOWNLOAD} Kbit/s
ipfw pipe 2 config bw ${UPLOAD} Kbit/s
ipfw add 9000 pipe 1 ip from any to $IP in
ipfw add 9001 pipe 2 ip from $IP to any out
ipfw add 9002 permit ip from any to $IP
ipfw add 9003 permit ip from $IP to any
done < bandwidth.conf

论坛徽章:
0
5 [报告]
发表于 2009-02-18 10:10 |只看该作者
while read line
do
IP=`echo "$line"|awk '{print $1}'`
DOWNLOAD=`echo "$line"|awk '{print $2}'`
UPLOAD=`echo "$line"|awk '{print $3}'`

ipfw pipe 1 config bw ${DOWNLOAD} Kbit/s
ipfw pipe 2 config bw ${UPLOAD} Kbit/s
ipfw add 9000 pipe 1 ip from any to $IP in
ipfw add 9001 pipe 2 ip from $IP to any out
ipfw add 9002 permit ip from any to $IP
ipfw add 9003 permit ip from $IP to any
done < bandwidth.conf
"test.sh" 17 lines, 563 characters
router# ./test.sh
while: Expression Syntax.
router# ipfw show
65535 7497 3625045 allow ip from any to any
不行,不能加入限制表

论坛徽章:
0
6 [报告]
发表于 2009-02-18 10:57 |只看该作者

回复 #5 lhm_3000 的帖子

不明白你哪的问题
我这边测试过是可以的。
你用sh -x 调试看一下

论坛徽章:
11
金牛座
日期:2015-03-19 16:56:22数据库技术版块每日发帖之星
日期:2016-08-02 06:20:00数据库技术版块每日发帖之星
日期:2016-04-24 06:20:00数据库技术版块每日发帖之星
日期:2016-04-13 06:20:00IT运维版块每日发帖之星
日期:2016-04-13 06:20:00数据库技术版块每日发帖之星
日期:2016-02-03 06:20:00数据库技术版块每日发帖之星
日期:2015-08-06 06:20:00季节之章:春
日期:2015-03-27 15:54:57羊年新春福章
日期:2015-03-27 15:54:37戌狗
日期:2015-03-19 16:56:41数据库技术版块每日发帖之星
日期:2016-08-18 06:20:00
7 [报告]
发表于 2009-02-18 10:59 |只看该作者
这个是体力活.....没什么技巧可言...

论坛徽章:
0
8 [报告]
发表于 2009-02-18 12:03 |只看该作者
已经基本搞好现在文本文件:
11.0.0.2 500 80 1
11.0.0.3 4096 2048 2
11.0.0.4 512 80 3
......
11.0.0.2为地址,500为下行,80为上行值,1为序号
脚本修改了以下:
router# cat test.sh
fwcmd=/sbin/ipfw
$fwcmd flush
$fwcmd pipe flush

while read line
do
usernum=`echo "$line"|awk '{print $4}'`
pipein=`echo $usernum*2 | bc`
pipeout=`expr $pipein + 1`
fwrulein=`expr $pipein + 1000`
fwruleout=`expr $fwrulein + 1`
fwholein=`expr $pipein + 33000`
fwholeout=`expr $fwholein + 1`
#LIST=`echo "$line"|awk '{print $4}'`
#PIPE=`expr $LIST + 1`

IP=`echo "$line"|awk '{print $1}'`
DOWNLOAD=`echo "$line"|awk '{print $2}'`
UPLOAD=`echo "$line"|awk '{print $3}'`

$fwcmd pipe $pipein config bw ${DOWNLOAD}Kbit/s
$fwcmd pipe $pipeout config bw ${UPLOAD}Kbit/s
$fwcmd add $fwrulein pipe $pipein ip from any to $IP in
$fwcmd add $fwruleout pipe $pipeout ip from $IP to any out
$fwcmd add $fwholein permit ip from any to $IP
$fwcmd add $fwholeout permit ip from $IP to any
done < /usr/local/etc/mpd/bandwidth.conf

现在规则可以加入了,请看:
router# ipfw pipe show
00002: 500.000 Kbit/s    0 ms   50 sl. 1 queues (1 buckets) droptail
    mask: 0x00 0x00000000/0x0000 -> 0x00000000/0x0000
BKT Prot ___Source IP/port____ ____Dest. IP/port____ Tot_pkt/bytes Pkt/Byte Drp
  0 tcp      58.60.14.45/80           11.0.0.2/2640    76     8660  0    0   0
00003:  80.000 Kbit/s    0 ms   50 sl. 0 queues (1 buckets) droptail
00004:   4.096 Mbit/s    0 ms   50 sl. 0 queues (1 buckets) droptail
00005:   2.048 Mbit/s    0 ms   50 sl. 0 queues (1 buckets) droptail
00006: 512.000 Kbit/s    0 ms   50 sl. 0 queues (1 buckets) droptail
00007:  80.000 Kbit/s    0 ms   50 sl. 0 queues (1 buckets) droptail
router# ipfw show
01002     76      8660 pipe 2 ip from any to 11.0.0.2 in
01003      0         0 pipe 3 ip from 11.0.0.2 to any out
01004      0         0 pipe 4 ip from any to 11.0.0.3 in
01005      0         0 pipe 5 ip from 11.0.0.3 to any out
01006      0         0 pipe 6 ip from any to 11.0.0.4 in
01007      0         0 pipe 7 ip from 11.0.0.4 to any out
33002     76      8660 allow ip from any to 11.0.0.2
33003      0         0 allow ip from 11.0.0.2 to any
33004      0         0 allow ip from any to 11.0.0.3
33005      0         0 allow ip from 11.0.0.3 to any
33006      0         0 allow ip from any to 11.0.0.4
33007      0         0 allow ip from 11.0.0.4 to any
65535 136273 137116246 allow ip from any to any


谢谢大家!
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP