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.
$mysockaddr = getsockname(SOCK);
($port, $myaddr) = sockaddr_in($mysockaddr);
printf "Connected from %s [%s]\n",
scalar gethostbyaddr($myaddr, AF_INET),
Update #2 - And the prize goes to converter for the link to IO::Interface in this post
Ooohhh, Rob no beer function well without!
Are you posting in the right place? Check out Where do I post X? to know for sure.
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
Want more info? How to link or
or How to display code and escape characters
are good places to start.