Skip to content

Instantly share code, notes, and snippets.

@chrisdavidmiles
Last active March 26, 2024 22:55
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save chrisdavidmiles/5aa1b3c7194bde4c7aa34895c1e497d6 to your computer and use it in GitHub Desktop.
Save chrisdavidmiles/5aa1b3c7194bde4c7aa34895c1e497d6 to your computer and use it in GitHub Desktop.
Bulk DNS Lookup bash script
#!/bin/bash
# Bulk DNS Lookup
# Generates a CSV of DNS lookups from a list of domains.
#
# File name/path of domain list:
domain_list='domains.txt' # One FQDN per line in file.
#
# IP address of the nameserver used for lookups:
ns_ip='1.1.1.1' # Is using Cloudflare's 1.1.1.1.
#
# Seconds to wait between lookups:
loop_wait='1' # Is set to 1 second.
echo 'Domain name, IP Address, IP PTR, IP NetName (WHOIS)' # Start CSV
for domain in $(cat $domain_list) # Start looping through domains
do
ip=$(dig @$ns_ip +short $domain | tail -n1) # IP address lookup
if [ -z "$ip" ] # If the IP is null (expired or invalid domain)
then # Then
echo "$domain,No DNS,," # Write "No DNS" in the IP column
else # And if an IP is found perform a PTR and NetName lookup
echo -en "$domain,$ip,"$(dig @$ns_ip +short -x $ip | xargs)','
whois $ip | grep -i 'netname' | awk '{print $NF}' | xargs
fi
sleep $loop_wait # Pause before the next lookup to avoid flooding NS
done
@realaaa
Copy link

realaaa commented Mar 5, 2024

thanks for that !! if anyone is doing it on Windows make sure to get rid of /r at the end of lines in the input first, if you're wondering why nothing works as expected :)

https://askubuntu.com/questions/1148263/r-added-end-of-the-script-command#:~:text=The%20%5Cr%20issue%20suggests%20you,that%20will%20break%20your%20scripts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment