Batch script to capture the IP of a rotating DNS record

Here is a script I wrote to capture a rotating IP address of a DNS A record. This is usually pretty common with Akamai or Cloudflare DNS servers where an IP for a DNS record will change randomly for security, round robin load balancing, etc. You can usually obtain the rotating IP details from those services.

If you are not able to obtain the rotating DNS IP then the below script is really useful. We had an Akamai DNS record that was changing IPs and it was hosted outside of our control. The firewall didn’t have the option to create a DNS based rule and the rule had to be added by IPs. After running the script for 30 minutes it captured 4 different IPs for a single DNS record, then it ran over night and a total of 7 IPs, then after a week 15 rotating akamai IPs were captured. We were able to add all of them to the firewall and access the service.

Rotating DNS IP batch script

RotatingDNS.bat Download it here

@ECHO OFF
REM Script to identify a rotating DNS record
REM usually akamai DNS A records will rotate IPs
REM The results will be in RotatingDNS_Results.txt
REM The script does a ping to the DNS entry every 5 seconds
REM The script wirtes output to the file only if a new IP is detected (no duplicates in the file keeps it minimal in size)

if not exist ".\RotatingDNS_Results.txt" echo List of Rotating IPs: > ".\RotatingDNS_Results.txt"

set DNS=MyDNSname.com

:100
timeout 5
for /f "tokens=4" %%D in ('ping -n 1 %DNS% ^| findstr statistics') do @set myvar=%%D
REM echo %myvar%
findstr /I /C:%myvar% .\RotatingDNS_Results.txt
IF "%ERRORLEVEL%" == "0" GOTO :100
echo %myvar% >> .\RotatingDNS_Results.txt
goto 100

Output:
10.10.10.1:
10.10.10.2:
10.10.10.3:
10.10.10.4:
10.10.10.5:

Options explained:
for /f “tokens=4” %%D in (‘ping -n 1 %DNS% ^| findstr statistics’) do @set myvar=%%D
::Run ‘ping -n 1 dnsname’ command and extract only one line that contains the word statistics. Read the 4th word on this line %%D which is the IP and store the 4th word in a variable myvar

findstr /I /C:%myvar% .\RotatingDNS_Results.txt
IF “%ERRORLEVEL%” == “0” GOTO :100
echo %myvar% >> .\RotatingDNS_Results.txt
goto 100

::Check if the string in the variable exists in the file
RotatingDNS_Results.txt . If it exists go to beginning of the loop and do nothing. If it does not exist add it and go back to the loop.

Basic version of the above script

Here is a very basic version of the above script. It will append the results to a file every 5 seconds. You will have a lot of entries in the file, to get useful data you need to run power-shell on the output to sort it and get unique IPs. The above version does that automatically.

Run this PowerShell script on the results file to remove duplicates and show only unique values: Get-Content .\RotatingDNS_Results.txt | sort | Get-Unique

@ECHO OFF
:100
 ping -n 1 google.com | findstr statistics >> c:\temp\RotatingDNS_Results.txt
 timeout 5
goto 100 

OUTPUT:
Ping statistics for 10.10.10.94:
Ping statistics for 10.10.10.193:
Ping statistics for 10.10.10.94:
Ping statistics for 10.10.10.94: 
This entry was posted in Networking, Scripting and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *