Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommended way to get hostname in Elixir

Tags:

elixir

in nodejs I run

 var os = require("os")
 os.hostname()

How can I get in elixir?

like image 418
shira Avatar asked Nov 27 '22 20:11

shira


2 Answers

There doesn't seem to be a function in Elixir's standard library for this but Erlang has inet:gethostname/0 which you can call directly:

iex(1)> {:ok, hostname} = :inet.gethostname
{:ok, 'dogbert'}
iex(2)> hostname
'dogbert'
like image 115
Dogbert Avatar answered Dec 19 '22 07:12

Dogbert


Just for sake of completeness you can also do this:

{hostname, exit_status} = System.cmd "hostname", [""]
{"TR01PPL010698\r\n", 0}

This works on Windows, *nix and Mac. As you can see it returns an Elixir binary (as opposed to an Erlang charlist) but you also may need to concern yourself with stripping away line-endings.

like image 39
Onorio Catenacci Avatar answered Dec 19 '22 08:12

Onorio Catenacci