ホームページ  >  記事  >  php教程  >  PowerShell は既存の DNS レコードに対する PTR 逆クエリを作成します

PowerShell は既存の DNS レコードに対する PTR 逆クエリを作成します

高洛峰
高洛峰オリジナル
2016-11-23 17:25:211435ブラウズ

今朝、Douzi は会社の DNS サーバーには前方解決のみがあり、対応する PTR レコードがないことを偶然発見しました。つまり、IP アドレスはドメイン名を通じて解決できますが、逆にすると IP アドレスからドメイン名を見つけることができません。

既存のレコードに対応する逆引きゾーンと PTR レコードがあるかどうかを判断するための非常に簡単なスクリプトを 1 時間で作成しました。ない場合は、それらを自動的に作成します。

アイデアは非常にシンプルで、フォールトトレランス処理や最適化は比較的大まかなスクリプトですが、機能を実装するのは良いことです。

$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 までご連絡ください。