2

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?

1 Answer 1

4

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .