Skip to content

Instantly share code, notes, and snippets.

@ilyaevseev
Created April 27, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ilyaevseev/04d66b7c608fff87c68d6adfd3492a81 to your computer and use it in GitHub Desktop.
Save ilyaevseev/04d66b7c608fff87c68d6adfd3492a81 to your computer and use it in GitHub Desktop.
Compare two sysctl outputs.
#!/usr/bin/perl
use strict;
use warnings;
die "Usage: $0 sysctl1.lst sysctl2.lst\n" if @ARGV != 2;
sub file2list($) {
my $fname = shift;
open F, $fname or die "Cannot open $fname: $!\n";
my @lines = <F>;
close F;
chomp @lines;
my %k = map { my ($a,$b) = split / = /, $_; $a => $b } @lines;
\%k
}
my $a = file2list($ARGV[0]);
my $b = file2list($ARGV[1]);
my (%onlya, %onlyb, %mismatch);
while (my ($k,$v) = each %$a) {
if (!exists($b->{$k})) {
$onlya{$k} = $v
} elsif ($b->{$k} ne $v) {
$mismatch{$k} = "$v == $b->{$k}"
}
}
while (my ($k,$v) = each %$b) {
$onlyb{$k} = $v if !exists($a->{$k});
}
sub print_list($$) {
my ($title, $listref) = @_;
print "\n==== $title ====\n";
printf("%-40s %s\n", $_, $listref->{$_}) foreach sort keys %$listref;
}
print_list "MISMATSH", \%mismatch;
print_list "ONLY AAA", \%onlya;
print_list "ONLY BBB", \%onlyb;
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment