Advertisement
teknoraver

cloudflare dyndns

Nov 16th, 2019
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.18 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # interface going down
  4. if [ "${0#*ip-down.d/}" = "${0##*/}" ]; then
  5.     exec killall -w "${0##*/}"
  6. fi
  7.  
  8. [ "$PPP_IFACE" = 'pppoe-data' ] || exec logger -p daemon.info -t dyndns "Not applying DNS to iface '$PPP_IFACE'"
  9.  
  10. read domain </proc/sys/kernel/hostname
  11.  
  12. domain=$domain.example.com
  13. email=
  14. key=
  15. zone=
  16. api=https://api.cloudflare.com/client/v4/zones/$zone/dns_records
  17.  
  18. auth="-H X-Auth-Email:$email -H X-Auth-Key:$key"
  19.  
  20. updatedns() {
  21.     ret=0
  22.     if ! id=$(curl -s $auth $api |jq -er ".result[] | select(.name == \"$domain\") | .id"); then
  23.         return 1
  24.     fi
  25.     uri="$api/$id"
  26.     json=/tmp/dyndns-$$.json
  27.  
  28.     curl -sS -XPUT $uri $auth \
  29.         -H 'Content-Type: application/json' \
  30.         -d "{\"type\":\"A\", \"name\":\"$domain\", \"content\":\"$IPLOCAL\"}" >$json
  31.     if jq -ec '.success == false' <$json >/dev/null; then
  32.         logger -p daemon.warning -t dyndns "server returned '$(jq -c .errors <$json)'"
  33.         ret=1
  34.     fi
  35.     rm -f $json
  36.     return $ret
  37. }
  38.  
  39. i=60
  40. while [ $i -ge 0 ]; do
  41.     updatedns && exec logger -p daemon.info -t dyndns "DNS for $domain set to $IPLOCAL"
  42.     sleep 5
  43.     logger -p daemon.notice -t dyndns "Retry setting DNS for $domain ($i more tries)"
  44.     i=$((i - 1))
  45. done &
  46.  
  47. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement