Skip to content

Instantly share code, notes, and snippets.

@mcarpenterjr
Created July 17, 2017 15:13
Show Gist options
  • Save mcarpenterjr/1dedf9f9f842710ba5f07937ef4f5ae4 to your computer and use it in GitHub Desktop.
Save mcarpenterjr/1dedf9f9f842710ba5f07937ef4f5ae4 to your computer and use it in GitHub Desktop.
Linux bash script for updating dns on godaddy dynamically. You need a developer account to access the godaddy api to get your secret and token.
#!/bin/bash
# This script is used to check and update your GoDaddy DNS server to the IP address of your current internet connection.
# Special thanks to mfox for his ps script
# https://github.com/markafox/GoDaddy_Powershell_DDNS
#
# First go to GoDaddy developer site to create a developer account and get your key and secret
#
# https://developer.godaddy.com/getstarted
# Be aware that there are 2 types of key and secret - one for the test server and one for the production server
# Get a key and secret for the production server
#
#Update the first 4 variables with your information
domain="" # your domain
name="" # name of A record to update
key="" # key for godaddy developer API
secret="" # secret for godaddy developer API
headers="Authorization: sso-key $key:$secret"
# echo $headers
result=$(curl -s -X GET -H "$headers" \
"https://api.godaddy.com/v1/domains/$domain/records/A/$name")
dnsIp=$(echo $result | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
# echo "dnsIp:" $dnsIp
# Get public ip address there are several websites that can do this.
ret=$(curl -s GET "http://ipinfo.io/json")
currentIp=$(echo $ret | grep -oE "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b")
# echo "currentIp:" $currentIp
if [ $dnsIp != $currentIp ];
then
# echo "Ips are not equal"
request='{"data":"'$currentIp'","ttl":3600}'
# echo $request
nresult=$(curl -i -s -X PUT \
-H "$headers" \
-H "Content-Type: application/json" \
-d $request "https://api.godaddy.com/v1/domains/$domain/records/A/$name")
# echo $nresult
fi
@SanPollo
Copy link

SanPollo commented May 3, 2024

This no longer works unless you have 20 or over (or maybe 50 or over) domains with GoDaddy as they have disabled the API without telling anyone. You will get a "Authenticated user is not allowed access" response.

@spjspj
Copy link

spjspj commented May 20, 2024

Updated name server to cloudflare.


#!/usr/bin/perl
##
#   File : cloudflare.pl
#   Purpose : Update cloudflare
##
use strict;
use LWP::Simple;
use POSIX qw(strftime);
## 20240504 - Updated my nameserver to cloudflare
##     blahblah@blah.com's Account
##     Godaddy - nameserver updated to
##     randoname.ns.cloudflare.com randoname2.ns.cloudflare.com
## curl -X GET "https://api.cloudflare.com/client/v4/user/tokens/verify" -H "Authorization: Bearer HXxC_M9VZpqyUUBG4V3K0mbx6shU4PGEzhg4Pggw" -H "Content-Type:application/json"
## {"result":{"id":"3eada03345bd930d33bda2444814fd75","status":"active"},"success":true,"errors":[],"messages":[{"code":10000,"message":"This API Token is valid and active","type":null}]}
## blah@lappie : /home/blah/mysite_certificates #
# Main
{
    my $set_ip = `nslookup mysite.org`;
    print $set_ip;
    $set_ip =~ m/Address *: *([\d\.]+)/im;
    $set_ip = $1;
    my $current_ip = `curl -s GET "http://icanhazip.com"`;
    print $current_ip;
    my $vals1 = $set_ip;
    $vals1 =~ s/\W//img;
    my $vals2 = $current_ip;
    $vals2 =~ s/\W//img;
    if ($vals1 ne $vals2)
    {
        print ("IPs are different ($set_ip is not $current_ip)\n");
        my $str = "curl --request PUT --url https://api.cloudflare.com/client/v4/zones/58553d8109b596a0de35d6bdbead1f24/dns_records/d89b1dfafbb9e0ea356c1f6491e9a142 -H 'Content-Type: application/json' -H 'X-Auth-Email: blahblah\@blah.com' -H 'Authorization: Bearer HXxC_G4V3K9MVZpqm0bxy6UUsBhUP4GEzhg4Pggw' --data '{\"content\": \"NEW_IP\", \"name\": \"mysite.org\", \"proxied\": false, \"type\": \"A\", \"comment\": \"Domain verification record\", \"ttl\": 3600 }'";

        chomp $current_ip;
        $str =~ s/NEW_IP/$current_ip/;
        open OUTPUT, "> /home/blah/mysite_certificates/fixed_set.sh";
        print OUTPUT $str;
        close OUTPUT;
        `chmod 750 /home/blah/mysite_certificates/fixed_set.sh`;
        `/home/blah/mysite_certificates/fixed_set.sh`;
    }
    else
    {
        print ("IPs are the same\n");
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment