Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir - Get Host By Name?

Tags:

elixir

How do you gethostbyname with Elixir?

There doesn't seem to be a supported API and the two solutions seem to revolve around,

  1. Erlang's Inet
  2. Fork to shell with System (hostname)
like image 993
Dane Balia Avatar asked Jan 09 '23 08:01

Dane Balia


1 Answers

The general philosophy in Elixir is that if a solution exists in standard erlang libraries, there is no reason to simply reproduce that solution with a elixir wrapper unless you are going to provide added functionality in some way.

Or in other words, erlang libraries are native.

iex(2)> :inet.gethostbyname('www.google.com')
{:ok, {:hostent, 'www.google.com', [], :inet, 4, [{216, 58, 192, 4}]}}

Note: the single quotes above are important, you can convert an Elixir string to an Erlang one by using String.to_charlist

iex(5)> :inet.gethostbyname(String.to_char_list("www.google.com"))
{:ok, {:hostent, 'www.google.com', [], :inet, 4, [{216, 58, 192, 4}]}}
like image 198
Fred the Magic Wonder Dog Avatar answered Jan 10 '23 20:01

Fred the Magic Wonder Dog