首頁  >  文章  >  php教程  >  PowerShell 為現有DNS記錄建立PTR反向查詢

PowerShell 為現有DNS記錄建立PTR反向查詢

高洛峰
高洛峰原創
2016-11-23 17:25:211435瀏覽

今天早上豆子無意中發現公司的DNS伺服器上面只有正向的解析,而沒有對應的PTR記錄。換句話說,可以透過網域名稱來解析IP位址,但倒過來IP位址是找不著網域的。

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

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

$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不存在,创建一个对应的
  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存在,判断是否存在对应的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 
    }
    }
   
   
  }

執行腳本

PowerShell 為現有DNS記錄建立PTR反向查詢

結果

PowerShell 為現有DNS記錄建立PTR反向查詢

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn