DNS and Reverse DNS Lookup

Author: Carl Sassenrath
Return to REBOL Cookbook

Here is a simple method for being able to quickly get the IP address of a machine within a domain using DNS (the domain name service). Just type this line from the REBOL console or put it within your script:

    ip: read dns://www.rebol.net

The result will be the IP address (as a tuple) for that machine in the domain. If the name cannot be resolved, a NONE is returned.

There is a shortcut for getting the IP address of the local machine:

    ip: read dns://

However, note that the local machine may have more than one network interface (such as an ethernet card and a wifi card), so it may have more than one IP address (but this function only returns one IP).

You can also do a reverse DNS lookup using the same method but by providing the IP address rather than a name. The IP must be a tuple value, as in this example:

    name: read dns://

If the reverse mapping cannot be found, a NONE is returned.

To get the name for the IP address you requested at the top of this document, use the JOIN function:

    name: read join dns:// ip

Note that the name may not be exactly the same as provided earlier (in this case it may be "rebol.net" not "www.rebol.net"). That's because is possible to map many names to a single IP. The name that is actually returned will depend on the DNS server configuration for the requested domain.

2006 REBOL Technologies REBOL.com REBOL.net