Advertisement
matriphe

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

Nov 13th, 2013
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.59 KB | None | 0 0
  1. #!/bin/bash
  2. NEWHOST=`nslookup nuwira.dyndns.org | 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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement