From 6aa89eaef7eeffbd9bbd35ad79d5850159e00245 Mon Sep 17 00:00:00 2001 From: tiennm99 Date: Wed, 22 May 2024 20:18:32 +0700 Subject: [PATCH] [Add] init --- update-dns/README.md | 26 +++++++++++++++++++++++++- update-dns/main.sh | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 update-dns/main.sh diff --git a/update-dns/README.md b/update-dns/README.md index 5c6db50..6e13fd2 100644 --- a/update-dns/README.md +++ b/update-dns/README.md @@ -1 +1,25 @@ -Update CloudFlare DNS records with dynamic IP +# update-dns + +Update CloudFlare DNS records with dynamic IP when your IP is behind router's IP. + +## How to use this script + +```bash +git clone https://github.com/tiennm99/update-dns +cd update-dns +nano main.sh +# Edit value in the script +./main.sh +``` + +You can also use the script with cronjob to update your IP every 5 minutes + +```bash +crontab -e +# Add the following line to the file +*/5 * * * * /path/to/main.sh +``` + +## __Note:__ +You may need to config your router to forward the ports you wanted to the exact machine. +If you have problems, let's discuss in the discussions section. diff --git a/update-dns/main.sh b/update-dns/main.sh new file mode 100644 index 0000000..cda54b9 --- /dev/null +++ b/update-dns/main.sh @@ -0,0 +1,35 @@ +#!/bin/bash + +# Configuration +CF_EMAIL="your@email.com" +CF_API_KEY="your_cloudflare_api_key" +ZONE_ID="your_zone_id" +RECORDS=("subdomain1.example.com" "subdomain2.example.com" "subdomain3.example.com") + +# Get current IP address using upnpc +CURRENT_IP=$(upnpc -s | awk -F'= ' '/ExternalIPAddress/ { print $2 }') + +# Function to update DNS record +update_record() { + local record="$1" + local record_id=$(curl -sX GET "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records?type=A&name=$record" \ + -H "X-Auth-Email: $CF_EMAIL" \ + -H "Authorization: Bearer $CF_API_KEY" \ + -H "Content-Type: application/json" | jq -r '.result[0].id') + + if [ -n "$record_id" ]; then + curl -sX PUT "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/dns_records/$record_id" \ + -H "X-Auth-Email: $CF_EMAIL" \ + -H "Authorization: Bearer $CF_API_KEY" \ + -H "Content-Type: application/json" \ + --data "{\"type\":\"A\",\"name\":\"$record\",\"content\":\"$CURRENT_IP\"}" + echo "Updated $record to $CURRENT_IP" + else + echo "Error: $record not found" + fi +} + +# Update DNS records +for record in "${RECORDS[@]}"; do + update_record "$record" +done