Last active
July 24, 2020 09:22
-
-
Save kookxiang/2a2679dade5ff51d1bdbd2513dc8262f to your computer and use it in GitHub Desktop.
simple ddns script for cloudflare
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
CF_TOKEN="********************************" | |
CF_ZONEID="*************************" | |
while true | |
do | |
sleep 10 | |
CURRENT_IP=$(ip addr show dev ppp0 | sed -e's/^.*inet \([^ ]*\) .*$/\1/;t;d' | head -1) | |
CURRENT_IP6=$(ip addr show dev ppp0 | grep global | sed -e's/^.*inet6 \([^ ]*\)\/.*$/\1/;t;d' | head -1) | |
PREVIOUS_IP=$(cat /tmp/ddns 2>&1 || true) | |
PREVIOUS_IP6=$(cat /tmp/ddns6 2>&1 || true) | |
# ipv4 | |
if [ -e "$CURRENT_IP" ]; then | |
echo "ERROR: current address not found!" | |
elif ! [ "$CURRENT_IP" = "$PREVIOUS_IP" ]; then | |
CF_RECORD_ID="*************************" | |
CF_RECORD="********************" | |
echo "Send new ipv4 address to CloudFlare..." | |
CF_RESULT=$(curl -sSL -X PUT https://api.cloudflare.com/client/v4/zones/$CF_ZONEID/dns_records/$CF_RECORD_ID \ | |
-H 'Content-Type: application/json' \ | |
-H 'Authorization: Bearer '$CF_TOKEN \ | |
-d '{"type": "A", "name": "'$CF_RECORD'", "content": "'$CURRENT_IP'", "proxied": false, "ttl": 120}') | |
if echo $CF_RESULT | grep '"success": true' > /dev/null; then | |
echo $CURRENT_IP > /tmp/ddns | |
else | |
echo $CF_RESULT | |
fi | |
fi | |
# ipv6 | |
if [ -e "$CURRENT_IP6" ]; then | |
echo "ERROR: current address not found!" | |
elif ! [ "$CURRENT_IP6" = "$PREVIOUS_IP6" ]; then | |
CF_RECORD_ID="*************************" | |
CF_RECORD="*************************" | |
echo "Send new ipv6 address to CloudFlare..." | |
CF_RESULT=$(curl -sSL -X PUT https://api.cloudflare.com/client/v4/zones/$CF_ZONEID/dns_records/$CF_RECORD_ID \ | |
-H 'Content-Type: application/json' \ | |
-H 'Authorization: Bearer '$CF_TOKEN \ | |
-d '{"type": "AAAA", "name": "'$CF_RECORD'", "content": "'$CURRENT_IP6'", "proxied": false, "ttl": 120}') | |
if echo $CF_RESULT | grep '"success": true' > /dev/null; then | |
echo $CURRENT_IP6 > /tmp/ddns6 | |
else | |
echo $CF_RESULT | |
fi | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment