cloudflare.sh 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/sh
  2. #Environment Variables
  3. ZONE=${ZONE:-example.com}
  4. HOST=${HOST:-example.com}
  5. EMAIL=${EMAIL:-example@example.com}
  6. API=${API:-1111111111111111}
  7. TTL=${TTL:1}
  8. PROXY=${PROXY:true}
  9. echo "Current time: $(date "+%Y-%m-%d %H:%M:%S")"
  10. ip_file="ip"
  11. new_ip=$(curl -s http://ipecho.net/plain)
  12. if [ -f $ip_file ]; then
  13. ip=$(cat $ip_file)
  14. if [ "$ip" = "$new_ip" ]; then
  15. echo "Same ip: $ip"
  16. exit 0
  17. fi
  18. fi
  19. ip="$new_ip"
  20. echo "IP: $ip"
  21. zone_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones?name=$ZONE" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | grep -Eo '"id":.?"\w*?"' |head -1|grep -o ':.*".*"'|grep -o '\w*')
  22. echo "Zone ID: $zone_id"
  23. record_id=$(curl -s -X GET "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records?name=$HOST" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" | grep -Eo '"id":.?"\w*?"' |head -1|grep -o ':.*".*"'|grep -o '\w*')
  24. echo "Record ID: $record_id"
  25. update=$(curl -s -X PUT "https://api.cloudflare.com/client/v4/zones/$zone_id/dns_records/$record_id" -H "X-Auth-Email: $EMAIL" -H "X-Auth-Key: $API" -H "Content-Type: application/json" --data "{\"id\":\"$zone_id\",\"type\":\"A\",\"name\":\"$HOST\",\"content\":\"$ip\",\"ttl\":$TTL,\"proxied\":$PROXY}")
  26. if echo "$update" | grep -q "\"success\":true"; then
  27. echo "IP changed to: $ip"
  28. echo "$ip" > $ip_file
  29. else
  30. printf "Update failed:\\n%s" "$update"
  31. exit 1
  32. fi