0

2 strings can be compared easily with test or [[ ... ]] like Compare string with regex

e.g.

wolf@linux:~$ var1=`grep 'trap2sink 127.0.0.7 w0rd' snmpd.conf`
wolf@linux:~$ 
wolf@linux:~$ if [[ $var1 = 'trap2sink 127.0.0.7 w0rd' ]]; then
> echo Good
> else
> echo Bad
> fi
Good
wolf@linux:~$ 

What about set of certain configuration which contains a few lines?

The purpose is to make sure every single configuration in all devices follow the standard.

Let say this is the standard of snmpd.conf. Let's name this snmpd.conf.standard

trap2sink 127.0.0.7 w0rd
rocommunity P@55 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9

And this is the sample config from device X. Let's name this snmpd.conf

*** some random data here and there *** 
rocommunity P@55 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9
trap2sink 127.0.0.7 w0rd
*** some random data here and there *** 

What is the best way to compare these 2 config?

Since there are a few lines of data, I'm not sure if grep can be used in this case, then followed by if ... else statement.

if [[ ... config matches ... ]]; then
    echo Good
else
    echo Bad
fi

Please let me know what is the best way to solve this kind of problem.

Update 1 by Bill Jetzer (it works ... I'll test with a few actual data and will share the output)

wolf@linux:~$ cat snmpd.conf
*** some random data here and there *** 
rocommunity P@5s 127.0.0.1
rocommunity 4nyth1ng 127.0.0.9
trap2sink 127.0.0.7 w0rd
*** some random data here and there *** 
wolf@linux:~$

wolf@linux:~$ cat snmpd.conf.standard trap2sink 127.0.0.7 w0rd rocommunity P@55 127.0.0.1 rocommunity 4nyth1ng 127.0.0.9 wolf@linux:~$

wolf@linux:~$ ptn='^(rocommunity|trap2sinc) '; wolf@linux:~$ diff <(grep -E "$ptn" snmpd.conf | sort) <(grep -E "$ptn" snmpd.conf.standard | sort); 2c2 < rocommunity P@5s 127.0.0.1


> rocommunity P@55 127.0.0.1 wolf@linux:~$

Wolf
  • 1,631

3 Answers3

2
ptn='^(rocommunity|trap2sinc) ';
diff <(grep -E "$ptn" file1| sort) <(grep -E "$ptn" file2| sort);

If it returns nothing, then those lines are common to both files.

2

You can compare any number of files at once by check-summing them and sorting the output. This will group all identical files together, so you can spot the renegades. man cksum

Diff has a fairly obscure output, that needs a practised eye to understand. It actually emits a set of commands (as used by the ed command) which would convert file1 into file2.

There is a variant sdiff which shows the files side-by-side, which is much easier to understand. man sdiff

As you do have a 'standard' file, just cksum that one first, and then cksum them all and grep -v to find the ones that are different.

Alternatively, the easiest way to ensure the files are all identical is to copy the standard onto all the others.

Paul_Pedant
  • 8,679
  • 2
    There's also diff -u, which I find far easier to scan visually than plain diff – Chris Davies Jul 09 '20 at 15:21
  • 1
    I got the impression that the files were not expected to be identical, only that they contain those three particular lines, and not necessarily in the same order. Hence checksumming the files didn't seem like an option. – Bill Jetzer Jul 09 '20 at 15:29
  • 1
    @roaima Had to run that to figure what the man page meant. But it's rather nice. – Paul_Pedant Jul 09 '20 at 15:29
1

If the idea is to have a process that can be automated, e.g. in a shell script, the to console-based answers by Bill Jetzer and Paul_Pedant are probably the best way to go.

If you want to inspect them "by eye", you may want to try meld, which is a graphical tool similar to "BeyondCompare" under Windows, and shows differences between text files in a rather easily accessible way.

AdminBee
  • 22,803
  • Thanks for the GUI tools. Yup, I want to automate the process, probably might need to combine both idea. – Wolf Jul 09 '20 at 23:06