Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the IP address of an erlang node?

Tags:

erlang

Is there a simple way to get the IP address of a connected erlang node? I'd like to initiate a SCTP connection with a few nodes and due to the way the system is engineered, the knowledge I have about them is just their node() atom.

More precisely, I am wondering if there is some API provided by Erlang (or some derived technique) that allows to obtain a node's IP address given its identifier atom().

like image 868
matehat Avatar asked Aug 31 '12 16:08

matehat


3 Answers

You can use the rpc module to call the function on a foreign node

example:

rpc:call(Node,inet,getif,[])

note: this only works on nodes that are already connected via erlang distribution

like image 114
David Budworth Avatar answered Sep 17 '22 03:09

David Budworth


I solved this problem by starting a process on the node and having the process send a message containing its IP addresses. If anyone knows of a more elegant solution, I would like to hear it.

The command I used to get the address after the process on the node had been started was: inet:getif(). Keep in mind that the result of that command includes the loop-back address.

Something to consider is that each node may have multiple IP addresses and the SCTP server may not be listening on all of them.

The other idea I thought about trying was to convert the atom returned from node() into a string, parse the string to get the hostname, and perform a DNS lookup. It might work, but I have never tried it. The result of the DNS lookup should be cached, so there might not be a network round trip. Also, I really hate assuming anything about the atom return from node().

like image 35
mjcopple Avatar answered Sep 18 '22 03:09

mjcopple


It looks like net_kernel:nodes_info() - for all nodes - and net_kernel:node_info(Node) for a single node - have this information, and more, although it does not look like it's published in the man page. This seems like a better solution in some ways because it will also work with things like Java and C nodes that you can't send functions to.

like image 28
David N. Welton Avatar answered Sep 21 '22 03:09

David N. Welton