linux超流量停机脚本,[原]流量监控脚本 v1.1

昨天,介绍了如何在网关上利用iptables实现监控各客户端IP流量的原理,并且提供了一个简单的监控脚本。今天,我继续修改完善该脚本,工作的目标是:希望可定时运行该脚本,当发现超出限定流量的客户端时,暂时终止其IP。

一、想法

我想实现的是:

引用

1、通过crontab来定时重复运行ipflow.pl脚本;

2、当发现超出限定流量的客户端时,用iptables暂时禁用对应的IP若干秒;

3、考虑到crontab运行的时间和禁止用户的时间可能不相同,采用两个脚本;

4、禁止用户的脚本需要等待若干秒,应放在后台运行。

二、脚本

1、ipflow.pl脚本

改动原ipflow.pl脚本,内容如下:

#!/usr/bin/perl -w

# Date:2009-03-12

# Author:HyphenWang

# Version:1.1

use strict;

my $IPTABLES_CMD="/sbin/iptables -v -n -x -L FORWARD";

my $BANIP_CMD="/root/banip.sh"; #定义禁止客户端IP的脚本路径

my $SEC="3"; #定义监控时收集数据的间隔时间,取平均值

my $ZERO="0"; #定义是否输出没有流量的客户端信息

my $BANIP="1"; #定义是否启用禁止IP的功能

my $BANSEC="60"; #定义禁止时间

my $limit_input_rate=150; #定义限定的下载流量

my $limit_output_rate=20; #定义限定的上传流量

my @exclude_ip=qw(30 153 155 200 221); #定义排除列表,表中的IP被排除在禁用范围外

my (%first_input,%first_output);

my (%second_input,%second_output);

my (%ban_ip,$input_rate,$output_rate);

#定义收集流量的函数

sub get_ipflow {

my ($ip_input,$ip_output)=@_;

for my $line (`$IPTABLES_CMD`) {

my @columns = split(/\s+/,$line);

$ip_input->{$columns[-1]}=$columns[2] if ($columns[3] eq "ACCEPT" and $columns[-1] =~ m/192\.168\.228\.\d+/);

$ip_output->{$columns[-2]}=$columns[2] if ($columns[3] eq "ACCEPT" and $columns[-2] =~ m/192\.168\.228\.\d+/);

$ban_ip{$columns[-1]}=1 if ($columns[3] eq "DROP" and $columns[-1] =~ m/192\.168\.228\.\d+/);

$ban_ip{$columns[-2]}=1 if ($columns[3] eq "DROP" and $columns[-2] =~ m/192\.168\.228\.\d+/);

}

}

get_ipflow(\%first_input,\%first_output);

sleep $SEC;

get_ipflow(\%second_input,\%second_output);

#开始输出

print "Now is ".localtime()."\n";

print "-"x53,"\n";

print "IP Address\t\tIn Flow Rate\tOut Flow Rate\n";

for my $ip (keys %first_input) {

if ($ZERO != 1) {

if (defined $second_input{$ip} and defined $second_output{$ip} and int(($second_input{$ip}-$first_input{$ip})/1024/$SEC) == 0) {

next;

}

}

if (defined $second_input{$ip} and defined $second_output{$ip}) {

$input_rate = ($second_input{$ip}-$first_input{$ip})/1024/$SEC;

$output_rate = ($second_output{$ip}-$first_output{$ip})/1024/$SEC;

printf ("%s\t\t%.f KByte/s\t%.f KByte/s\n",$ip,$input_rate,$output_rate);

#禁用超出流量的IP

if ($BANIP == 1 and $input_rate >= $limit_input_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {

#传递给禁用脚本,并放到后台运行,而不会影响当前脚本的继续运行

system ("$BANIP_CMD INPUT $ip $BANSEC &");

#被禁用的IP当然需要在最后的Banned IP Address栏显示出来

$ban_ip{$ip}=1;

}

if ($BANIP == 1 and $output_rate >= $limit_output_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {

system ("$BANIP_CMD OUTPUT $ip $BANSEC &");

$ban_ip{$ip}=1;

}

}

}

print "-"x53,"\n";

print "Banned IP Address:\n";

for (keys %ban_ip) {

print "$_\n";

}

2、banip.sh脚本

这个脚本是用于接收来自ipflow.pl的信息,然后禁用对应的IP的:

#!/bin/bash

SEC="$3";

IPTABLES="/sbin/iptables"

case $1 in

"INPUT")

$IPTABLES -I FORWARD -d $2 -j DROP

;;

"OUTPUT")

$IPTABLES -I FORWARD -s $2 -j DROP

;;

esac

sleep $SEC;

case $1 in

"INPUT")

$IPTABLES -D FORWARD -d $2 -j DROP

;;

"OUTPUT")

$IPTABLES -D FORWARD -s $2 -j DROP

;;

esac

exit 0

※ 虽然我这里定义了下载和上传使用不同的命令,实际上效果是相同的——禁用那一个方向,所有流量都不能通过。我这样写,仅是为了今后统计客户端信息而考虑。

另外,正如之前所说的,不通过网关nat转发的流量是不会计算在FORWARD表中,而是在INPUT/OUTPUT表。所以,这脚本不会影响直接访问网关的流量。

本地下载:

三、定时运行

把脚本写入crontab中,让其自动运行:

引用

# crontab -l

*/1 * * * * /root/ipflow.pl

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值