All pastes #764344 Raw Edit

Stuff

public text v1 · immutable
·published 2007-11-07 03:03 UTC
rendered paste body
#!/usr/bin/perl

use strict;
use File::Tail;

  #########################################################
  #----------------|  constantes  |---------------------
  #########################################################
  my $conf  = 'xWall.conf';
  my $debug = 1;
  my @sect  = ( 'mod', 'sys', 'mon', 'par', 'rule' );
                
  #########################################################  
  #---------------|  declaraciones  |----------------------
  #########################################################
  my ( %x,
       %mod,
       %sys,
       @mods,       
       @tmp,
       $buf,
       $opt, 
       $val,
       $ret);
  #########################################################
  
# MAIN

load_conf();                                                                #CARGAMOS LA CONFIGURACION INICIAL DE xWall.conf

#foreach(sort keys %x) { print("\$x\{$_\} => $x{$_}\n"); } 
#modprobe(@mods);
#iptables('-nL');
exit 1;

sub load_conf()
{
    open(File, "<$conf");                                                   #SE LEE EL FICHERO DE CONFIGURACION
        @tmp = <File>;
    close(File);
    
    @tmp = grep (!/^(\#|\s+\#|\s+$)/, @tmp);                                #SE ELIMINAN LINEAS COMENTADAS TOTALES Y VACIAS
    @tmp = map  (uncomment($_), @tmp);                                      #SE ELIMINAN LINEAS COMENTADAS SOBRE CODIGO
    foreach(@tmp)  { $buf .= $_; }                                          #LLENAMOS $BUF CON TODA LA CONFIGURACION
    foreach(@sect) { parse($buf, $_); }                                     #PARSEAMOS CADA SECCION DE CONFIGURACION
    
}    

sub parse()
{    
    
    my ($sDat, $flag) = @_;                                                 #OBTENEMOS EL BUFER Y LA BANDERA DE SECCION MOD, SYSCTL ETC
    $sDat =~ s/\n/::/g;                                                     #SUBSTITUIMOS LOS RETORNOS POR DOBLE DOS PUNTOS :: PARA TRATAR TODO COMO UN UNICO TEXTO EN UNA LINEA
    $sDat =~ s/[\'\"]//g;                                                   #ELIMINAMOS COMILLAS DOBLES Y SIMPLES DEL TEXTO                      
    
    for($flag)                                                              #SELECCIONAMOS LA BANDERA PARA TRATAR CADA SECCION POR SEPARADO
    {    
        /^mod$/ &&  do
                    {
                        $sDat =~ /section_mod::(.*)end_mod::/g;             #QUE CONCUERDE LO QUE ESTA ENTRE CADA DELIMITADOR DE SECCION
                        my $xa = $1;
                        $xa =~ s/[\s*]//g;                                  #ELIMINAMOS ESPACIOS EN EL TEXTO RESULTANTE     
                        $xa =~ s/::/\n/g;                                   #SUSTITUIMOS LOS :: POR RETORNOS DENUEVO
                        
                        print($xa);
                    };
        /^sys$/ &&  do
                    {
                        $sDat =~ /section_sysctl::(.*)end_sysctl::/g;       #QUE CONCUERDE LO QUE ESTA ENTRE CADA DELIMITADOR DE SECCION
                        my $xa = $1;
                        $xa =~ s/[\s*]//g;                                  #ELIMINAMOS ESPACIOS EN EL TEXTO RESULTANTE 
                        $xa =~ s/::/\n/g;                                   #SUSTITUIMOS LOS :: POR RETORNOS DENUEVO
                        print($xa);
                    };
        /^mon$/ &&  do
                    {
                        $sDat =~ /section_monitor::(.*)end_monitor::/g;     #QUE CONCUERDE LO QUE ESTA ENTRE CADA DELIMITADOR DE SECCION
                        my $xa = $1;
                        $xa =~ s/[\s*]//g;                                  #ELIMINAMOS ESPACIOS EN EL TEXTO RESULTANTE  
                        $xa =~ s/::/\n/g;                                   #SUSTITUIMOS LOS :: POR RETORNOS DENUEVO
                        print($xa)
                    };
        /^par$/ &&  do
                    {
                        $sDat =~ /section_par::(.*)end_par::/g;             #QUE CONCUERDE LO QUE ESTA ENTRE CADA DELIMITADOR DE SECCION
                        my $xa = $1;
                        $xa =~ s/[\s*]//g;                                  #ELIMINAMOS ESPACIOS EN EL TEXTO RESULTANTE 
                        $xa =~ s/::/\n/g;                                   #SUSTITUIMOS LOS :: POR RETORNOS DENUEVO
                        print($xa)
                    };
                    
       /^rule$/ &&  do
                    {
                        $sDat =~ /section_rules::(.*)end_rules::/g;
                        my $xa;
                        my @unz = split(/::/, $1);
                        foreach(@unz) { $_ =~ /^(\s*)(\S.*)/; $xa .= "$2\n" if defined $2; }
                        $xa =~ s/::/\n/g;
                        print($xa)
                    };
    }
}
    #foreach my $shit (@pars)
#    {  
#        chomp($shit);                                                               #SACAR EL RETORNO
#        next if ($shit =~ /^\#/ or $shit eq '' or $shit =~ /^(\s*$|\s*\#)/);        #ELIMINAR SI ES UN COMENTARIO O LINEA VACIA 
#        $shit =~ /(.+\S)(|\S|\s|\s+)\=(\S|\s|\s+)(.*)/;  ($opt, $val) =  ($1, $4);  #PARSEAR LA OPCION Y EL VALOR, OPCION EN $1 VALOR EN $4 
#        if ($val =~ /^(.+|\'.*\'|\".*\")(\s+\#)/) { $val = $1;}                     #SI HAY COMENTARIOS EN LA LINEA DE CONFIG
#        $val =~ s/(\'|\"|\s+)//ig;                                                  #SI TIENE COMILLAS EL VALOR
#        $x{$opt} =  $val;
#        #print("opcion $opt \t=> $val\n");                                  #DEBUG
#    }


sub uncomment
{
    chomp($_);
    next if ($_ =~ /^(\#|\s*$|\s*\#)/);
    if ($_ =~ /^(.+|'.*'|".*")(\s*\#)/) { $_ = $1;}
    return("$_\n");
}
    
sub iptables 
{
    my $regla = shift;
    print "$x{ipt} $regla > /dev/null\n" if ($debug);
    #$result = system("$iptables $line > /dev/null");
    #if ($result != 0) {
    #    print "X: ($result) iptables $line\n";
    #}
}

sub modprobe()
{
    @mods = @_;
    foreach my $shit (@mods)
    {
        print("Añadiendo modulo: $shit\n") if($debug);
        $ret = `/sbin/modprobe $shit 2>&1 3>&1`;
        if ($ret != 0) { print("X: ($ret) modprobe $shit $!\n"); }
    }
}
#my $fichero = File::Tail->new (
#                    name        => 'help', 
#                    maxinterval => 0.7 );
#my $fichero2 = File::Tail->new (
 #                   name        => 'log', 
  #                  maxinterval => 0.7,
   #                 interval    => 1,
    #                adjustafter => 7 );

#while ( my $linea = $fichero->read ) { print $linea; }

my @arch = ('help', 'log');
my @files;
my $debug = 1;
my $timeout;

foreach (@arch) { push(@files, File::Tail->new(
                                    name   => "$_",
                                    debug  => $debug,
                                    maxinterval => 0.7 )); }

while (1) 
{
   my ($nfound, $timeleft, @pending)= File::Tail::select(undef, undef, undef, $timeout, @files);
   unless ($nfound) {
     # timeout - do something else here, if you need to
   } else {
     foreach (@pending) {
        print $_->{"input"}." (".localtime(time).") ".$_->read;
   }
 }
}