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:~$