I have big file that holds something like 40000 rows of domain names.
I would like to read that file and use dig
(or something else) to look up the IP addresses of the domain names in the DNS, and print them out to another file.
How do I do this?
EDIT: Been testing this with some of the proposed solutions. With this for most the part:
#!/bin/bash
> ips.txt
cat test.txt | while read host; do
ip=$(getent hosts "$host")
if [ $? -ne 0 ]; then
echo "Host $host was not resolved.";
continue
fi
ip=$(echo "$ip" | awk '{ print $1 }')
echo "Host: $host, IP: $ip" >> ips.txt
done
This produces a file that is empty. Not sure why this is not working.
I tried another solution:
for host in 0.accountkit.com 0.bigclicker.me 0.cdn.ddstatic.com 0.facebook.com 0.fls.doubleclick.net 0.hiveon.net 0.mining.garden
do
# get ips to host using dig
ips=($(dig "$host" a +short | grep '^[.0-9]*$'))
for ip in "${ips[@]}";
do
printf 'allow\t\t%s\n' "$ip"
done
done > allowedip.txt
This will print the ip-addresses but problem is that I need to read the DNS names from the file and not in the script itself.
for i in $(cat dns_file); do dig $i >> new_dns_file; done
?