Link to home
Start Free TrialLog in
Avatar of ashsysad
ashsysadFlag for United States of America

asked on

Need Perl script help

Hello,

I'm trying to develop a Perl script which can be used for comparing the Kernel settings between 2 Linux machines. Attached are the stripped version of 'sysctl -a' output taken from couple of Linux machines. I want to take these two files as input for the script and display the output in a formatted manner. Using this output, I can cross-verify the Kernel settings between 2 Linux servers. Please help me in completing my script attached in this.
Thanks in advance.

Sample Expected output:

KERNEL PARAMETER                         VALUE in 1ST SERVER       VALUE in 2ND SERVER
kernel.acct                                       4       2       30                   4       2       30
kernel.acpi_video_flags                    0                                       0
kernel.blk_iopoll                              1
kernel.bootloader_type                   113                                   113
kernel.cad_pid                                  1
kernel.cap-bound                             -257                                 -257
kernel.compat-log                           1                                       1
.
.




#!/usr/bin/perl

if( @ARGV != 2 )
{
   print "\nUsage: $0 <file1> <file2>\n\n";
   exit 1;
}

open(File1, "$ARGV[ 0 ]" );
open(File2, "$ARGV[ 1 ]" );

my @first_file = <File1>;
my @second_file = <File2>;
my %output1;
my %output2;

printf("%-40s %-25s %-50s\n", "KERNEL PARAMETER", "VALUE in 1ST SERVER", "VALUE in 2ND SERVER");

foreach $i(@first_file) {
        @array1 = split(" = ",$i);
        $output1{"$array1[0]\t$array1[1]"}++;
        foreach $key (%output1) {
                print "$key";
        }
}

foreach $j(@second_file) {
        @array2 = split(" = ",$j);
         $output2{"$array2[0]\t$array2[1]"}++;
        foreach $key (%output2) {
                print "$key";
}
}

Open in new window

file1.sorted.txt
file2.sorted.txt
ASKER CERTIFIED SOLUTION
Avatar of tdlewis
tdlewis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ashsysad

ASKER

Hello  Tdlewis/Ozo,  

Thankyou so much for the script !!  


@Tdlewis,  I slightly modified your script yesterday and made to look like this.


#!/usr/bin/perl
if( @ARGV != 2 )
{
   print "\nUsage: $0 <First-file> <Second-file>\n\n";
   exit 1;
}

open(File1, "$ARGV[0]" );
open(File2, "$ARGV[1]" );

my @first_file = <File1>;
my @second_file = <File2>;
my %output1, %output2, %kerparm;
my ($cnt1,$cnt2) = (0,0);

print "\nEnter '1' for Listing all Kernel parameters or '2' to list only the Differences : ";
my $choice=<STDIN>;
chomp($choice);

system $^O eq 'MSWin32' ? 'cls' : 'clear';
printf("%-50s %-25s %-50s\n\n", "KERNEL PARAMETER", "FIRST SERVER", "SECOND SERVER");

foreach $i(@first_file) {
        $i =~ s/\s+$//;
        @array1 = split(" = ",$i);
        $output1{$array1[0]} = $array1[1];
        $cnt1++;
        $kerparm{$array1[0]} = $cnt1;
}

foreach $j(@second_file) {
        $j =~ s/\s+$//;
        @array2 = split(" = ",$j);
        $output2{$array2[0]} = $array2[1];
        $cnt2++;
        $kerparm{$array2[0]} = $cnt2;
}

foreach $key (sort keys %kerparm)
{
  $output1{$key} = "" unless defined $output1{$key};
  $output2{$key} = "" unless defined $output2{$key};
  if($choice eq '1') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}); }
  elsif($choice eq '2') {
          printf("%-50s %-25s %-50s\n", $key,$output1{$key},$output2{$key}) if ($output1{$key} ne $output2{$key});
  }
}
Thanks a lot.  Have a wonderful day !!!