Advertisement
pawn007

perl ex5

Dec 9th, 2019
659
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 1.54 KB | None | 0 0
  1. # Extract and print the names and phone numbers from
  2. # each line in a text file
  3. my $file = "C:\\Users\\baoba\\Desktop\\gateway.log";
  4.  
  5. #Create a path for output file
  6. my $file_output = "C:\\Users\\baoba\\Desktop\\report.txt";
  7.  
  8. #to read file
  9. open my $handle, "<", $file or die "Can't open $file: $!";
  10.  
  11. #To read file with write permission ">"
  12. open my $handle_output, ">", $file_output or die "Can't open $file_output: $!";
  13. my $count=0;
  14. my $count_drop=0;
  15.  
  16. # While read every line, do ...
  17. while (my $line = <$handle>) {
  18.     $count +=1;
  19.    
  20.     #If each line in each loop match with regex
  21.     # (.*\d\d:\d\d:\d\d)                will match with any word before 11:11:11
  22.     # (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) will match with anyh digit 111.111.111.111 (ip format)
  23.     # Using round bracket to group and extract date src_ip and dst_ip
  24.     if ($line =~/(.*\d\d:\d\d:\d\d).*DROP.*SRC=(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) DST=(\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}).*/) {
  25.         my $time = $1;
  26.         my $SRC =$2;
  27.         my $DST =$3;
  28.         $count_drop +=1;
  29.         # to test output
  30.         #print ("$time $SRC $DST\n");
  31.        
  32.         #Print output
  33.         print("On $time dropped packet from $SRC going to $DST.\n");
  34.        
  35.         #Write output to new file
  36.         print $handle_output "On $time dropped packet from $SRC going to $DST.\n";
  37.     }
  38. }
  39. #Output outside the loop
  40. #Please determine which output is in and outside of the loop
  41. #Remember to check \n newline at the end
  42. print("The firewall dropped a total of $count_drop packets.\n");
  43. print("Report written to $file_output.");
  44. close $handle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement