/etc/hosts Auto Update BASH Script Based on Dynamic DNS

matriphe Nov 13th, 2013 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. NEWHOST=`nslookup | grep Address | sed '$!d' | sed 's/Address: //'` # getting new IP address
  3. if [[ $NEWHOST != "" ]]; then # check if there's IP address
  4. sed 's/^(.*)apihostname$//' /etc/hosts > /tmp/hosts # delete old entry
  5. sed '$d' /tmp/hosts > /tmp/hosts2 # delete new line, copy to temporary file /tmp/hosts
  6. echo "$NEWHOST apihostname" >> /tmp/hosts2 # write it to temporary file /tmp/hosts2
  7. cp /tmp/hosts2 /etc/hosts # done, copy back to /etc/hosts
  8. rm /tmp/hosts # deleting temporary files
  9. rm /tmp/hosts2
  10. echo "IP address: $NEWHOST" # show the new IP address
  11. fi
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand