[erlang-questions] reliably figure out hostname

Tim Watson <>
Thu Aug 2 18:33:42 CEST 2012


On 2 August 2012 15:31, Raimo Niskanen
<> wrote:
> On Thu, Aug 02, 2012 at 09:40:03AM +0100, Gustav Simonsson wrote:
>>
>> {ok, LocalHostName} = inet:gethostname().
>> inet_res:gethostbyname(LocalHostName).
>
> That forces using the OTP DNS resolver, which may be in conflict
> with the current node configuration. The first approach should be:
>
>     {ok, LocalHostName} = inet:gethostname().
>     inet:gethostbyname(LocalHostName).
>

That's clearly not what net_kernel is doing though:

==============================================

:x-test $ erl -name foobar
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4]
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
()1>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
:x-test $ erl -name 
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4]
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
()1>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
:x-test $ erl
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4]
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
1> {ok, Host} = inet:gethostname().
{ok,"frigg"}
2> inet:gethostbyname(Host).
{ok,{hostent,"frigg",[],inet,4,[{127,0,0,1}]}}
3>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
:x-test $

==============================================

And the problem I've got is that whilst on this machine (CentOS 6.2,
R15B01-64bit) the network configuration is *somehow* set up so that
net_kernal magically picks up the right domain info, this doesn't work
consistently everywhere else. And neither does calling inet_rc, as
Gustav suggested:

==============================================
:x-test $ erl
Erlang R15B01 (erts-5.9.1) [source] [64-bit] [smp:4:4]
[async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.9.1  (abort with ^G)
1> {ok, LocalHostName} = inet:gethostname().
{ok,"frigg"}
2> inet_res:gethostbyname(LocalHostName).
{error,nxdomain}
3>

=============================================

On some other machines, inet_res:gethostbyname/1 returns {error, timeout}.

> / Raimo Niskanen
>
>>
>> You get a hostent record back which has the FQDN.
>>
>> // Gustav Simonsson
>>
>> Sent from my PC
>>
>> ----- Original Message -----
>> > From: "Tim Watson" <>
>> > To: "Tim Watson" <>
>> > Cc: "erlang-questions Questions" <>
>> > Sent: Thursday, 2 August, 2012 10:17:32 AM
>> > Subject: Re: [erlang-questions] reliably figure out hostname
>> >
>> >
>> >
>> > Any takers for this? I can't be the only person who's had to figure
>> > this out. The context is a distributed systems testing framework
>> > that needs to support resolving 'localhost' to a proper host name so
>> > the framework can become a hidden node and interact with the other
>> > erlang nodes It is testing against. Having to specify the real host
>> > name would make configuring the tool across different machines and
>> > development environments unpleasantly complicated so figuring out
>> > the right hostname is a boon in terms of keeping the configuration
>> > overhead down, but doing this for long names nodes is proving highly
>> > awkward.
>> >
>> >
>> > Can anyone suggest a good portable solution?
>> >
>> > On 31 Jul 2012, at 13:43, Tim Watson <  >
>> > wrote:
>> >
>> >
>> >
>> >
>> >
>> > Is there a way to calculate the hostname reliably across platforms in
>> > Erlang? I have a non-distributed node that I wish to become a
>> > distributed node. Normally I call net_kernel:start([Name,
>> > shortnames]) and this is just fine. It also works with [Name,
>> > longnames] *sometimes and on some systems* - but other times it
>> > pukes. I've tried looking in the 'domain' or 'search' entries from
>> > inet:get_rc/0 but these are not always populated, even when dns
>> > config is clearly in place. I've also tried using
>> > 'inet_db:get_searchlist/0' but again, sometimes this returns [[]]
>> > but net_kernel:start([foobar, longnames]) doesn't work, whereas
>> > doing net_kernel:start([  , longnames]) does.
>> >
>> >
>> > Am I missing something incredibly obvious here? *is* there actually a
>> > simple way of determining what the proper fqdn for the machine
>> > should be, without breaking out to the os? I had even considered
>> > doing inet:gethostbyname/1 but again, the search domains entry seems
>> > to be empty, so I'd assume that -name foobar will work whereas in
>> > fact, -name foobar @fqdn is required otherwise net_kernel won't
>> > start.
>> > _______________________________________________
>> > erlang-questions mailing list
>> > 
>> > http://erlang.org/mailman/listinfo/erlang-questions
>> >
>> _______________________________________________
>> erlang-questions mailing list
>> 
>> http://erlang.org/mailman/listinfo/erlang-questions
>
> --
>
> / Raimo Niskanen, Erlang/OTP, Ericsson AB
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions



More information about the erlang-questions mailing list