Super tiny and simple: I wrote this to support reverse lookups. E.g.:
iex(1)> Host.reverse_lookup(ip: "172.217.5.206")
{:ok, "lax28s10-in-f14.1e100.net"}
I made this because I’m writing a Phoenix web service which needs to make decisions based on the domain of the visitor. And so it needs the ability to determine the domain name from the request IP address.
Here’s the simple implementation:
def reverse_lookup(ip: ip) do
{output, status} = System.cmd("host", [ip])
case status do
0 ->
%{"domain" => domain} =
Regex.named_captures(~r/domain name pointer (?<domain>.+)\.$/, output)
{:ok, domain}
_ ->
{:error, output}
end
end