12

I would like a command that will resolve a hostname to an IP address, in the same way that a normal program would resolve the hostname. In other words, it has to take into account mDNS (.local) and /etc/hosts, as well as regular DNS. So that rules out host, dig, and nslookup, since all three of those tools only use regular DNS and won't resolve .local addresses.

On Linux, the getent command does exactly what I want. However, getent does not exist on OS X.

Is there a Mac OS X equivalent of getent? I'm aware that I could write one in a few lines using getaddrinfo, and that's what I'll do if I have to, but I was just wondering if there was already a standard command that could do it.

Thanks!

13

I think dscacheutil is what you're looking for. It supports caching, /etc/hosts, mDNS (for .local).

dscacheutil -q host -a name foo.local

Another option is dns-sd

dns-sd -q foo.local

More information about dnscacheutil.

1
  • What is a clear example. For example localhost? How to use, what is the output? It does not work. – mathtick Jan 21 at 22:25
2

(Building on user422009's answer)

Add this to your ~/.bash_profile:

getent() {
  [ "$1" == "hosts" ] && shift
  for x
  do
    echo $x $(dscacheutil -q host -a name $x | awk '/^ip_address/{print $NF}')
  done
}

Then either open a new terminal or source your bash_profile:

. ~/.bash_profile

And then it'll work:

$ getent hosts www.example.com
www.example.com 93.184.216.34
0

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.