溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

PowerShell 給現(xiàn)有DNS記錄創(chuàng)建PTR反向查詢

發(fā)布時間:2020-06-25 14:24:01 來源:網(wǎng)絡(luò) 閱讀:2189 作者:beanxyz 欄目:開發(fā)技術(shù)

今天早上豆子無意中發(fā)現(xiàn)公司的DNS服務(wù)器上面只有正向的解析,而沒有對應(yīng)的PTR記錄。換句話說,可以通過域名來解析IP地址,但是倒過來IP地址是找不著域名的。


1個小時寫了個很簡單的腳本,判斷已有的記錄是否存在對應(yīng)的reverse zone 和PTR記錄,如果沒有的話,自動給我創(chuàng)建加上。


思路很簡單,腳本也比較糙,沒有任何容錯處理和優(yōu)化,不過實現(xiàn)功能就好。

$ptrzones=Get-DnsServerzone -ComputerName syddc01 | Where-Object {$_.zonename -like "*.arpa"}
#獲取所以的A記錄
$machines=Get-DnsServerResourceRecord -ComputerName syddc01 -RRType A -ZoneName 'omnicom.com.au'| select @{n='IP';e={$_.recorddata.IPV4Address.IPAddressToString}}, hostname, timestamp, @{n='PTRZone';e={$temp=$_.recorddata.IPV4Address.IPAddressToString.split('.');$t=$temp[2]+'.'+$temp[1]+'.'+$temp[0]+‘.in-addr.arpa’;$t}}

foreach($machine in $machines){
  
  #判斷是否存在PTR的reverse zone
  write-host $machine.hostname
  write-host $machine.PTRZone 
  $flag=0
  foreach($p in $ptrzones){
    if($p.zonename -eq $machine.PTRZone){
        #write-host " Matched PTR Zone" -BackgroundColor Cyan
        $flag=1
        break
        }
  
  }
  #如果PTR Zone不存在,創(chuàng)建一個對應(yīng)的
  if($flag -eq 0){
    write-host " PTRZone is Missing,A new PTRZone will be created" -ForegroundColor Red
    $temp=$machine.IP.Split('.')
    $range=$temp[0]+'.'+$temp[1]+'.'+$temp[2]+".0/24"
    #$range
    Add-DnsServerPrimaryZone -DynamicUpdate Secure -NetworkId $range -ReplicationScope Domain -ComputerName syddc01
  }
  else{
  
    #如果PTR zone存在,判斷是否存在對應(yīng)的PTR記錄
    $hname=Get-DnsServerResourceRecord -ComputerName syddc01 -RRType Ptr -ZoneName $machine.PTRZone | select @{n='name';e={$_.recorddata.ptrdomainname}}
    #$hname
    $temp="*"+$machine.hostname+"*"
    if($hname -like $temp){
        
       Write-Host "Already exist" -ForegroundColor Cyan
    
    }
    else{
        #PTR Zone存在 但是PTR記錄不存在
        Write-Host "Adding PTR record" -ForegroundColor Yellow
        Add-DnsServerResourceRecordPtr -ComputerName syddc01 -ZoneName $machine.PTRZone -Name $machine.IP.Split('.')[3] -AllowUpdateAny -TimeToLive 01:00:00 -AgeRecord -PtrDomainName $machine.hostname 
    }
    }
  
  
  }

  

 執(zhí)行腳本

    

PowerShell 給現(xiàn)有DNS記錄創(chuàng)建PTR反向查詢


結(jié)果


PowerShell 給現(xiàn)有DNS記錄創(chuàng)建PTR反向查詢

   


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI