PowerShell给现有DNS记录创建PTR反向查询-创新互联

今天早上豆子无意中发现公司的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反向查询

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:PowerShell给现有DNS记录创建PTR反向查询-创新互联
网站URL:http://myzitong.com/article/ddgeoo.html