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.
- 03-27-2011 #1
- Join Date
- Mar 2007
[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)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
#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
I'm running this on FreeBSD 8.2-RELEASE.
The caching name server is working with no problems.
- 03-27-2011 #2
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:
if grep -q NOERROR root.hints.new; then # It worked else # It failed fi
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.
- 03-27-2011 #3
- Join Date
- Mar 2007
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.
#!/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
This thread can be renamed to SOLVED.