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