Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- #===============================================================================
- #
- # FILE: ddns.sh
- #
- # USAGE: ./ddns.sh
- #
- # DESCRIPTION: Enables the use of a DreamHost hosted FQDN with dynamic
- # IP addresses.
- # REQUIRES: API Access and key - https://panel.dreamhost.com/?tree=home.api
- # Details about the API at http://wiki.dreamhost.com/API
- # AUTHOR: Eri Ramos Bastos - bastos.eri()gmail.com
- # VERSION: Wed Oct 6 21:52:09 ADT 2010
- #
- #
- #===============================================================================
- # Configuration
- hostname="your.fqdn.hostname"
- export key="YOURAPIKEY"
- #-------------------------------------------------------------------------------
- # Don't change from here to bottom unless you know what you're doing
- #-------------------------------------------------------------------------------
- fetch_cmd="curl -s"
- current_ip_url="http://icanhazip.com"
- function die () { echo "$progname: $1"; exit ${2:-1} ; }
- function Verbose ()
- {
- # May be useful for troubleshooting
- [ $VERBOSE ] && echo "[MESSAGE] $1"
- return 0
- } # ---------- end of function Verbose ----------
- function RunCmd ()
- {
- local cmd="$1"
- local uuid=$(uuidgen)
- local link="https://api.dreamhost.com/?key=$key&unique_id=$uuid&cmd=$cmd"
- local api_response=$($fetch_cmd "$link")
- if ! (echo $api_response | grep -q 'success'); then
- return 1
- fi
- echo "$api_response"
- } # ---------- end of function RunCmd ----------
- #-------------------------------------------------------------------------------
- # Runs from here
- #-------------------------------------------------------------------------------
- # Fetches current DNS entries
- cmd=dns-list_records
- dreamhost_response=$(RunCmd "$cmd")
- [ $? -ne 0 ] && die "Unable to get response from DreamHost"
- current_dns_ip=$(echo "$dreamhost_response"|grep "$hostname" |cut -f5)
- # Fetches IP address in use
- Verbose "Fetching current IP address"
- current_ip_response=$($fetch_cmd "$current_ip_url")
- [ -z "$current_ip_response" ] && die "Unable to get IP address from $current_ip_url"
- # Do magic
- Verbose "Comparing IPs"
- if [ "$current_dns_ip" = "$current_ip_response" ]
- then
- Verbose "IP matches. Nothing else to do..."
- else
- Verbose "IP needs update"
- #Remove current DNS entry
- Verbose "Removing current DNS entry $current_dns_ip"
- cmd="dns-remove_record&record=$hostname&type=A&value=$current_dns_ip"
- dreamhost_response=$(RunCmd "$cmd")
- [ $? -ne 0 ] && die "Unable to get response from DreamHost"
- #Creates new DNS entry
- Verbose "Adding new entry $current_ip_response"
- now=$(date +%Y%m%d_%H%M)
- cmd="dns-add_record&record=$hostname&type=A&value=$current_ip_response&comment=$now"
- dreamhost_response=$(RunCmd "$cmd")
- [ $? -ne 0 ] && die "Unable to get response from DreamHost"
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement