I am in the shell and I want to know the corresponding IP address(es) for a hostname.
I know I could get it through Perl, PHP, Python or a number of other scripting languages (probably even awk!), and this will be my workaround, but I am surprised that there seems to be no command-line tool to do this, no simple wrapper around getaddrinfo().
Am I wrong? Is there one? host and getent do not count, I want something that uses the libc and acts according to /etc/nsswitch.conf, and something that is probably installed on any (linux) system by default.
Answer from https://superuser.com/questions/681612/is-there-a-standard-command-line-tool-to-do-a-hostname-lookup : getent hosts does actually do exactly what I want, not just an /etc/hosts lookup.
One way is to leverage arp which follows nsswitch.conf settings, only uses libc and is available on any Linux/Unix system, eg:
$ arp stackoverflow.com | sed -e 's/.*(//' -e 's/).*$//'
198.252.206.16
Here is a shell function using this method that also workaround the issue reported by clacke:
resolve()
{
: ${1:?Usage: resolve name}
(
PATH=$PATH:/usr/sbin
ao=$(arp $1)
[ "$(echo "$ao"|grep HWaddress)" ] && ao=$(arp -a $1)
echo "$ao" | sed -e 's/.*(//' -e 's/).*$//'
)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With