Advertisement
Guest User

ddns.sh

a guest
Oct 6th, 2010
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.94 KB | None | 0 0
  1. #!/bin/bash
  2. #===============================================================================
  3. #
  4. #       FILE:  ddns.sh
  5. #      
  6. #       USAGE:  ./ddns.sh
  7. #
  8. #       DESCRIPTION: Enables the use of a DreamHost hosted FQDN with dynamic
  9. #                    IP addresses.
  10. #       REQUIRES: API Access and key - https://panel.dreamhost.com/?tree=home.api
  11. #                 Details about the API at http://wiki.dreamhost.com/API
  12. #       AUTHOR:   Eri Ramos Bastos - bastos.eri()gmail.com
  13. #       VERSION:  Wed Oct  6 21:52:09 ADT 2010
  14. #
  15. #
  16. #===============================================================================
  17.  
  18.  
  19. # Configuration
  20. hostname="your.fqdn.hostname"
  21. export key="YOURAPIKEY"
  22.  
  23.  
  24. #-------------------------------------------------------------------------------
  25. #   Don't change from here to bottom unless you know what you're doing
  26. #-------------------------------------------------------------------------------
  27. fetch_cmd="curl -s"
  28. current_ip_url="http://icanhazip.com"
  29.  
  30.  
  31. function die () { echo "$progname: $1"; exit ${2:-1}  ; }
  32.  
  33. function Verbose ()
  34. {
  35.     # May be useful for troubleshooting
  36.     [ $VERBOSE ] && echo "[MESSAGE] $1"
  37.     return 0
  38. }    # ----------  end of function Verbose  ----------
  39.  
  40.  
  41. function RunCmd ()
  42. {
  43.     local cmd="$1"
  44.     local uuid=$(uuidgen)
  45.     local link="https://api.dreamhost.com/?key=$key&unique_id=$uuid&cmd=$cmd"
  46.     local api_response=$($fetch_cmd "$link")
  47.     if ! (echo $api_response | grep -q 'success'); then
  48.         return 1
  49.     fi
  50.     echo "$api_response"
  51. }    # ----------  end of function RunCmd  ----------
  52.  
  53.  
  54.  
  55. #-------------------------------------------------------------------------------
  56. # Runs from here
  57. #-------------------------------------------------------------------------------
  58.  
  59.  
  60. # Fetches current DNS entries
  61. cmd=dns-list_records
  62. dreamhost_response=$(RunCmd "$cmd")
  63. [ $? -ne 0 ] && die "Unable to get response from DreamHost"
  64. current_dns_ip=$(echo "$dreamhost_response"|grep "$hostname" |cut -f5)
  65.  
  66. # Fetches IP address in use
  67. Verbose "Fetching current IP address"
  68. current_ip_response=$($fetch_cmd "$current_ip_url")
  69. [ -z "$current_ip_response" ] && die "Unable to get IP address from $current_ip_url"
  70.  
  71. # Do magic
  72. Verbose "Comparing IPs"
  73. if [ "$current_dns_ip" = "$current_ip_response" ]
  74. then
  75.     Verbose "IP matches. Nothing else to do..."
  76. else
  77.     Verbose "IP needs update"
  78.  
  79.     #Remove current DNS entry
  80.     Verbose "Removing current DNS entry $current_dns_ip"
  81.     cmd="dns-remove_record&record=$hostname&type=A&value=$current_dns_ip"
  82.     dreamhost_response=$(RunCmd "$cmd")
  83.     [ $? -ne 0 ] && die "Unable to get response from DreamHost"
  84.  
  85.     #Creates new DNS entry
  86.     Verbose "Adding new entry $current_ip_response"
  87.     now=$(date +%Y%m%d_%H%M)
  88.     cmd="dns-add_record&record=$hostname&type=A&value=$current_ip_response&comment=$now"
  89.     dreamhost_response=$(RunCmd "$cmd")
  90.     [ $? -ne 0 ] && die "Unable to get response from DreamHost"
  91. fi
  92.  
  93. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement