How to list all domains and their IPs

marson

Verified User
Joined
Jan 30, 2012
Messages
60
Hello

I would like to create a script that saves all domains and their IPs to the file in the format like this:

1.2.3.4 domain1.com
1.2.3.4 domain2.com
2.3.4.1 domain3.com
1.2.3.4 www.domain1.com
2.3.4.1 www.domain3.com

where 1.2.3.4 and 2.3.4.1 are the IPs associated to the server. My goal is to create list of all A record with domain, and www.domain as the name and save it on some file like hosts.txt. this is because I want to have an easy way to copy and paste these ips onto local hosts file to test if sites works after migration, however my bash skills are limited and I don't know where to start

I think that the best option would be to list all *.db files in /var/named, cut the .db extension and save the result as variable like $domain and then loop in each .db file to find $domain and get the ip from A field then pipng that on the separate file as $IP $domain and lastly do the same again this time adding www. at the beginning of $domain. Can someone point me where to start or maybe someone will want to make that for me - for fee ofcourse. If I can hire someone to do that for me it will be nice.
 
This wont work the point of all of this is to create file to future use as a hosts entries, to test the site (main donai + www.domain) on a local computer without changing DNS server - I already coded very very trivial version of this:

Code:
#!/bin/sh
ip=$(ip route get 8.8.8.8 | awk -F"src " 'NR==1{split($2,a," ");print a[1]}')
cat /etc/virtual/domains >domeny.txt
cat /etc/virtual/domains >domeny2.txt
sed -i -e 's/^/www./' domeny2.txt
cat domeny2.txt >>domeny.txt
sed -i -e 's/^/     /' domeny.txt
awk -v ip="$ip" '{print ip $0}' domeny.txt >hosts.txt
rm -f domeny*

However this is sooo stupid that I am affraid to post it here ;) and more importantly yhis assumes that all domaims point to the same main IP of the server wchich is not always the case.

Yes - sure - you can laugh ;)
 
Hi,

How about this:
for i in `cat /etc/virtual/domainowners | cut -d ":" -f1`; do echo "`dig $i a +short` $i"; done

I tried this on my server and the output is as you wish:
1.2.3.4 domain1.com
1.2.3.4 domain2.com
2.3.4.1 domain3.com

The file "/etc/virtual/domainowners" contains all your domains and domain owners, and the "dig" command fetches the IP of the domain.
Though in my case, I had one domain that was not pointed, so no IP address was printed for that domain.
 
Thanks for the reply but I think that this will not work, I try to explain that again

All the point of this script is that when we migrate client from one server to another we must check if client's sites works, so to do that we have 2 options one is to change the DNS records to point to the new server and other is to check the site via hosts file, and in most cases the IP where the site point doesn't math the IP of the server because site is migrating but DNS are not switched yet, so I want to find a way to get the domain name and the IP that is assigned to that domain locally, So that's why I thought about local zone files in the first place, the dig command will query an external server to to give the result so this is not an option in such case.
 
I see. Sorry for misunderstanding. Well, in this case, I think you could still try using "dig" to query your specific dns server (the one where you set your new A record for the domain), for example "dig @your_dns_server_ip domain.tld a +short". I mean, if you are able to find the new ip in the zone file of your dns server, dig should be able to query it. But if you only have access to the local dns zone files, then probably you will have to make a script that parses the dns zone and takes the new ip from the zone.
 
Perhaps -
Code:
cut -d ':' -f1 /etc/virtual/domainowners | while read sLine; do host "$sLine" localhost | grep 'has address' | awk '{print $4 " " $1 " www."$1}' ; done
 
Last edited:
Maybe alternative to checking servers DNS, is to read the config files directly to see what DA should be setting the domain/ip to for whatever web server is used. As we cannot rely on the DNS or mail as either of those can be turned off to use external services. This doesn't take into account aliases/subdomains, just does the main domain, I was keeping it short. But it does output in your requested format. Would have to do a separate one for the aliases. I tested it on a few servers and it works fine.

Code:
cat /usr/local/directadmin/data/users/*/domains/*.conf | grep '^domain=\|^ip=' | cut -f2- -d= | awk '{key=$0; getline; print key " " $0;}' | awk '{print $2,$1}'
 
Maybe alternative to checking servers DNS, is to read the config files directly to see what DA should be setting the domain/ip to for whatever web server is used. As we cannot rely on the DNS or mail as either of those can be turned off to use external services. This doesn't take into account aliases/subdomains, just does the main domain, I was keeping it short. But it does output in your requested format. Would have to do a separate one for the aliases. I tested it on a few servers and it works fine.

Code:
cat /usr/local/directadmin/data/users/*/domains/*.conf | grep '^domain=\|^ip=' | cut -f2- -d= | awk '{key=$0; getline; print key " " $0;}' | awk '{print $2,$1}'

It is nice however I think that getting an IP from a zone file will be more accurate, for example I have one domain on my server wchich is set in zone file manually to an external IP so in your solution this field is not properly recognized but of course this was special case, and thank you very much for help as a side note I think that in DirectAdmin should be a preview button to see the site without need to change DNS with some kind of internal redirects, like in plesk it was very helpful.
 
Back
Top