Find the answer to your Linux question:
Results 1 to 3 of 3
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Mar 2007
    Posts
    6

    [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.

    Code:
    dig (at)e.root-servers.net . ns >root.hints.new 2> errors
    
     case `cat root.hints.new` in
       *NOERROR*)
            # It worked
            :;;
       *)
            echo "Subject: The root.hints file update has FAILED."
            echo
            echo "The root.hints update has failed"
            echo "This is the dig output reported:"
            echo
            cat root.hints.new errors
            exit 1
            ;;
     esac
    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:
    #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
    fi
    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
    Location
    Seattle, WA, USA
    Posts
    3,252
    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 root.hints.new. 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:
    Code:
    if grep -q NOERROR root.hints.new; then
        # It worked
    else
        # It failed
    fi
    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
    Just Joined!
    Join Date
    Mar 2007
    Posts
    6
    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.

    Code:
    #!/bin/sh
    #
    #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"`
    then
    echo "Named is running!"
    else
    echo "Named is NOT running!"
    echo "Exiting."
    exit 0
    fi
    
    #Check for internet connection
    echo "Checking if we are online..."
    
    if `ping -c 1 <external ip> | grep -q "100.0% packet loss"`;
    then
    echo "We are not online!"
    echo "Could not ping external server!"
    echo "Exiting."
    exit 1
    else
    echo "We are online!"
    fi
    
    #Create root.hints file
    
    echo "Creating /etc/namedb/root.hints.new..."
    dig @a.root-servers.net . ns > /etc/namedb/root.hints.new
    
    if `cat /etc/namedb/root.hints.new | grep -q "NOERROR"`;
    then
    echo "File creation successful!"
    echo "Replacing old file with updated information..."
    chmod 644 /etc/namedb/root.hints.new
    cp /etc/namedb/root.hints /etc/namedb/root.hints.old
    cp /etc/namedb/root.hints.new /etc/namedb/root.hints
    rm -f /etc/namedb/root.hints.new
    echo "Restarting Named..."
    /etc/rc.d/named onerestart
    else
    echo "The root.hints file update has FAILED!"
    echo "Printing dig output..."
    cat /etc/namedb/root.hints.new
    echo "Exiting."
    exit 1
    fi
    
    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
    $spacer_close

Posting Permissions

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