• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 528
  • Last Modified:

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
0
ashsysad
Asked:
ashsysad
  • 2
2 Solutions
 
tdlewisCommented:
Try replacing the last two loops with this:
foreach $i(@first_file) {
        $i =~ s/\s+$//;
        @array1 = split(" = ",$i);
        $output1{$array1[0]} = $array1[1];
        $keys{$array1[0]} = 1;
}

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

foreach $key (sort keys %keys)
{
  $output1{$key} = "" unless defined $output1{$key};
  $output2{$key} = "" unless defined $output2{$key};
  print "$key\t$output1{$key}\t$output2{$key}\n";
}

Open in new window

0
 
ozoCommented:
#!/usr/bin/perl

if( @ARGV != 2 )
{
   print "\nUsage: $0 <file1> <file2>\n\n";
   exit 1;
}
my %keys;
while( <> ){
        s/\s+$//;
        my @array1 = split(" = ");
        $keys{$array1[0]}[!@ARGV]=$array1[1];
}

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

printf("%-40s %-25s %-50s\n", $_, @{$keys{$_}}) for sort keys %keys;

Open in new window

0
 
ashsysadAuthor Commented:
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});
  }
}
0
 
ashsysadAuthor Commented:
Thanks a lot.  Have a wonderful day !!!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now