Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Getting net addy on localhost?

by rob_au (Abbot)
on Nov 27, 2001 at 09:40 UTC ( [id://127731]=note: print w/replies, xml ) Need Help??


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!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://127731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found