Skip to content

Instantly share code, notes, and snippets.

@mralexgray
Forked from joemiller/he-dns-update.sh
Created March 23, 2017 16:09
Show Gist options
  • Save mralexgray/4ae63bc31b63cc0a4936b15e3464d6d8 to your computer and use it in GitHub Desktop.
Save mralexgray/4ae63bc31b63cc0a4936b15e3464d6d8 to your computer and use it in GitHub Desktop.

Revisions

  1. @joemiller joemiller revised this gist Apr 1, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions he-dns-update.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/local/bin/bash
    #!/bin/bash
    #
    # Script for updating DNS records on Hurricane Electirc's DNS system (https://dns.he.net).
    #
    @@ -43,6 +43,10 @@
    # Joe Miller / https://github.com/joemiller 2016
    #

    LOGGER_BIN="logger"
    CURL_BIN="curl"
    CURL_TIMEOUT_SECS=10


    # load config file and verify required args
    . "/etc/he-dns-update.conf"
    @@ -57,10 +61,6 @@ if [ -z "$proto" ]; then
    exit 1
    fi

    LOGGER_BIN="logger"
    CURL_BIN="/usr/local/bin/curl"
    CURL_TIMEOUT_SECS=10

    out=$("$CURL_BIN" \
    -"$proto" \
    --silent \
  2. @joemiller joemiller created this gist Apr 1, 2016.
    96 changes: 96 additions & 0 deletions he-dns-update.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    #!/usr/local/bin/bash
    #
    # Script for updating DNS records on Hurricane Electirc's DNS system (https://dns.he.net).
    #
    # The record will be updated with the IP address that originates the request.
    #
    # Usage
    # -----
    #
    # Create config file `/etc/he-dns-update.conf`:
    #
    # HOSTNAME=myhost.dom.tld
    # KEY=<update_key>
    #
    # Update IPv4 (A) record:
    #
    # $ ./he-dns-update.sh 4
    #
    # Update IPv6 (AAAA) record:
    #
    # $ ./he-dns-update.sh 6
    #
    # Logs / Output
    # -------------
    #
    # Output is sent to syslog instead of stdout (unless there is an error with the config file).
    #
    # Examples:
    #
    # Apr 1 12:00:03 gw HE-DNS: OK: ipv4 address updated: good 12.34.56.78
    # Apr 1 12:00:05 gw HE-DNS: OK: ipv4 address no change: nochg 12.34.56.78
    # Apr 1 12:00:11 gw HE-DNS: ERROR: ipv6 address not updated : curl timed out.
    #
    # Cron example
    # ------------
    # Hourly:
    #
    # 0 * * * * /usr/local/bin/he-dns-update.sh 4
    # 0 * * * * /usr/local/bin/he-dns-update.sh 6
    #
    # Author
    # ------
    # Joe Miller / https://github.com/joemiller 2016
    #


    # load config file and verify required args
    . "/etc/he-dns-update.conf"
    if [ -z "$HOSTNAME" ] || [ -z "$KEY" ]; then
    echo "ERROR: Config file '/etc/he-dns-update.conf' is missing one or more required vars: HOSTNAME, KEY"
    exit 1
    fi

    proto="$1"
    if [ -z "$proto" ]; then
    echo "Usage: $0 [4|6]"
    exit 1
    fi

    LOGGER_BIN="logger"
    CURL_BIN="/usr/local/bin/curl"
    CURL_TIMEOUT_SECS=10

    out=$("$CURL_BIN" \
    -"$proto" \
    --silent \
    -m "$CURL_TIMEOUT_SECS" \
    -k \
    https://dyn.dns.he.net/nic/update \
    -d "hostname=$HOSTNAME" \
    -d "password=$KEY" 2>&1)

    # $out will be empty on timeout, so we catch curl's return code for timeout and populate
    # $out with a helpful message. Could do this for other common errors from curl too.
    rc=$?
    if [ "$rc" == 28 ]; then
    out="curl timed out."
    fi

    # possible outputs from curl:
    #
    # on success, either:
    # good 192.168.0.1
    # nochg 192.168.0.1
    #
    # on error:
    # could be anything else. we log an error with the full output.

    case $out in
    *good*)
    "$LOGGER_BIN" -p local0.info -t HE-DNS "OK: ipv$proto address updated: $out" ;;
    *nochg*)
    "$LOGGER_BIN" -p local0.notice -t HE-DNS "OK: ipv$proto address no change: $out" ;;
    *)
    "$LOGGER_BIN" -p local0.err -t HE-DNS "ERROR: ipv$proto address not updated : $out" ;;
    esac