1

I'm writing some bash on a linux server, and there's one concept I'm trying to get my head around.

Given a domain name, can I tell if it's pointing to the current server? Bearing in mind that the server may have multiple ip addresses.

My current plan is to loop all ip addresses from the 'ip addr' command, and see if the 'host example.com' returns one of them.

Any better ideas?

3

You don't need to loop through the ip addresses you can just use ip addr which lists all the configured addresses for the system and grep for the address that host example.com returns, so

ip addr | grep -q $(host -t A example.com | awk '{print $NF}')

would return 0 in $? if the ip address returned by host is matched. You also need to check for IPv6 addresses so as above

ip addr | grep -q $(host -t AAAA example.com | awk '{print $NF}'

will return 0 to $? if the IPv6 address for example.com is matched.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.