Monday 28 May 2012

Shell Script: NSLookup DNS entry to see which server is LIVE


Here is a simple script which allows you to perform an NSLookup on a DNS entry to find out if the current host is Live or not.

This is useful if you have a mirrored backup server that shouldnt process any data unless its currently live. If a failover occurs, the backup server will then realise it's live and process the information which the other server was currently processing.


Code Snippet
  1. #!/bin/sh
  2. ################################################################################
  3. # Does an NSLookup on a server DNS entry to determine which server is currently LIVE.
  4. ################################################################################
  5. #
  6. # Get LIVE server and query current server
  7. liveServer=`eval nslookup mgs | grep "Name:"`
  8. currentServer=`eval hostname`
  9.  
  10.  
  11. # Nslookup to see if this server is LIVE
  12. echo "$liveServer" | grep $currentServer >/dev/null 2>&1
  13.  
  14. if [ "$?" -eq "0" ]; then
  15.     echo "This IS the Live server"
  16. else
  17.     echo "This IS NOT the Live server"
  18. fi
  19.  
  20.  
  21.  
  22. exit 0
End of Code Snippet

No comments: