ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Dynamic DNS with CloudFlare

    IT Discussion
    cloudflare api linux dns edge router
    8
    25
    4.4k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stacksofplatesS
      stacksofplates
      last edited by stacksofplates

      So I have a dynamic DNS service for my ERL, but it's not in my actual domain. So I made a small script that updates my CloudFlare DNS from my ERL with their API. There is a cron job that just runs this script every 5 minutes.

      #!/bin/bash
      
      key="your-api-key"
      zoneID="your-zone-id"
      email="[email protected]"
      recordID="record-id-to-update"
      recordName="newrecord.yourdomain.com"
      ip=$(ifconfig eth0 | grep "inet addr:" | cut -d: -f2 | awk '{ print $1 }')
      
      curl -X PUT "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records/$recordID" \
           -H "X-Auth-Email: $email" \
           -H "X-Auth-Key: $key" \
           -H "Content-Type: application/json" \
           --data '{"type":"A","name":"'"$recordName"'","content":"'"$ip"'","ttl":120,"proxied":false}' -k
      

      You do have to get the record id from the API. I haven't found a way to get it through the web interface. So that means you have to create the record initially, either through the API or the web interface (or use an existing one). Once you have the ID, just paste it in the variable.

      dbeatoD 1 Reply Last reply Reply Quote 8
      • stacksofplatesS
        stacksofplates
        last edited by

        You can do this with a device on your LAN also. Instead of getting the IP from the interface just use something like

        ip=$(curl http://icanhazip.com)
        
        1 Reply Last reply Reply Quote 1
        • dbeatoD
          dbeato @stacksofplates
          last edited by

          @stacksofplates interesting that would work awesome. Gotta test it this week.

          1 Reply Last reply Reply Quote 1
          • scottalanmillerS
            scottalanmiller
            last edited by

            Awesome, thanks.

            1 Reply Last reply Reply Quote 0
            • B
              bnrstnr
              last edited by

              Are you guys still using this? Do you think it's the best method still?

              1 Reply Last reply Reply Quote 0
              • JaredBuschJ
                JaredBusch
                last edited by

                This is interesting.
                But what about jsut using a CNAME to your DynDNS name? That is how I handle it.

                I have jared.bundystl.com as a CNAME on CloudFlare for sorvani.mooo.com from afraid.org. My router keeps afraid.org up to date.

                B stacksofplatesS 2 Replies Last reply Reply Quote 4
                • B
                  bnrstnr @JaredBusch
                  last edited by

                  @jaredbusch good call, that sounds way easier than what’s above. Setting it up your way now. Thanks

                  1 Reply Last reply Reply Quote 0
                  • stacksofplatesS
                    stacksofplates @JaredBusch
                    last edited by stacksofplates

                    @jaredbusch said in Dynamic DNS with CloudFlare:

                    This is interesting.
                    But what about jsut using a CNAME to your DynDNS name? That is how I handle it.

                    I have jared.bundystl.com as a CNAME on CloudFlare for sorvani.mooo.com from afraid.org. My router keeps afraid.org up to date.

                    I just don’t have to do accounts with anything else, that’s all. This just makes it a one stop shop. I actually stopped running it on my ERL and run it on one of the servers here. It’s been running since I did this and have never had any issues.

                    1 Reply Last reply Reply Quote 1
                    • NashBrydgesN
                      NashBrydges
                      last edited by

                      @stacksofplates Where do you obtain your record ID? I easily have the record name but can't seem to locate a record ID for my A record. Or is "A" what you're referring to as record ID?

                      B 1 Reply Last reply Reply Quote 0
                      • NashBrydgesN
                        NashBrydges
                        last edited by NashBrydges

                        Interesting feature with Cloudflare that I just discovered is that they flatten a CNAME record to the root of the domain. So I can enter my DDNS domain as a CNAME record. It will query the IP of that domain and return the IP address to use for the root domain. So this script isn't really needed since it will do the lookup for you.

                        B stacksofplatesS 2 Replies Last reply Reply Quote 0
                        • NashBrydgesN
                          NashBrydges
                          last edited by

                          0_1519848967546_03ce84de-9460-4f81-ae28-0d14371addf8-image.png

                          1 Reply Last reply Reply Quote 0
                          • B
                            bnrstnr @NashBrydges
                            last edited by bnrstnr

                            @nashbrydges said in Dynamic DNS with CloudFlare:

                            Where do you obtain your record ID?

                            #!/bin/bash
                            
                            key="Your_Global_API_Key"
                            zoneID="Your_Zone_ID"
                            email="[email protected]"
                            recordName="example.com"
                            
                            curl -X GET "https://api.cloudflare.com/client/v4/zones/$zoneID/dns_records?type=A&name=$recordName" \
                                 -H "X-Auth-Email: $email" \
                                 -H "X-Auth-Key: $key" \
                                 -H "Content-Type: application/json"
                            
                            1 Reply Last reply Reply Quote 0
                            • B
                              bnrstnr @NashBrydges
                              last edited by bnrstnr

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • B
                                bnrstnr
                                last edited by

                                @NashBrydges I would have bet money that the $recordID was just the short name of the record (ie. "sub" of "sub.example.com"). I swear I did it like that and it worked, but apparently not :flushed_face:.

                                The script to get the real identifier is above.

                                NashBrydgesN 1 Reply Last reply Reply Quote 0
                                • NashBrydgesN
                                  NashBrydges @bnrstnr
                                  last edited by

                                  @bnrstnr said in Dynamic DNS with CloudFlare:

                                  @NashBrydges I would have bet money that the $recordID was just the short name of the record (ie. "sub" of "sub.example.com"). I swear I did it like that and it worked, but apparently not :flushed_face:.

                                  The script to get the real identifier is above.

                                  Thanks. I'll give that a try later.

                                  1 Reply Last reply Reply Quote 0
                                  • stacksofplatesS
                                    stacksofplates @NashBrydges
                                    last edited by

                                    @nashbrydges said in Dynamic DNS with CloudFlare:

                                    Interesting feature with Cloudflare that I just discovered is that they flatten a CNAME record to the root of the domain. So I can enter my DDNS domain as a CNAME record. It will query the IP of that domain and return the IP address to use for the root domain. So this script isn't really needed since it will do the lookup for you.

                                    Ya I don’t have a DDNS name so that’s why I Serb it up this way.

                                    1 Reply Last reply Reply Quote 0
                                    • 3
                                      360col
                                      last edited by

                                      Very useful. bookmarking this!

                                      1 Reply Last reply Reply Quote 0
                                      • NashBrydgesN
                                        NashBrydges
                                        last edited by

                                        I've confirmed that Cloudflare's CNAME flattening feature works exactly as advertised.

                                        Create a CNAME record with following values:

                                        • Type = CNAME
                                        • Name = Your root mydomain.com domain name
                                        • Domain Name = Your DDNS domain subdomain.ddns.net (I use No-IP for example but change yours as needed)

                                        I then created another CNAME record to handle the www subdomain and everything works as expected. Both root domain name and www route correctly.

                                        B 1 Reply Last reply Reply Quote 0
                                        • B
                                          bnrstnr @NashBrydges
                                          last edited by

                                          @nashbrydges I thought somebody already said this, but I cant find it. The only bad part about the CNAME method if you're using a free account on no-ip, then you have to login to their site and confirm that you're still using the DDNS every 30 days, which is a PITA. Setup the script with a cron job and never have to worry about checking in on no-ip or afraid.org every n days.

                                          NashBrydgesN JaredBuschJ 2 Replies Last reply Reply Quote 0
                                          • B
                                            bnrstnr
                                            last edited by bnrstnr

                                            @stacksofplates said in Dynamic DNS with CloudFlare:

                                            ip=$(curl http://icanhazip.com)

                                            While I was looking into the Cloudflare API a little further yesterday, I came across this. I don't know much about it, but here it is.

                                            Consider replacing curl -s http://icanhazip.com with dig +short myip.opendns.com @resolver1.opendns.com. It works exactly the same.

                                            Querying icanhazip.com or other similar sites requires a fairly expensive TCP connection, HTTP overhead, etc. This can be burdensome when queried regularly via cron jobs. A UDP-based DNS connection is considerably faster, lighter weight, and uses far less resources.

                                            1 Reply Last reply Reply Quote 0
                                            • 1
                                            • 2
                                            • 1 / 2
                                            • First post
                                              Last post