Score:0

Shell script to check resolv.conf

eg flag

I need to write a shell script which checks the contents of resolv.conf

Eg

domain example.com
search abc.com abc.org abc.net
nameserver 1.1.2.2
nameserver 3.3.4.4

abc.com/abc.org/abc.net can appear in any order

nameserver line can appear in any order

There could be spaces between the parameters.

What is the logic that can be used to check the config

Eg If abc.com is missing, it should highlight abc.com is missing

Tilman Schmidt avatar
bd flag
That task is not very well defined. What exactly do you want to check resolv.conf for? Also, does it have to be a shell script, and if so, which shell? The standard shells are not very well suited to this task. Scripting languages like Perl or Python would be a better match.
djdomi avatar
za flag
or planty overwrite the resolve.conf of the target regardless?
in flag
You should take a look at configuration management.
Score:2
us flag
Rob

The most basic check to see if a file contains a particular pattern is with the common grep command-line utility.

With the correct flags and options you can build a basic script that does something along the lines of the following pseudo-code:

# Break down the elements you're looking for 
# in basic regular expression patterns 
# and store them in an array

MyRequiredEntries=("nameserver\s*1.1.2.2" "nameserver\s*3.3.4.4" "domain\s*example.com" "search\s.*abc\.com" "search\s.*abc\.net")

# Loop over the array elements 

for MyEntry in "${MyRequiredEntries[@]}" 
do 
   
   # use the exit code of grep -q for further logic 

   grep -q "$MyEntry" /etc/resolv.conf

   MyResult=$?

   if [ $MyResult -eq 0 ]  ; then 
      echo "do something when resolv.conf contains the pattern $MyEntry" 
   else 
      echo "do something when resolv.conf does NOT contain the pattern $MyEntry" 
   fi 
done

Which can of course be fine-tuned according to your needs.


As others have commented: rather than doing a game of "spot the differences" you may want to step back and think as to why you're running this test and what you intend to do with your findings.

If the end result is to ensure that all servers have the correct configuration, then preferably start managing them with a configuration management system and avoid these differences from occurring in the future.
And if starting a central configuration management solution is still a bridge too far, rather than testing and then manually fixing settings: don't bother and directly apply the correct settings everywhere...

One a side-note: when (some of) your systems are configured with DHCP, you may want to start with pushing the correct settings from there.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.