Skip to content

Instantly share code, notes, and snippets.

@akrizs
Created May 21, 2018 00:40
Show Gist options
  • Select an option

  • Save akrizs/bccdf69af52c3edbc5d8a6cad11e1e8c to your computer and use it in GitHub Desktop.

Select an option

Save akrizs/bccdf69af52c3edbc5d8a6cad11e1e8c to your computer and use it in GitHub Desktop.

Revisions

  1. akrizs created this gist May 21, 2018.
    47 changes: 47 additions & 0 deletions do-dyndns.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash
    # Function to split out the json value fetched from DigitalOcean to parse out just the IP adress.
    # You have to install jq for this to work, the parsing of the current stored ip depends on it: https://stedolan.github.io/jq/
    function extractip {
    temp=`echo $ip_received | jq -r '.domain_record.data'`
    echo ${temp}
    };

    # Your domain name
    domain="YOUR-DOMAIN"

    # Record ID to update (subdomain)
    record="YOUR-DNS-RECORD"

    # API Key.
    api_key="YOUR-DIGITAL-OCEAN-API-KEY"

    # Feth ip from external source.
    current_ip=$(curl ifconfig.co)

    # Fetch IP from interface.
    #current_ip="$(ifconfig re0 | grep -E 'inet.[0-9]' | grep -v '127.0.0.1'| awk '{
    #print $2}')"

    ##### Fetch current IP stored on Digital Ocean.
    # Fetch the json object.
    ip_received="$(curl \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $api_key" \
    -X GET "https://api.digitalocean.com/v2/domains/$domain/records/$record")"

    # Run the json parsing script and get the value of {data:"ipaddress"}.
    stored_ip=$(extractip)

    if [ "$current_ip" = "$stored_ip" ]
    then
    echo "No update needed"
    echo $stored_ip
    echo $current_ip
    else
    echo ip_sent="$(curl \
    -k \
    -H "Authorization: Bearer $api_key" \
    -H "Content-Type: application/json" \
    -d '{"data": "'"$current_ip"'"}' \
    -X PUT "https://api.digitalocean.com/v2/domains/$domain/records/$record")"
    fi