Advertisement
mikelieman

baseline.pl - check system drift since install

Feb 1st, 2022
2,422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 4.03 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use v5.21;
  6. use Data::Dumper;
  7.  
  8.  
  9. my $files = {};  # key = filename
  10.  
  11. get_all_files($files);
  12. get_rpm_files($files);
  13. verify_rpm_files($files);
  14.  
  15. #changed rpm
  16. for my $key (sort keys %{$files}) {
  17.     if ($files->{$key}->{'rpm_verify'}->{'flags'}) {
  18.         say $key;
  19.     }
  20. }
  21.  
  22. # not rpm
  23. for my $key (sort keys %{$files}) {
  24.     if ($files->{$key}->{'rpm'}) { next }
  25.     if ($files->{$key}->{'directory'}) { next }
  26.     say $key;
  27. }
  28. #say Dumper $files;
  29.  
  30.  
  31. ###############################################################################
  32. #
  33. #
  34. #
  35. ################################################################################
  36. sub get_all_files {
  37.  
  38.     my $all_files = shift; #hashref, key = filename
  39.  
  40.     #my $command = q{/usr/bin/find / -type f 2>/dev/null};
  41.     my $command = q{/usr/bin/find / -type f -o -type d};
  42.     my @list = qx{$command};
  43.  
  44.     foreach my $filename (@list) {
  45.  
  46.         next if ($filename =~ /^\/proc\//); # kernel structures
  47.         next if ($filename =~ /^\/run\//); # runtime data
  48.         next if ($filename =~ /^\/sys\//); # kernel structures
  49.  
  50.         chomp $filename;
  51.         $all_files->{$filename}->{'all'} = 1;
  52.         $all_files->{$filename}->{'directory'} = 0;
  53.         if (-d $filename) { $all_files->{$filename}->{'directory'} = 1 }
  54.  
  55.     }
  56.  
  57.     return;
  58.  
  59. } # sub get_all_files
  60.  
  61.  
  62. ################################################################################
  63. #
  64. #
  65. #
  66. ################################################################################
  67. sub get_rpm_files {
  68.  
  69.     my $all_files = shift; #hashref, key = filename
  70.  
  71.     my $command = q{/usr/bin/rpm -qal};
  72.     my @list = qx{$command};
  73.  
  74.     foreach my $filename (@list) {
  75.  
  76.         chomp $filename;
  77.  
  78.         # filter non-files
  79.         next if $filename =~ /(contains no files)/;
  80.  
  81.         $all_files->{$filename}->{'rpm'} = 1;
  82.  
  83.     }
  84.  
  85.     return;
  86.  
  87. } # sub get_rpm_files
  88.  
  89.  
  90. ################################################################################
  91. #
  92. #
  93. # .M.......
  94. # 123456789
  95. #
  96. ################################################################################
  97. sub verify_rpm_files {
  98.  
  99.     my $all_files = shift; #hashref, key = filename
  100.  
  101.     my $command = q{/usr/bin/rpm -Va};
  102.  
  103.     my @list = qx{$command};
  104.  
  105.     foreach my $line (@list) {
  106.  
  107.         chomp $line;
  108.         my @pieces = split /\s+/, $line;
  109.  
  110.         my $flags = shift @pieces;
  111.         my @rev = reverse @pieces;
  112.         my $filename = shift @rev;
  113.         my $attribute = shift @rev;
  114.  
  115.         if (0) {
  116.             say "line $line";
  117.             #say "pieces: " . Dumper @pieces;
  118.             say "filename $filename";
  119.             say "flags: $flags";
  120.             say parse_flags($flags);
  121.             say "attribute: $attribute";
  122.             say parse_attr_code($attribute);
  123.         }
  124.  
  125.         $all_files->{$filename}->{'rpm_verify'}->{'flags'} = $flags;
  126.         $all_files->{$filename}->{'rpm_verify'}->{'attr'} = $attribute;
  127.  
  128.     }
  129.  
  130.     return;
  131.  
  132. } # sub verify_rpm_files
  133.  
  134.  
  135. ################################################################################
  136. #
  137. #
  138. #
  139. ################################################################################
  140. sub parse_flags {
  141.  
  142.     my $flags = shift;
  143.  
  144.     my $output = q{};
  145.  
  146.     if ($flags) {
  147.         if ($flags =~ /^S/)         { $output .= "File Size Differs\n"};
  148.         if ($flags =~ /^.M/)        { $output .= "File Mode Differs\n"};
  149.         if ($flags =~ /^..5/)       { $output .= "MD5 Checksum Differs\n"};
  150.         if ($flags =~ /^...D/)      { $output .= "Device File Major/Minor Ver Nbrs Differ\n"};
  151.         if ($flags =~ /^....L/)     { $output .= "A Mismatch Occurs In Link\n"; };
  152.         if ($flags =~ /^.....U/)    { $output .= "File Ownership Differs\n"; };
  153.         if ($flags =~ /^......G/)   { $output .= "File Group Differs\n"; };
  154.         if ($flags =~ /^.......T/)  { $output .= "File Time (mtime) Differs\n"; };
  155.     }
  156.    
  157.     return $output;
  158.  
  159. } # sub parse_flags
  160.  
  161.  
  162. ################################################################################
  163. #
  164. #
  165. #
  166. ################################################################################
  167. sub parse_attr_code {
  168.  
  169.     my $attribute = shift;
  170.  
  171.     my $output = q{};
  172.  
  173.     if ($attribute) {
  174.         if ($attribute eq "c") {$output = "Configuration File" };
  175.         if ($attribute eq "d") {$output = "Documentation File" };
  176.         if ($attribute eq "g") {$output = "Ghost File" };
  177.         if ($attribute eq "l") {$output = "License File" };
  178.         if ($attribute eq "r") {$output = "Readme File" };
  179.     }
  180.  
  181.     return $output;
  182.  
  183. } # sub parse_attr_code
  184.  
  185.  
  186. __END__
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement