Advertisement
Guest User

matchip

a guest
Jul 24th, 2014
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 5.81 KB | None | 0 0
  1. #!/usr/bin/perl perl
  2.                                                                
  3.  
  4.  
  5. use Time::Piece;
  6. use Excel::Writer::XLSX;
  7. my $date;
  8. my $ResultsFile;
  9.  
  10.  
  11. my @WholefileString;    #this is the whole file divided up by lines
  12. my @ArrayofFiles;   #holds File names
  13. my @SA;         #has a System Admin
  14. my @NoSA;           #does not have a system Admin
  15. my $MyoutFile;
  16. sub readFile
  17. {
  18.     print "\n  Combine IP Addresses from Scaned Files.\n";
  19. print "\n\n1) CSV files must have double pipette symbols || at the end of each row.\n";
  20. print "2) GSARS or System Admin File should be entered first.\n";
  21. print "3) Security_Scans_mm-dd-yy.csv will be created with the results.\n\n";
  22. print "Enter the number of files to be matched. (2-10) \n";
  23.     $num=<STDIN>;
  24.     for($i=1; $i<=$num; $i++)
  25.     {
  26.         if($i eq 1)
  27. {
  28. print "Path of SA File: ";
  29.             $Fname=<STDIN>;
  30.     push (@fileholder,$Fname);
  31. }
  32. else                                                    
  33. {
  34. print "Path of file $i: ";
  35.             $Fname=<STDIN>;
  36.     push (@fileholder,$Fname);
  37. }
  38.        
  39.     }
  40.     push (@ArrayofFiles,@fileholder);
  41.     &BuildArrays;
  42. }
  43.  
  44. sub BuildArrays
  45. {
  46.     $TotalFiles=@ArrayofFiles;
  47.     $Banner = ();
  48.     local $/="||\n";            #This is the EOL Symbol
  49.     for($i=0; $i<$TotalFiles; $i++)
  50.     {
  51.         $FiletoOpen=$ArrayofFiles[$i];
  52.         open (INPUT, $FiletoOpen) or die "$FiletoOpen is not in this Path\n\n\n";
  53.         @WholefileString = <INPUT>;
  54.  
  55.         chop $WholefileString[0];
  56.        
  57.         @Header = split(",",$WholefileString[0]);
  58.         $Hlength = @Header;
  59.         push(@HeaderLengths,$Hlength);
  60. chop $WholefileString[0];
  61.  
  62. chop $WholefileString[0];
  63.  
  64.         chop $Banner;
  65.         $Banner = $Banner.$WholefileString[0].",\n";
  66.         shift(@WholefileString);
  67.  
  68. @SortedIP = sort {(split ',', $a)[0] cmp (split ',', $b)[0]} @WholefileString;
  69. push(@SortedIP,NULL);
  70.  
  71. foreach(@SortedIP )
  72. {
  73.     push (@{$ArrayofFiles[$i]},$_);
  74. }
  75.    
  76. @out=();
  77. @WholefileString =();
  78.     }
  79.     &GetSpacing;
  80. &CombineData;
  81. }
  82. sub GetSpacing
  83. {
  84.  
  85.     push(@HeaderLengths2,@HeaderLengths);
  86.  
  87.     for($i=2; $i<$TotalFiles; $i++)
  88. {
  89.     $value = $HeaderLengths2[$i];
  90.     $spaces = $value + $HeaderLengths2[$i-1];
  91. $HeaderLengths2[$i] = $spaces;
  92.  
  93.     }
  94.     $value = $HeaderLengths[0];
  95.     $str=();
  96.     for($i=0; $i<$value-2; $i++)
  97. {
  98.     $str =$str ."N/A,";
  99. }
  100. $NoContact=$str;
  101.     for($i=0; $i<$TotalFiles; $i++)
  102. {
  103.     $spaces = $HeaderLengths[$i];
  104.     $str=();
  105. for($j=0; $j <$spaces; $j++)
  106. {
  107.     $str =$str .",";
  108. }
  109. push(@StandaredSpace,$str);
  110. }  
  111.  
  112.     for($i=0; $i<$TotalFiles; $i++)
  113. {
  114.     $spaces = $HeaderLengths2[$i];
  115.     $str=();
  116.     for($j=0; $j <$spaces; $j++)
  117.     {
  118.         $str =$str .",";
  119.     }
  120.     push(@SkipedFileSpace,$str);
  121. }
  122.  
  123. }
  124. sub CombineData
  125. {
  126.     $found = 0;
  127.     print "\n\n Matching in progress. \n\n";
  128.     for($i=1; $i<$TotalFiles; $i++)         #THIS IS THE FILE LOOP
  129.     {
  130.         for($ii=0; $ArrayofFiles[$i][$ii] ne NULL; $ii++)   #FILE 1 IP
  131.         {
  132.             if($ArrayofFiles[$i][$ii] ne CHECKED)
  133.             {
  134.                 @templine=split(',',$ArrayofFiles[$i][$ii]);
  135.                 $IP = @templine[0].',';
  136.         for($j=$i; $j<$TotalFiles; $j++)        #THIS IS THE second file LOOP
  137.         {
  138.             for($jj=0; $ArrayofFiles[$j][$jj] ne NULL; $jj++) #THIS IS THE IP2
  139. {
  140.     if($ArrayofFiles[$j][$jj] ne CHECKED)
  141. {
  142. $IP2=$ArrayofFiles[$j][$jj];
  143.     if($IP2=~ m{$IP})
  144.     {
  145.  
  146. chop $ArrayofFiles[$j][$jj];    #remove EOL
  147. chop $ArrayofFiles[$j][$jj];    #remove '|'
  148. chop $ArrayofFiles[$j][$jj];    #remove last '|'
  149. push(@TEMP,$ArrayofFiles[$j][$jj]);
  150. $ArrayofFiles[$j][$jj]="CHECKED";
  151. $found=1;
  152. }
  153. else
  154. {
  155.     if($found eq 1)
  156. {
  157.     $found =0;
  158.     last;
  159. }
  160. }
  161.         }
  162. $found =0;
  163. }
  164. &CreateFinalArrays($i,$j);
  165. }
  166. &FindSystemAdmin;
  167.     @MATCH=();
  168. }
  169.  
  170.  
  171. }
  172.         print "Finished scanning file $ArrayofFiles[$i]";
  173.     }
  174.     &Finalprint;       
  175. }
  176.  
  177. sub CreateFinalArrays
  178. {
  179.     $first=$_[0];  
  180.     $space=$_[1];
  181.     $Tlength=@TEMP;
  182.     $Mlength=@MATCH;
  183.     if($Tlength ne 0)                       #if temp has an element
  184.     {
  185.         if($Mlength eq 0)                  #if match length =0
  186.         {  
  187.             if($first eq 1)
  188.             {
  189.                 foreach(@TEMP)
  190.                 {
  191.                     push(@MATCH,$_)
  192.                 }
  193.             }
  194.             else
  195.             {
  196.                 foreach(@TEMP)
  197.                 {
  198.                     $string = $SkipedFileSpace[$space-1].$_;
  199.                     push(@MATCH,$string)
  200.                 }          
  201.             }
  202.         }  
  203.         else
  204.         {  
  205.             if($Tlength eq $Mlength)
  206.             {
  207.                 for($k=0; $k<$Tlength; $k++)
  208.                 {
  209.                     $string = $MATCH[$k].",".$TEMP[$k];
  210.                     $MATCH[$k]=$string;
  211.                 }
  212.  
  213.             }
  214.             elsif($Tlength < $Mlength)
  215.             {
  216.                 for($k=0; $k<$Mlength; $k++)
  217.                 {
  218.                     if($k< $Tlength)
  219.                     {
  220.                         $string = $MATCH[$k].",".$TEMP[$k];
  221.                         $MATCH[$k]=$string;
  222.                     }
  223.                     else   
  224.                     {
  225.                         $string = $MATCH[$k].$StandaredSpace[$space];
  226.                         $MATCH[$k]=$string;
  227.                     }
  228.                 }
  229.             }
  230.             else #($Tlength > $Mlength)
  231.             {
  232.                 for($k=0; $k<$Tlength; $k++)
  233.                 {
  234.                     if($k< $Mlength)
  235.                     {
  236. $string = $MATCH[$k].",".$TEMP[$k];
  237.                         $MATCH[$k]=$string;
  238.                     }
  239.                     else
  240.                     {
  241.                         $string = $SkipedFileSpace[$space-1].$TEMP[$k];
  242.                         $MATCH[$k]=$string;
  243.  
  244.                     }
  245.                 }
  246.             }
  247.         }
  248.         @TEMP=();
  249.     }
  250.     else
  251.     {
  252.         if($Mlength ne 0)
  253.         {
  254.             for($k=0; $k<$Mlength; $k++)
  255.             {
  256.                 $string = $MATCH[$k].$StandaredSpace[$space];
  257.                 $MATCH[$k]=$string;
  258.  
  259.             }
  260.         }
  261.     }
  262. }
  263.  
  264. sub FindSystemAdmin
  265. {
  266.     for($k=0; $ArrayofFiles[0][$k] ne NULL; $k++)
  267.     {
  268.         $IP2=$ArrayofFiles[0][$k];
  269.         if($IP2=~ m/$IP/)
  270.         {
  271.             chop $IP2;  #removes EOL
  272.             chop $IP2;
  273.     chop $IP2;
  274.             foreach(@MATCH)
  275.             {
  276.                 $final=$IP2.",".$_;
  277.                 push(@SA,$final);
  278.             }
  279.             $found = 1;
  280.             last;
  281.         }
  282.     }  
  283.     if($found ne 1)
  284.     {
  285.         foreach(@MATCH)
  286. {
  287.     $final=$IP.$NoContact.",".$_;
  288.     push(@NoSA,$final);
  289. }
  290.        
  291.     }
  292.     $found = 0;
  293. }
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300. sub Finalprint
  301.  
  302. {      
  303.  
  304. open (Excel::Writer::XLSX,'>'.$ResultsFile);
  305.  
  306.  
  307.  
  308.     print Excel::Writer::XLSX $Banner;
  309.  
  310.  
  311. foreach(@SA)
  312. {
  313.    
  314.    
  315.     print Excel::Writer::XLSX$_."\n";
  316.    
  317.    
  318.    
  319.    
  320. }
  321.  
  322. foreach(@NoSA)
  323. {  
  324.     print Excel::Writer::XLSX $_."\n";
  325.    
  326.  
  327.    
  328. }
  329.  
  330.  
  331.  
  332.  
  333.     close(Excel::Writer::XLSX);
  334. }
  335.  
  336.  
  337. $date = Time::Piece->new->strftime('%m-%d-%y');
  338. $ResultsFile="Security_Scans_".$date.".csv";
  339.  
  340.  
  341.  
  342. &readFile;
  343. &moreFiles;
  344.  
  345. <>;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement