Ostatnio aktywny 2 days ago

Lookup current IPv4 and IPv6 addresses and updates them on Hetzner DNS if they changed. Saves current IP addresses in `$HOME/.my-ipv4` and `$HOME/.my-ipv6`

Rewizja 012ba86c2627cdad4cfb61f9762e50c9d94b31a2

update-ip.sh Surowy
1#!/bin/bash
2# Created on 2025-12-07 12:22 UTC
3#
4# Removed IPv6 as this way does not get the real IPv6 address (why?)
5# 2025-12-24
6
7# Get my outgoing ipv4 address via ifconfig.me and compare it with the recent ip saved in ~/.my-ipv4
8# Update dns with the actual ipv4 if it changed
9
10# set default value for CHECK
11# override this when starting the script like `CHECK=true update-ip.sh`
12: "${CHECK:=false}"
13
14if [ "${CHECK}x" = "truex" ]
15then
16 # currently abort if jq is not installed
17 # might enter another routine to still display the record at the end
18 command -v jq > /dev/null 2>&1 || { echo >&2 "jq not found"; exit 1; }
19
20 # jq may be placed somewhere in /usr/local so first get the real path
21 JQ=$(command -v jq)
22fi
23
24# specify file names
25IPFILE4="${HOME}/.my-ipv4"
26IPFILE6="${HOME}/.my-ipv6"
27
28# Auth token (Hetzner new dns console as of Dec 2025)
29API_URL="https://api.hetzner.cloud/v1/zones/"
30AUTH_TOKEN=""
31
32# Domain settings (zone id, rrset name, rrset type)
33ZONE_ID="domain.local"
34RR_NAME="@"
35RR_TYPE4="A"
36RR_TYPE6="AAAA"
37RR_COMMENT4=""
38RR_COMMENT6=""
39
40# remote ipv4
41IPREMOTE4=$(curl -4 -sgkL https://ifconfig.me/ip 2>/dev/null)
42# IPREMOTE6=$(curl -6 -sgkL https://ifconfig.me/ip 2>/dev/null)
43# echo "DBG: got ip ${IPREMOTE4}"
44
45# check if remote ips contain valid data
46# TODO:
47# get info about useful regex on ipv4 and ipv6 addresses
48
49# check if old IP4 is saved in file
50if [ -w "${IPFILE4}" -a -f "${IPFILE4}" ]
51then
52 SAVEDIP4=$(cat ${IPFILE4})
53 # echo "DBG: saved ip is ${SAVEDIP4}"
54else
55 # no existing ipv4 file, create one and add new ipv4 into it
56 /bin/echo -n "${IPREMOTE4}" > ${IPFILE4}
57 # echo "DBG: saved ip ${IPREMOTE4} to file ${IPFILE4}"
58fi
59
60#check for IP6
61# if [ -w "${IPFILE6}" -a -f "${IPFILE6}" ]
62# then
63# SAVEDIP6=$(cat ${IPFILE6})
64# # echo "DBG: saved ip is ${SAVEDIP4}"
65# else
66# # no existing ipv4 file, create one and add new ipv4 into it
67# /bin/echo -n "${IPREMOTE6}" > ${IPFILE6}
68# # echo "DBG: saved ip ${IPREMOTE4} to file ${IPFILE4}"
69# fi
70
71# check if remote ip is identical as locally saved ip
72if [ "${SAVEDIP4}x" = "${IPREMOTE4}x" ]
73then
74 # if remote ip is equal as locally saved (old) ip
75 # echo "DBG: do nothing, ips are equal"
76 echo >&2 "IPv4 identical. Nothing to do"
77else
78 echo >&2 "Setting IPv4 address to ${IPREMOTE4}"
79 # ips differ, update dns ip with remote ip and save into local ip file
80 curl -s -g -X POST -H "Authorization: Bearer ${AUTH_TOKEN}" \
81 -H "Content-Type: application/json" \
82 -d '{"records":[{"value":"'${IPREMOTE4}'","comment":"'${RR_COMMENT4}'"}]}' \
83 "${API_URL}${ZONE_ID}/rrsets/${RR_NAME}/${RR_TYPE4}/actions/set_records" > /dev/null
84 /bin/echo -n "${IPREMOTE4}" > ${IPFILE4}
85fi
86
87if [ "${CHECK}x" = "truex" ]
88then
89 # finally check (output actual json record saved on dns server) remote ip
90 curl -s -H "Authorization: Bearer ${AUTH_TOKEN}" \
91 "${API_URL}${ZONE_ID}/rrsets/${RR_NAME}/${RR_TYPE4}" | ${JQ} "[.[] | .records]"
92fi
93
94# and repeat for ipv6
95# if [ "${SAVEDIP6}x" = "${IPREMOTE6}x" ]
96# then
97# # if remote ip is equal as locally saved (old) ip
98# # echo "DBG: do nothing, ips are equal"
99# echo >&2 "IPv6 identical. Nothing to do"
100# else
101# echo >&2 "Setting IPv6 address to ${IPREMOTE6}"
102# # ips differ, update dns ip with remote ip and save into local ip file
103# curl -s -g -X POST -H "Authorization: Bearer ${AUTH_TOKEN}" \
104# -H "Content-Type: application/json" \
105# -d '{"records":[{"value":"'${IPREMOTE6}'","comment":"'${RR_COMMENT6}'"}]}' \
106# "${API_URL}${ZONE_ID}/rrsets/${RR_NAME}/${RR_TYPE6}/actions/set_records" > /dev/null
107# /bin/echo -n "${IPREMOTE6}" > ${IPFILE6}
108# fi
109#
110# if [ "${CHECK}x" = "truex" ]
111# then
112# # also check for ipv6
113# curl -s -H "Authorization: Bearer ${AUTH_TOKEN}" \
114# "${API_URL}${ZONE_ID}/rrsets/${RR_NAME}/${RR_TYPE6}" | ${JQ} "[.[] | .records]"
115# fi
116