How can I install getent on Snow Leopard?

I have several scripts which require the utility getent. In addition, I've read several handy tips which use getent.

getent is a Networking utility which is widely available on Unix, *BSD and Linux-based systems. This tool is commonly used by Sysadmins and power users to lookup network information (IP addresses, etc). As far as hostnames go, getent is one of the few tools which use both /etc/hosts and DNS.

As Wikipedia says:

getent is a unix command that helps a user get entries in a number of important text files called databases.

The important thing here is that getent will search the same name services in the same order as would most applications on a unix-like system. This makes it invaluable when debugging system or application trouble. Tools like dig and nslookup specifically look at DNS only, while dscl expects you to spell out the name services you want to search.

I would like to use getent for two testcases:

  1. Parse /etc/hosts before querying DNS. For example, if I add testhost.example.org to /etc/hosts, then getent hosts testhost.example.org should use the entry from /etc/hosts before querying DNS.

  2. Query LDAP using getent

MacPorts does not seem to provide a package providing getent.

Does getent exist on Snow Leopard?


Solution 1:

dscacheutil is probably the best way to do this. It doesn't emulate the usual search process, it uses it, so you'll actually get the same results as a standard lookup (including caching, /etc/hosts, computer records in directory services, DNS, mDNS for .local names, etc).

$ dscacheutil -q host -a name www.apple.com
name: e3191.c.akamaiedge.net
alias: www.apple.com www.isg-apple.com.akadns.net www.apple.com.edgekey.net 
ip_address: 69.192.45.15

$ dscacheutil -q host -a name gordons-mbp.local
name: gordons-mbp.local
ipv6_address: fe80:5::e6ce:8fff:fe50:136

name: gordons-mbp.local
ip_address: 192.168.10.249

$ dscacheutil -q host -a ip_address 127.0.0.1
name: localhost
alias: 1.0.0.127.in-addr.arpa 
ip_address: 127.0.0.1

$ dscacheutil -q user -a name gordon
name: gordon
password: ********
uid: 501
gid: 20
dir: /Users/gordon
shell: /bin/bash
gecos: Gordon Davisson

Solution 2:

Have a look at How to emulate Unix’ getent with MacOSX’s dscl. Since you have scripts that make use of getent, you may wish to pursue a script as a wrapper around dscl.

Solution 3:

It's not rocket science to scrounge the getent source from OpenSolaris (or some other OS, I'd try one of the BSDs rather than Linux) and port it; I got that to work mainly by just disabling the stuff that macOS didn't have any equivalent for (netmasks and project - Solaris specific). Or one can write it in perl fairly easily. Someone did a limited one a long time ago, and I did a more complete one; it doesn't support ethers because there's no standard lookup for that from perl, but the rest seems to work ok.

getent.pl