Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/perl perl
- use Time::Piece;
- use Excel::Writer::XLSX;
- my $date;
- my $ResultsFile;
- my @WholefileString; #this is the whole file divided up by lines
- my @ArrayofFiles; #holds File names
- my @SA; #has a System Admin
- my @NoSA; #does not have a system Admin
- my $MyoutFile;
- sub readFile
- {
- print "\n Combine IP Addresses from Scaned Files.\n";
- print "\n\n1) CSV files must have double pipette symbols || at the end of each row.\n";
- print "2) GSARS or System Admin File should be entered first.\n";
- print "3) Security_Scans_mm-dd-yy.csv will be created with the results.\n\n";
- print "Enter the number of files to be matched. (2-10) \n";
- $num=<STDIN>;
- for($i=1; $i<=$num; $i++)
- {
- if($i eq 1)
- {
- print "Path of SA File: ";
- $Fname=<STDIN>;
- push (@fileholder,$Fname);
- }
- else
- {
- print "Path of file $i: ";
- $Fname=<STDIN>;
- push (@fileholder,$Fname);
- }
- }
- push (@ArrayofFiles,@fileholder);
- &BuildArrays;
- }
- sub BuildArrays
- {
- $TotalFiles=@ArrayofFiles;
- $Banner = ();
- local $/="||\n"; #This is the EOL Symbol
- for($i=0; $i<$TotalFiles; $i++)
- {
- $FiletoOpen=$ArrayofFiles[$i];
- open (INPUT, $FiletoOpen) or die "$FiletoOpen is not in this Path\n\n\n";
- @WholefileString = <INPUT>;
- chop $WholefileString[0];
- @Header = split(",",$WholefileString[0]);
- $Hlength = @Header;
- push(@HeaderLengths,$Hlength);
- chop $WholefileString[0];
- chop $WholefileString[0];
- chop $Banner;
- $Banner = $Banner.$WholefileString[0].",\n";
- shift(@WholefileString);
- @SortedIP = sort {(split ',', $a)[0] cmp (split ',', $b)[0]} @WholefileString;
- push(@SortedIP,NULL);
- foreach(@SortedIP )
- {
- push (@{$ArrayofFiles[$i]},$_);
- }
- @out=();
- @WholefileString =();
- }
- &GetSpacing;
- &CombineData;
- }
- sub GetSpacing
- {
- push(@HeaderLengths2,@HeaderLengths);
- for($i=2; $i<$TotalFiles; $i++)
- {
- $value = $HeaderLengths2[$i];
- $spaces = $value + $HeaderLengths2[$i-1];
- $HeaderLengths2[$i] = $spaces;
- }
- $value = $HeaderLengths[0];
- $str=();
- for($i=0; $i<$value-2; $i++)
- {
- $str =$str ."N/A,";
- }
- $NoContact=$str;
- for($i=0; $i<$TotalFiles; $i++)
- {
- $spaces = $HeaderLengths[$i];
- $str=();
- for($j=0; $j <$spaces; $j++)
- {
- $str =$str .",";
- }
- push(@StandaredSpace,$str);
- }
- for($i=0; $i<$TotalFiles; $i++)
- {
- $spaces = $HeaderLengths2[$i];
- $str=();
- for($j=0; $j <$spaces; $j++)
- {
- $str =$str .",";
- }
- push(@SkipedFileSpace,$str);
- }
- }
- sub CombineData
- {
- $found = 0;
- print "\n\n Matching in progress. \n\n";
- for($i=1; $i<$TotalFiles; $i++) #THIS IS THE FILE LOOP
- {
- for($ii=0; $ArrayofFiles[$i][$ii] ne NULL; $ii++) #FILE 1 IP
- {
- if($ArrayofFiles[$i][$ii] ne CHECKED)
- {
- @templine=split(',',$ArrayofFiles[$i][$ii]);
- $IP = @templine[0].',';
- for($j=$i; $j<$TotalFiles; $j++) #THIS IS THE second file LOOP
- {
- for($jj=0; $ArrayofFiles[$j][$jj] ne NULL; $jj++) #THIS IS THE IP2
- {
- if($ArrayofFiles[$j][$jj] ne CHECKED)
- {
- $IP2=$ArrayofFiles[$j][$jj];
- if($IP2=~ m{$IP})
- {
- chop $ArrayofFiles[$j][$jj]; #remove EOL
- chop $ArrayofFiles[$j][$jj]; #remove '|'
- chop $ArrayofFiles[$j][$jj]; #remove last '|'
- push(@TEMP,$ArrayofFiles[$j][$jj]);
- $ArrayofFiles[$j][$jj]="CHECKED";
- $found=1;
- }
- else
- {
- if($found eq 1)
- {
- $found =0;
- last;
- }
- }
- }
- $found =0;
- }
- &CreateFinalArrays($i,$j);
- }
- &FindSystemAdmin;
- @MATCH=();
- }
- }
- print "Finished scanning file $ArrayofFiles[$i]";
- }
- &Finalprint;
- }
- sub CreateFinalArrays
- {
- $first=$_[0];
- $space=$_[1];
- $Tlength=@TEMP;
- $Mlength=@MATCH;
- if($Tlength ne 0) #if temp has an element
- {
- if($Mlength eq 0) #if match length =0
- {
- if($first eq 1)
- {
- foreach(@TEMP)
- {
- push(@MATCH,$_)
- }
- }
- else
- {
- foreach(@TEMP)
- {
- $string = $SkipedFileSpace[$space-1].$_;
- push(@MATCH,$string)
- }
- }
- }
- else
- {
- if($Tlength eq $Mlength)
- {
- for($k=0; $k<$Tlength; $k++)
- {
- $string = $MATCH[$k].",".$TEMP[$k];
- $MATCH[$k]=$string;
- }
- }
- elsif($Tlength < $Mlength)
- {
- for($k=0; $k<$Mlength; $k++)
- {
- if($k< $Tlength)
- {
- $string = $MATCH[$k].",".$TEMP[$k];
- $MATCH[$k]=$string;
- }
- else
- {
- $string = $MATCH[$k].$StandaredSpace[$space];
- $MATCH[$k]=$string;
- }
- }
- }
- else #($Tlength > $Mlength)
- {
- for($k=0; $k<$Tlength; $k++)
- {
- if($k< $Mlength)
- {
- $string = $MATCH[$k].",".$TEMP[$k];
- $MATCH[$k]=$string;
- }
- else
- {
- $string = $SkipedFileSpace[$space-1].$TEMP[$k];
- $MATCH[$k]=$string;
- }
- }
- }
- }
- @TEMP=();
- }
- else
- {
- if($Mlength ne 0)
- {
- for($k=0; $k<$Mlength; $k++)
- {
- $string = $MATCH[$k].$StandaredSpace[$space];
- $MATCH[$k]=$string;
- }
- }
- }
- }
- sub FindSystemAdmin
- {
- for($k=0; $ArrayofFiles[0][$k] ne NULL; $k++)
- {
- $IP2=$ArrayofFiles[0][$k];
- if($IP2=~ m/$IP/)
- {
- chop $IP2; #removes EOL
- chop $IP2;
- chop $IP2;
- foreach(@MATCH)
- {
- $final=$IP2.",".$_;
- push(@SA,$final);
- }
- $found = 1;
- last;
- }
- }
- if($found ne 1)
- {
- foreach(@MATCH)
- {
- $final=$IP.$NoContact.",".$_;
- push(@NoSA,$final);
- }
- }
- $found = 0;
- }
- sub Finalprint
- {
- open (Excel::Writer::XLSX,'>'.$ResultsFile);
- print Excel::Writer::XLSX $Banner;
- foreach(@SA)
- {
- print Excel::Writer::XLSX$_."\n";
- }
- foreach(@NoSA)
- {
- print Excel::Writer::XLSX $_."\n";
- }
- close(Excel::Writer::XLSX);
- }
- $date = Time::Piece->new->strftime('%m-%d-%y');
- $ResultsFile="Security_Scans_".$date.".csv";
- &readFile;
- &moreFiles;
- <>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement