Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Powershell dynamic dns cloudflare script

a guest Nov 13th, 2016 204 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. $domain_list = ("<fqdn for dynamic update>")
  2. $email = "<email from cloudflare>"
  3. $api_key = "<api key from cloudflare>"
  4.  
  5. function Get-externalIp() {
  6. try {
  7. $ip = invoke-restmethod 'https://api.ipify.org?format=json' | select -expandproperty IP
  8. return $ip
  9. } catch {
  10. return $false
  11. }
  12. }
  13.  
  14.  
  15. function create-CFdns() {
  16. Param(
  17.   [Parameter(Mandatory=$true)]
  18.   [string] $DNSname,
  19.   [Parameter(Mandatory=$true)]
  20.   [string] $type,
  21.   [Parameter(Mandatory=$true)]
  22.   [string] $ip_update,
  23.   [string] $id="NS"
  24.   )
  25. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  26. $headers.Add("Content-Type", "application/json")
  27. $headers.Add("X-Auth-Key", "$api_key")
  28. $headers.Add("X-Auth-Email", "$email")
  29. $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  30.  
  31.     if(-not($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers)) {
  32.         return $false
  33.     }
  34.  
  35.     if(-not ($result.result | where { $_.name -eq "$DNSname"})) {
  36.         try {
  37.             $json = "{""type"":""" + $type +""",
  38.                      ""name"":""" + $dnsname + """,
  39.                      ""content"":""" + $ip_update + """ }"
  40.             $result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/$id/dns_records/" -Method POST -Body $json -headers $headers
  41.             return $result.result
  42.         } catch {
  43.             return $false
  44.         }
  45.     } else {
  46.         return $false
  47.     }
  48. }
  49.  
  50. function update-CFdns() {
  51. Param(
  52.   [Parameter(Mandatory=$true)]
  53.   [string] $DNSname,
  54.   [Parameter(Mandatory=$true)]
  55.   [string] $ip_update,
  56.   [string] $id="NS"
  57.   )
  58.  
  59. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  60. $headers.Add("Content-Type", "application/json")
  61. $headers.Add("X-Auth-Key", "$api_key")
  62. $headers.Add("X-Auth-Email", "$email")
  63. $uri_base = "https://api.cloudflare.com/client/v4/zones/" + $id
  64.  
  65.     if($id -eq "NS") {
  66.     $id = get-cfzoneid $DNSname
  67.     }
  68.  
  69.     if(-not ($result = invoke-restmethod -Uri "$uri_base/dns_records" -Method GET  -headers $headers)) {
  70.     return $false
  71.     }
  72.  
  73.     if($data = $result.result | where { $_.name -eq "$DNSname"}) {
  74.         try {
  75.             $data | add-member "content" "$ip_update" -force
  76.             $json = $data | ConvertTo-Json
  77.             $query_url = $uri_base + "/dns_records/" + $data.id
  78.             $result = invoke-restmethod -Uri $query_url -Method PUT -Body $json -headers $headers
  79.             return $result.result
  80.         } catch {
  81.             return $_
  82.         }
  83.     } else {
  84.         return $false
  85.     }
  86. }
  87.  
  88. function get-cfzoneid() {
  89. Param(
  90.     [string] $DNSname
  91. )
  92. $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
  93. $headers.Add("Content-Type", "application/json")
  94. $headers.Add("X-Auth-Key", "$api_key")
  95. $headers.Add("X-Auth-Email", "$email")
  96.  
  97.     if($result = invoke-restmethod -Uri "https://api.cloudflare.com/client/v4/zones/" -Method GET  -headers $headers | select result) {
  98.         if($DNSname.count -ge 1) {
  99.             $dns_tmp = $DNSname.split(".")
  100.             $zone = $dns_Tmp[$dns_tmp.count-2] + "." + $dns_Tmp[$dns_tmp.count-1]
  101.         }
  102.        
  103.         if ($id = $result.result | where { $_.name -match $zone} | select -expandproperty id) {
  104.             return $id
  105.         } else {
  106.             return $false
  107.         }
  108.     }
  109. }
  110.  
  111. $current_ip = Get-externalIp
  112. if($current_ip) {
  113.     foreach($domain in $domain_list) {
  114.     $id = get-cfzoneid $domain
  115.         if(-not(update-CFdns $domain $current_ip $id)){
  116.         create-CFdns $domain A $current_ip $id
  117.         }
  118.     }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top