今天早上豆子无意中发现公司的DNS服务器上面只有正向的解析,而没有对应的PTR记录。换句话说,可以通过域名来解析IP地址,但是倒过来IP地址是找不着域名的。


1个小时写了个很简单的脚本,判断已有的记录是否存在对应的reverse zone 和PTR记录,如果没有的话,自动给我创建加上。


思路很简单,脚本也比较糙,没有任何容错处理和优化,不过实现功能就好。

$ptrzones=Get-DnsServerzone-ComputerNamesyddc01|Where-Object{$_.zonename-like"*.arpa"}#获取所以的A记录$machines=Get-DnsServerResourceRecord-ComputerNamesyddc01-RRTypeA-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($machinein$machines){#判断是否存在PTR的reversezonewrite-host$machine.hostnamewrite-host$machine.PTRZone$flag=0foreach($pin$ptrzones){if($p.zonename-eq$machine.PTRZone){#write-host"MatchedPTRZone"-BackgroundColorCyan$flag=1break}}#如果PTRZone不存在,创建一个对应的if($flag-eq0){write-host"PTRZoneisMissing,AnewPTRZonewillbecreated"-ForegroundColorRed$temp=$machine.IP.Split('.')$range=$temp[0]+'.'+$temp[1]+'.'+$temp[2]+".0/24"#$rangeAdd-DnsServerPrimaryZone-DynamicUpdateSecure-NetworkId$range-ReplicationScopeDomain-ComputerNamesyddc01}else{#如果PTRzone存在,判断是否存在对应的PTR记录$hname=Get-DnsServerResourceRecord-ComputerNamesyddc01-RRTypePtr-ZoneName$machine.PTRZone|select@{n='name';e={$_.recorddata.ptrdomainname}}#$hname$temp="*"+$machine.hostname+"*"if($hname-like$temp){Write-Host"Alreadyexist"-ForegroundColorCyan}else{#PTRZone存在但是PTR记录不存在Write-Host"AddingPTRrecord"-ForegroundColorYellowAdd-DnsServerResourceRecordPtr-ComputerNamesyddc01-ZoneName$machine.PTRZone-Name$machine.IP.Split('.')[3]-AllowUpdateAny-TimeToLive01:00:00-AgeRecord-PtrDomainName$machine.hostname}}}

执行脚本


结果