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.
I have the following code which is being used to match up lines of a file based on whether or not it matches a pattern from an array.
The array @pids has the following for data, but has several hundreds more than this:
4682
4690
4692
4693
4696
5320
If the line I'm reading in has this number in it then I push it to a new array. Once it gets to the end of the file it goes back to the beginning of the file and works on matching the next element of the array @pids. I then print the new array out to a file.
Unfortunately, The loop is taking forever, is there any way I can speed this up? I'm assuming because I'm keep going through the file over and over again, making things a bit repetitive but not sure how else I should do it.
seek INPUT, 0, 0;
my @flow;
my $count = 0;
my $pid_count = 0;
foreach my $mPID(@pids){
while(my $line = <INPUT>){
if ($line =~ /$mPID/){
push @flow, $line;
}
}
push @flow, "###############\n";
seek INPUT, 0, 0;
}
open (OUTPUT, '>'.$output) or die "Couldn't read $output.\n";
print OUTPUT @flow;
close (OUTPUT);
Here's an example of the data coming from :
Dec 1 23:59:03 ftp1 ftpd[4152]: PASV
Dec 1 23:59:04 ftp1 ftpd[4152]: NLST
Dec 1 23:59:04 ftp1 ftpd[4152]: FTP session closed
Dec 1 23:59:05 ftp1 ftpd[4682]: USER test1
Dec 1 23:59:05 ftp1 ftpd[4682]: PASS password
Dec 1 23:59:08 ftp1 ftpd[4682]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prd
Example of data I'm getting all the pids matching the IP from:
Dec 1 23:59:08 ftp1 ftpd[4682]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prd
Dec 1 23:59:10 ftp1 ftpd[4690]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prod1
Dec 1 23:59:10 ftp1 ftpd[4692]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prod
Dec 1 23:59:11 ftp1 ftpd[4693]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 1 23:59:14 ftp1 ftpd[4696]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 1 23:59:40 ftp1 ftpd[5320]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 1 23:59:47 ftp1 ftpd[5325]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prd
Dec 1 23:59:48 ftp1 ftpd[5328]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prod1
Dec 1 23:59:49 ftp1 ftpd[5329]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prod
Dec 1 23:59:49 ftp1 ftpd[5330]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 2 00:00:09 ftp1 ftpd[9876]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 2 00:00:25 ftp1 ftpd[12830]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], test1
Dec 2 00:00:25 ftp1 ftpd[12832]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prd
Dec 2 00:00:27 ftp1 ftpd[12850]: FTP LOGIN FROM 192.168.0.2 [192.168.0.2], prod1
Thanks!