Mike
Mike

Reputation: 1273

How to obtain fully qualified domain name (or just domain name) in Erlang/Elixir?

Erlang has inet.gethostname to obtain the hostname:

iex(1)> {:ok, hostname} = :inet.gethostname
{:ok, 'Michaels-MacBook-Pro'}

But how do you obtain the domain name?

Upvotes: 1

Views: 734

Answers (2)

:net_adm.dns_hostname(:net_adm.localhost)

seems to do what you want.

Upvotes: 7

Mike
Mike

Reputation: 1273

It isn't the most elegant solution, but this is the only thing I've found that works so far:

def fqdn do
  {fqdn, _exit_status} = System.cmd("hostname", ["-f"])
  String.trim(fqdn)
end

Upvotes: 1

Related Questions