Код: Выделить всё
#!/usr/bin/perl
use DBD::mysql;
use DBI();
use Time::Local;
use Getopt::Std;
use Socket;
#Load command line args
getopts('f:c:n:m:p:i:z:drle', \%opts);
$opt_f = $opts{f};
$opt_c = $opts{c};
$opt_r = $opts{r};
$opt_n = $opts{n};
$opt_m = $opts{m};
$opt_p = $opts{p};
$opt_d = $opts{d};
$opt_z = $opts{z};
if ($opt_c eq "" or ($opt_n eq "" and $opt_m eq "" and $opt_z eq "" and $opts{l} eq "")){
print "usage: $0\n";
print "\t-f file1\n";
print "\t-c tc_class\n";
print "\t-n our_net\n";
print "\t-m our_mask \n";
print "\t[-z] file2\n";
print "\t[-p] our_port \n";
print "\t[-r] incoming \n";
print "\t[-l] local nets\n";
print "\t[-e] clean traffic class before insert\n";
print "\t[-d] debug\n";
exit (0);
};
#Load settings from config
open (CONFIG, "/netup/utm/utm.cfg");
@config = <CONFIG>;
close (CONFIG);
foreach $line (@config) {
if ($line =~ m/^([^#].+?)=(.*)$/) {
$$1 = $2;
};
};
# Connect to the database
if ($database_type eq "mysql") {
$DBI_data="DBI:$database_type:database=$database;host=$database_host;mysql_socket=$database_sock_path;";
} elsif ($database_type eq "postgres") {
$DBI_data="DBI:$database_type:dbname=$database";
} else {
print "Unknown database $database_type! Stopped. \n";
exit (1);
};
$dbh = DBI->connect("$DBI_data","$database_login","$database_password",{'RaiseError' => 1});
%locals = ();
$count=0;
$opt_d = ($opt_d > 0) ? 1 : 0;
# OPEN FILE AND PARSE
open (NETS,"$opt_f");
@nets1 = <NETS>;
close (NETS);
# OPEN FILE AND PARSE
open (NETS,"$opt_z");
@nets2 = <NETS>;
close (NETS);
if($opts{e}){
# erase all in tlcass
if ($opt_d)
{
print "DELETE FROM traffic_classes_nets WHERE cid='$opt_c'\n";
}
else
{
$dbh->do("DELETE FROM traffic_classes_nets WHERE cid='$opt_c'");
}
};
foreach (@nets1){
if ($_ =~ m/^([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)[\/]*([0-9]*)[\s\t]+([0-9]*)$/) {
$net = "$1";
$mask = ($2 > 0) ? "$2" : "32";
$port = ($3 > 0) ? "$3" : "0";
$bin_net = unpack("N",inet_aton($net));
$bin_mask = unpack ("N", pack("B32","1"x$mask . "0"x (32-$mask)));
$bin_port = $port * 1;
if($opts{z}){
foreach (@nets2){
if ($_ =~ m/^([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)[\/]*([0-9]*)[\s\t]+([0-9]*)$/) {
$net2 = "$1";
$mask2 = ($2 > 0) ? "$2" : "32";
$port2 = ($3 > 0) ? "$3" : "0";
$bin_our_net = unpack("N",inet_aton($net2));
$bin_our_mask = unpack ("N", pack("B32","1"x$mask2 . "0"x (32-$mask2)));
$bin_our_port = $port2 * 1;
if ($opts{r}){
insert_row($opt_c, $bin_our_net, $bin_our_mask, $bin_our_port, $bin_net, $bin_mask, $bin_port);
}else{
insert_row($opt_c, $bin_net, $bin_mask, $bin_port, $bin_our_net, $bin_our_mask, $bin_our_port);
};
};
};
}
else{
$bin_our_net = unpack("N",inet_aton($opt_n));
$bin_our_mask = unpack("N",inet_aton($opt_m));
$bin_our_port = $opt_p * 1;
if($opts{l}){
# local nets !
# rotating
insert_row($opt_c, $bin_net, $bin_mask, $bin_port, $bin_net, $bin_mask, $bin_port);
while (($key, $value) = each(%locals)){
($bin_our_net, $bin_our_mask, $bin_our_port) = split /[\s\t]+/, $value;
insert_row($opt_c, $bin_net, $bin_mask, $bin_port, $bin_our_net, $bin_our_mask, $bin_our_port);
insert_row($opt_c, $bin_our_net, $bin_our_mask, $bin_our_port, $bin_net, $bin_mask, $bin_port);
};
$locals{$bin_net} = "$bin_net $bin_mask $bin_port";
}else{
if ($opts{r}){
insert_row($opt_c, $bin_net, $bin_mask, $bin_port, $bin_our_net, $bin_our_mask, $bin_our_port);
}else{
insert_row($opt_c, $bin_our_net, $bin_our_mask, $bin_our_port, $bin_net, $bin_mask, $bin_port);
};
};
};
};
};
sub insert_row{
my $tclass = shift;
my $bin_net = shift;
my $bin_mask = shift;
my $bin_port = shift;
my $bin_our_net = shift;
my $bin_our_mask = shift;
my $bin_our_port = shift;
$count++;
if ($opt_d)
{
print "INSERT INTO traffic_classes_nets(id,cid,sub_class,ip_from,netmask_from,ip_to,netmask_to,srcport,dstport,input,output,src_as,dst_as,tcp_flags,prot,tos,nexthop) VALUES ('NULL', '$opt_c', '$count', '$bin_net', '$bin_mask', '$bin_our_net', '$bin_our_mask', '$bin_port', '$bin_our_port', '0', '0', '0', '0', '0', '0', '0', '0')\n"
}
else
{
$dbh->do("INSERT INTO traffic_classes_nets(id,cid,sub_class,ip_from,netmask_from,ip_to,netmask_to,srcport,dstport,input,output,src_as,dst_as,tcp_flags,prot,tos,nexthop) VALUES ('NULL', '$opt_c', '$count', '$bin_net', '$bin_mask', '$bin_our_net', '$bin_our_mask', '$bin_port', '$bin_our_port', '0', '0', '0', '0', '0', '0', '0', '0')");
}
};