matehat
matehat

Reputation: 5374

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

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().

Upvotes: 7

Views: 3028

Answers (3)

David N. Welton
David N. Welton

Reputation: 1865

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.

Upvotes: 2

David Budworth
David Budworth

Reputation: 11626

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

Upvotes: 5

mjcopple
mjcopple

Reputation: 1600

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().

Upvotes: 4

Related Questions