Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121347
  • 博文数量: 17
  • 博客积分: 1652
  • 博客等级: 中尉
  • 技术积分: 200
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-02 11:33
文章分类

全部博文(17)

文章存档

2011年(2)

2010年(15)

分类:

2010-09-07 13:51:51

最近几天忙了下linux下的监控和ip-mac的绑定,我是个小菜鸟,我就把自己忙的瞎捣鼓的事记录一下,嘿嘿,大家不要嘲笑我噢,首先我要做的是流量的监控,由于我是个小菜鸟所以我的脚本水平实在有限所以嘛我就跟网上搜了一个perl的脚本,这个脚本据我使用结果才发现它是一个按/s流量来限制的,就是说假如你下载东东超过200k/s它就会在iptables里面自动添加一条DROP下面是这三个脚本的内容..
 
第一个脚本
banip.sh
# Date:2009-03-12
# Author:HyphenWang
# Version:1.0
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
 
第二个脚本:
 
ipflow_1.3
#!/usr/bin/perl -w
# Date:2009-03-14
# Author:HyphenWang
# Version:1.3
use strict;
my $IPTABLES_CMD="/sbin/iptables -v -n -x -L FORWARD";
my $BANIP_CMD="/root/banip.sh";
my $SEC="3";
my $ZERO="1";
#my $BANIP="1";
my $BANSEC="60";
#my $HTML="1" if (defined $ARGV[0] and $ARGV[0] eq "-p");
my ($HTML,$BANIP);
my $limit_input_rate=200;
my $limit_output_rate=35;
my @exclude_ip=qw(30 153 155 200 221);
my @vzserver_ip=qw(135);
);
  push (@exclude_ip,@vzserver_ip);
my (%first_input,%first_output);
my (%second_input,%second_output);
my (%ban_ip,$input_rate,$output_rate);
if (defined $ARGV[0]) {
  for (@ARGV) {
    $HTML = 1 if $_ eq "-p";
    $BANIP = 1 if $_ eq "-b";
  }
}
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);
if (! defined $BANIP) {
  if (! defined $HTML) {
    print "Now is ".localtime()."\n";
    print "-"x53,"\n";
    print "IP Address\t\tIn Flow Rate\tOut Flow Rate\n";
  }
  else {
  print<








EOF
  }
}
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;
    if (! defined $BANIP) {
      if (! defined $HTML) {
        printf ("%s\t\t%.f KByte/s\t%.f KByte/s\n",$ip,$input_rate,$output_rate);
      }
      else {
        printf ("
\n\n\n\n",$ip,$input_rate,$output_rate);
      }
    }
    if ($input_rate >= $limit_input_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {
      #system ("$BANIP_CMD INPUT $ip $BANSEC &");
      print $ip,"\n" if defined $BANIP;
      $ban_ip{$ip}=1;
    }
    if ($output_rate >= $limit_output_rate and ! grep ("192.168.228.$_" eq $ip,@exclude_ip)) {
      #system ("$BANIP_CMD OUTPUT $ip $BANSEC &");
      print $ip,"\n" if defined $BANIP;
      $ban_ip{$ip}=1;
    }
  }
}
if (! defined $BANIP) {
  if (! defined $HTML) {
    print "-"x53,"\n";
    print "Banned IP Address:\n";
  }
  else {
  print<
IP AddressIn Flow RateOut flow Rate
%s%.f KByte/s%.f KByte/s




Banned IP Address

EOF
  }
}
if (! defined $BANIP) {
  for (keys %ban_ip) {
    if (! defined $HTML) {
    print "$_\n";
    }
    else {
    print<


EOF
    }
  }
}
if (defined $HTML) {
  print<
$_



限速:200 KByte/s,若检查时发现超出该值,会自动中断对应的客户端IP一分钟。
若您被中断了,请等待,或告诉我。
EOF
}
第三个脚本
 
#!/bin/bash
for i in `/root/ipflow.pl -b`
do
  /root/banip.sh INPUT $i 60 &
done
以上三个脚本
执行 perl ipflow_1.3 就能显示实时流量
可以写个任务计划让它自动执行
crontab -e
0-59/2 * * * * perl /ipflow_1.3 > /back.log
然后每次只要 less /back.log 就能看到了.


 
阅读(2053) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~