getaddrinfo() from shell?
When you call
ping name.domain, it goes through both
/etc/hosts and the DNS resolver to obtain an IP. It could be an IP hard-coded in
/etc/hosts, or it could be one from the DNS server. It does so by calling getaddrinfo() or equivalent, not directly, of course.
How do I call
getaddrinfo() from shell? How do I reproduce the effect of "normal" net utilities to obtain an IP from an address?
This is not about using
host which only go through DNS, or
getent which only goes through
hosts. I want to reproduce common application behavior (e.g.
ping) when it receives a name it needs to resolve. There are other questions about
host. This question is not a duplicate of those.
Update: here are my findings (based partly on answers to other Qs)
- on Ubuntu (and Debian?) there is
gethostip -d name.domainfrom
perl -MSocket -le 'print inet_ntoa inet_aton shift' name.domainworks reliably and is terser than the accepted answer.
getentmay also work:
getent ahostsv4 name.domain | grep STREAM | head -1 | cut -f1 -d' '
This seems to be the best one can do.