Last active 2 hours ago

Useful for dynamic IPs and automatic updates to combine with cron

update_name.sh Raw
1#!/bin/bash
2
3# Configuration
4USERNAME=""
5API_TOKEN=""
6DOMAIN=""
7SUBDOMAIN="" # Leave empty or use "@" for the root domain
8RECORD_ID="" # You can find this via 'List DNS Records' in their API docs
9
10IP_FILE="ip.txt"
11
12# Get current public IP
13NEW_IP=$(curl -s https://api.ipify.org)
14
15if [ -f $IP_FILE ]; then
16 OLD_IP=$(cat $IP_FILE)
17 if [ $NEW_IP == $OLD_IP ]; then
18 echo "IP has not changed."
19 exit 0
20 fi
21fi
22
23# Update the record via Name.com API v4
24curl -u "$USERNAME:$API_TOKEN" \
25 -X PUT \
26 -H "Content-Type: application/json" \
27 -d "{\"answer\":\"$NEW_IP\", \"host\":\"$SUBDOMAIN\", \"type\":\"A\", \"ttl\":300}" \
28 "https://api.name.com/v4/domains/$DOMAIN/records/$RECORD_ID"
29
30echo "$NEW_IP" > $IP_FILE