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 
    }
    }
  
  
  }

  

 执行脚本

    

wKiom1g1GgPRRmf1AACazFbSvhY540.png


结果


wKiom1g1GgSCwHkNAABsVrgiP64146.png

   


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
批量添加DNS A记录PTR记录可以通过批处理脚本或者使用PowerShell来实现。 一种方法是使用批处理脚本,可以编写一个文本文件,以扩展名为.bat或者.cmd保存。在文件中,使用"dnscmd"命令加上A记录的参数和对应的值来批量添加A记录。例如: dnscmd /RecordAdd example.com @ A 192.168.1.1 dnscmd /RecordAdd example.com @ A 192.168.1.2 dnscmd /RecordAdd example.com @ A 192.168.1.3 这样,每一行都会添加一个A记录到example.com域名的根节点。 对于PTR记录,可以使用"dnscmd"命令加上PTR记录的参数和对应的值来批量添加PTR记录。例如: dnscmd /RecordAdd 1.168.192.in-addr.arpa 1 PTR host1.example.com. dnscmd /RecordAdd 2.168.192.in-addr.arpa 1 PTR host2.example.com. dnscmd /RecordAdd 3.168.192.in-addr.arpa 1 PTR host3.example.com. 这样,每一行都会添加一个PTR记录到1.168.192.in-addr.arpa、2.168.192.in-addr.arpa和3.168.192.in-addr.arpa域名中的对应子节点。 另一种方法是使用PowerShell来批量添加DNS记录。可以编写一个.ps1文件,然后使用`Add-DnsServerResourceRecordA`和`Add-DnsServerResourceRecordPTR` cmdlet来批量添加A记录PTR记录。例如: Add-DnsServerResourceRecordA -ZoneName example.com -Name host1 -IPv4Address 192.168.1.1 Add-DnsServerResourceRecordA -ZoneName example.com -Name host2 -IPv4Address 192.168.1.2 Add-DnsServerResourceRecordA -ZoneName example.com -Name host3 -IPv4Address 192.168.1.3 Add-DnsServerResourceRecordPTR -ZoneName 1.168.192.in-addr.arpa -IPv4Address 192.168.1.1 -PtrDomainName host1.example.com. Add-DnsServerResourceRecordPTR -ZoneName 2.168.192.in-addr.arpa -IPv4Address 192.168.1.2 -PtrDomainName host2.example.com. Add-DnsServerResourceRecordPTR -ZoneName 3.168.192.in-addr.arpa -IPv4Address 192.168.1.3 -PtrDomainName host3.example.com. 这样,使用PowerShell脚本可以批量添加A记录PTR记录到指定的域名和IP地址。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值