Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 371
  • Last Modified:

Perl Pattern searching help with ftp flows

I apologize in advance for my ignorance in perl, I'm unfortunately a beginner and am attempting teach myself as I go along with some video tutorials and books and help from you guys.

Ok, so I wrote a script in bash to show the entire flow of a ftp connection by searching username or IP address. I had it read the data into an array, search for criteria, and then match that process id with others so I would get the entire flow.

The performance however was extremely slow and from suggestion of others on the experts exchange community I decided to give it a try in perl.  I am attempting to learn as much as I can but still have a long way to go. I'm attempting to search for criteria, take the process id of that line, and then read all the lines into an array that matches that process id so I'm basically getting the entire flow of the ftp connection.  

I'm assuming I would read each line in from the file, do a pattern match on it and if it matches to the IP address that I'm searching for I would then copy that line to an array.  I'm then thinking that after I read those lines into the array I'll go back and grab the process id from each of those lines, do another search on the file and put all the lines matching the process id into a new array, and then print the array out.  

Does this sound about right? Any suggestions would help. Thanks.

examples of data in log file:
Dec  1 23:59:03 sslmftp1 ftpd[4152]: USER xxxxxx  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: PASS password  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: FTP LOGIN FROM 172.19.x.xx [172.19.x.xx], xxxxxx  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: PWD  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: CWD /test/data/872507/  
Dec  1 23:59:03 sslmftp1 ftpd[4152]: TYPE Image

Open in new window

0
dloszewski
Asked:
dloszewski
1 Solution
 
ozoCommented:
I'm not sure what you are wanting to do with that log file.
Can you give an example of what the result of processing that data should look like?
0
 
wilcoxonCommented:
Personally, I'd use Tie::File.
use strict;
use warnings;
use Tie::File;
use Fcntl 'O_RDONLY';
my $log = shift or die "Usage: $0 logfile ip\n";
my $ip = shift or die "Usage: $0 logfile ip\n";
tie my @file, 'Tie::File', $log, mode => O_RDONLY or die "could not open $log: $!";
# get pids
my %tmp;
foreach my $pid (map { / ftpd\[(\d+)\]/; $1 } grep /\Q$ip\E/, @file) {
    $tmp{$pid}++;
}
my @tmp = sort { $a <=> $b } keys %tmp;
print "found multiple pids (@tmp) for $ip\n" if (@tmp > 1);
# get log rows based on pid
my $rx = join '|', @tmp;
my @rows = grep / ftpd\[(?:$rx)\]/, @file;
print @rows;
# do whatever else you want with the rows for the ip specified

Open in new window

0
 
dloszewskiAuthor Commented:
Awesome, thanks!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now