Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.15 KB | None | 0 0
  1. #!/usr/bin/env perl
  2. #Juan J. Fernandez
  3. ##########################
  4. #Insert Mikrotik log to DB
  5. ##########################
  6.  
  7. use DBI;
  8.  
  9. ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
  10.  
  11. my @abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
  12. if ($hour > 11) {
  13.   $ampm = "PM";
  14. } else {$ampm = "AM";}
  15.  
  16. $sep = "_"; #separar fecha del file
  17.  
  18. #year en dos digitos
  19. $year += 1900;
  20. $year = sprintf("%02d", $year % 100);
  21.  
  22. #today
  23. my $fecha = "$mday" . "/" . "$abbr[$mon]" . "/" . "$year";
  24. my $file = "$mday" . $sep . "$abbr[$mon]" . $sep . "$hour" . "$ampm" . ".txt";
  25.  
  26. #ahora van rutinas de expreciones regulares para insertar
  27. #la informacion en las tablas
  28. my $driver = 'DBI:mysql';
  29. my $database = "mikrotik_db";
  30. my $user = 'root';
  31. my $password = '2011@Sagrado';
  32. my $host = 'localhost';
  33.  
  34. my $dbh = DBI->connect("$driver:$database:$host","$user","$password")
  35.             or die "Can't connect: " . DBI->errstr;                        
  36.  
  37. open(my $FH, "<", "$file")
  38.   or die "Could not open filehandle: $!\n";
  39.  
  40. my $NASip;
  41. my $AccessRequest;
  42. my @MacAddress;
  43. my @ConnectionType;
  44. my @user;
  45. my $count = 0;
  46. my $hora;
  47. my $FromIP;
  48.  
  49. while(<$FH>) {
  50. ##########################################################################
  51.   if (/LOG Received at (...) (..?) (\d\d[:]\d\d[:]\d\d).*Access Request/) {
  52.      $AccessRequest = 1;
  53.      $hora = $3;
  54.   }
  55.     if (/Computer.*([0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2}[:][0-9A-F]{2})$/) {
  56.       $MacAddress[$count] = $1 if $AccessRequest;
  57.     }
  58.     if (/Username --------------> (\w*)/) {
  59.       $user[$count] = $1 if $AccessRequest;
  60.     }
  61.     if (/From IP.*(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/){
  62.       $FromIP = $1 if $AccessRequest;
  63.     }
  64.     if (/NAS IP Address.*(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/) { #ultimo sub-segmento del segmento Access Request
  65.       $NASip = $1 if $AccessRequest;
  66.       $count++;
  67.       $AccessRequest = "";
  68.     }
  69.    
  70.     #Agregar a base de datos
  71.     if($MacAddress[$count] and $user[$count] and $FromIP and $hora){
  72.     $request = 'Sending RADIUS authentication request / Access Request';
  73.     $query = "INSERT INTO event_log(username,macaddress,fecha,hora,ip,evento) VALUES(?,?,?,?,?,?)";
  74.     my $sth=$dbh->prepare($query)
  75.       or die "unable to make the request to the db\n" . DBI->errstr;
  76.     $sth->execute($user[$count],$MacAddress[$count],$fecha,$hora,$FromIP,$request);
  77.     $FromIP="";
  78.    
  79.      }
  80. ##########################################################################
  81. if(@MacAddress){
  82.   if (/LOG Received at ... ..? (\d\d[:]\d\d[:]\d\d)\s(\w+).*(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b) logged out\s(\w+ \w+)/) {
  83.     my $evento = "logged out $4";print "done\n";
  84.     my $currentCount = $count - 1;
  85.     $query = "INSERT INTO event_log(username,macaddress,fecha,hora,ip,evento) VALUES(?,?,?,?,?,?)";
  86.     my $sth=$dbh->prepare($query)
  87.       or die "unable to make the request to the db\n" . DBI->errstr;
  88.     $sth->execute($2,$MacAddress[$currentCount],$fecha,$1,$3,$evento);
  89.   }}
  90. if(@MacAddress){
  91.   if (/LOG Received at ... ..? (\d\d[:]\d\d[:]\d\d)\s(\w+) failed to login with MT Host IP (\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/)
  92.   {
  93.       my $evento = "FAILED LOGGING ATTEMPT";print "failed attempt done\n";
  94.       my $currentCount = $count - 1;
  95.       $query = "INSERT INTO event_log(username,macaddress,fecha,hora,ip,evento) VALUES(?,?,?,?,?,?)";
  96.       my $sth=$dbh->prepare($query) or die "unable to make the request to the db\n" . DBI->errstr;
  97.       $sth->execute($2,$MacAddress[$currentCount],$fecha,$1,$3,$evento);
  98.   }}
  99. if(@MacAddress){
  100.   if(/LOG Received at ... ..? (\d\d[:]\d\d[:]\d\d)\s(\w+) is trying to log in to the LAN through the Web interface from (\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/)
  101.   {
  102.      my $evento = "trying yo log in to the LAN from the Web interface";
  103.      my $currentCount = $count - 1;
  104.      $query = "INSERT INTO event_log(username,macaddress,fecha,hora,ip,evento) VALUES(?,?,?,?,?,?)";
  105.      my $sth=$dbh->prepare($query) or die "unable to make the request to the db\n" . DBI->errstr;
  106.      $sth->execute($2,$MacAddress[$currentCount],$fecha,$1,$3,$evento);
  107.   }}  
  108. }
  109.  
  110. $dbh->disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement