Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Extract and print the names and phone numbers from
- # each line in a text file
- my $file = "C:\\Users\\baoba\\Desktop\\gateway.log";
- #Create a path for output file
- my $file_output = "C:\\Users\\baoba\\Desktop\\report.txt";
- #to read file
- open my $handle, "<", $file or die "Can't open $file: $!";
- #To read file with write permission ">"
- open my $handle_output, ">", $file_output or die "Can't open $file_output: $!";
- my $count=0;
- my $count_drop=0;
- # While read every line, do ...
- while (my $line = <$handle>) {
- $count +=1;
- #If each line in each loop match with regex
- # (.*\d\d:\d\d:\d\d) will match with any word before 11:11:11
- # (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) will match with anyh digit 111.111.111.111 (ip format)
- # Using round bracket to group and extract date src_ip and dst_ip
- 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}).*/) {
- my $time = $1;
- my $SRC =$2;
- my $DST =$3;
- $count_drop +=1;
- # to test output
- #print ("$time $SRC $DST\n");
- #Print output
- print("On $time dropped packet from $SRC going to $DST.\n");
- #Write output to new file
- print $handle_output "On $time dropped packet from $SRC going to $DST.\n";
- }
- }
- #Output outside the loop
- #Please determine which output is in and outside of the loop
- #Remember to check \n newline at the end
- print("The firewall dropped a total of $count_drop packets.\n");
- print("Report written to $file_output.");
- close $handle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement