Find the answer to your Linux question:
Results 1 to 3 of 3
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    [SOLVED] named hints update script

    I need help getting this bourne shell script working properly. This script I found online while reading some tutorials on getting a caching name server running, which is supposed to update the rootserver hints file.

    The below case statement always returns that the hints file has failed. Even though when I check the file it looks normal and I can see the NOERROR status in the returned header.

    dig (at) . ns > 2> errors
     case `cat` in
            # It worked
            echo "Subject: The root.hints file update has FAILED."
            echo "The root.hints update has failed"
            echo "This is the dig output reported:"
            cat errors
            exit 1
    I have also been wanting to turn these case statements into if statements if anyone could help me with the syntax for that. I don't typically like to use case statements when there is usually only 1 or 2 possible results. The problem I'm having here is that I can't seem to put command output into a string/variable. See the code below for an example of my intentions.

    #Code to check if named is running.
    status = 'rndc status | grep server'
    if [ $status != 'server is up and running' ]
    echo "Named is not running!"
    echo "Exiting."
    exit 0
    Maybe it's just me but I think the if statement looks cleaner than the case statement used in the original script.

    Extra information:
    I'm running this on FreeBSD 8.2-RELEASE.
    The caching name server is working with no problems.

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    In your case statement, the reason that it always returns error is because the input is handled line-by-line. So it looks at the first line of If that line matches the glob *NOERROR*, then it says it works. However, if that line doesn't, then it goes to the error case.

    A better way of doing something like that would be:
    if grep -q NOERROR; then
        # It worked
        # It failed
    Here we search the file for the string "NOERROR", and if it exists, we say that it worked. Some things to note:

    1) grep -q means that grep doesn't output anything, and just exits with status 0 if the string was found, and nonzero otherwise.

    2) We don't use if [ ... ], but instead just if .... This is because you can always just if a command; if the command exits with status 0 (success), it is considered true.

    In your second example, the problem is your use of single quotes instead of backticks. Also, instead of backticks, you can use $(...). This is sometimes preferred because it is easier to distinguish from single quotes.

  3. #3
    Much appreciated Cabhan! :D

    Here's the full script which will need very little editing for anyone who wishes to use it. I put in full file paths so you can run the script from anywhere. Comments or suggestions are welcome.

    #Update named hints file
    #Make a cron entry and forget about it!
    #Check if named is running
    echo "Checking if named is running..."
    if `rndc status | grep -q "server is up and running"`
    echo "Named is running!"
    echo "Named is NOT running!"
    echo "Exiting."
    exit 0
    #Check for internet connection
    echo "Checking if we are online..."
    if `ping -c 1 <external ip> | grep -q "100.0% packet loss"`;
    echo "We are not online!"
    echo "Could not ping external server!"
    echo "Exiting."
    exit 1
    echo "We are online!"
    #Create root.hints file
    echo "Creating /etc/namedb/"
    dig . ns > /etc/namedb/
    if `cat /etc/namedb/ | grep -q "NOERROR"`;
    echo "File creation successful!"
    echo "Replacing old file with updated information..."
    chmod 644 /etc/namedb/
    cp /etc/namedb/root.hints /etc/namedb/root.hints.old
    cp /etc/namedb/ /etc/namedb/root.hints
    rm -f /etc/namedb/
    echo "Restarting Named..."
    /etc/rc.d/named onerestart
    echo "The root.hints file update has FAILED!"
    echo "Printing dig output..."
    cat /etc/namedb/
    echo "Exiting."
    exit 1
    echo "Update Complete!"
    exit 0
    I wasn't able to just copy and paste my script since the server it's on doesn't have X running or a web browser so there might be a mistake but I don't see any after reading it 2x.

    This thread can be renamed to SOLVED.
    Thank you.

  4. $spacer_open

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts