может есть проще решение чем это.
trafstat_kill.pl
Код: Выделить всё
#!/usr/local/bin/perl
use DBI;
$db_user="root";
$db_pass="pass";
$db_name = "radius";
$radcheck_table = "radcheck";
$radacct_table = "radacct";
$deny_script = "/bin/sh /home/cron/deny_ip.sh";
$mpd_console_ip = "127.0.0.1";
$mpd_console_port = "5005";
$mpd_console_login = "admin";
$mpd_console_passwd = "password";
my %in_lim, %in_traf;
$dbh = DBI->connect("dbi:mysql:dbname=" . $db_name, $db_user, $db_pass) or die "Cannot connect to DB server:" . $DBI::errstr . "\n";
# Reading limits
$sth = $dbh->prepare("select username, trafinmb from ".$radcheck_table);
$sth->execute();
while (($user, $in_l) = $sth->fetchrow_array()){
if($in_l != "") {$in_lim{$user} = $in_l;} else {$in_lim{$user} = 0;};
$in_traf{$user} = "0";
};
# Reading traffic info
$sth = $dbh->prepare("select username, acctoutputoctets from ".$radacct_table);
$sth->execute();
while (($user, $in) = $sth->fetchrow_array()){$in_traf{$user} += $in;};
# Incoming limits
for $key (sort(keys(%in_lim))){
if (($in_lim{$key} >= 0) && ($in_traf{$key} >= $in_lim{$key})){
system("$deny_script \"$key\" $mpd_console_ip $mpd_console_port \"$mpd_console_login\" \"$mpd_console_passwd\" > /dev/null 2>&1");
};
};
# Close connection to the DB
$dbh->disconnect() or warn "Error disconnecting from DB: " . $DBI::errstr . "\n";
deny_ip.sh
Код: Выделить всё
#!/bin/sh
NAME="$1" # username to block
MHOST="$2" # mpd host
MPORT="$3" # mpd telnet port
MLOGIN="$4" # mpd console login
MPASSWD="$5" # mpd console password
test -z "$NAME" && exit 1
test -z "$MHOST" && exit 1
test -z "$MPORT" && exit 1
test -z "$MLOGIN" && exit 1
test -z "$MPASSWD" && MPASSWD=""
for bundle in `/usr/bin/printf "${MLOGIN}\n${MPASSWD}\nbundle\nexit\n" | /usr/bin/nc $MHOST $MPORT | grep Opened | awk '{print $1}'`; do
if /usr/bin/printf "${MLOGIN}\n${MPASSWD}\nbundle $bundle\nshow bund\nexit\n" | /usr/bin/nc $MHOST $MPORT | grep "\"${NAME}\"" > /dev/null 2>&1
then
/usr/bin/printf "${MLOGIN}\n${MPASSWD}\nbundle $bundle\nclose phys\nexit\n" | /usr/bin/nc $MHOST $MPORT >/dev/null 2>&1
fi
echo
done;
exit 0;