http://www.perlmonks.org?node_id=127731


in reply to Getting net addy on localhost?

The reason that this code is returning 127.0.0.1 is that you are requesting the resolution of the name localhost, which should always be your loopback adapter address - If you want it to resolve your external IP address, you should pass the external hostname you have been assigned. If this is dynamic in nature then you may want to look at other solutions such as Net::Pcap which allow network interfaces to be iterated.

Update #1 - I did a bit of digging on a hunch and if you have a known external address that you can connect to, you could make a socket connection to it and then call perlfunc:getsockname on the socket handle (or sockname if you use IO::Socket) to return the packed address of this end of the socket. eg.

use Socket; $mysockaddr = getsockname(SOCK); ($port, $myaddr) = sockaddr_in($mysockaddr); printf "Connected from %s [%s]\n", scalar gethostbyaddr($myaddr, AF_INET), inet_ntoa($myaddr);

Update #2 - And the prize goes to converter for the link to IO::Interface in this post

 

Ooohhh, Rob no beer function well without!