Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Answer: How do I get the local internet IP address?

by tirwhan (Abbot)
on Jan 07, 2006 at 19:38 UTC ( #521748=categorized answer: print w/replies, xml ) Need Help??

Q&A > network programming > How do I get the local internet IP address? - Answer contributed by tirwhan

This question is usually asked to find out which IP address the local host uses to connect to a remote one. One important thing to realise in this context is that there is no such thing as a host's IP address. Network interfaces have IP addresses, not hosts, and a single network interface can have many (virtual) IP addresses. The operating system's routing subsystem decides which network interface and IP address to use to connect to a remote machine. If your machine only has one external network interface, and this interface only has one IP address then this IP address is commonly called the machine's address, but that is inaccurate. For example, if the machine is connected to a VPN via a virtual interface it will use this interface's IP address to connect to another machine on the VPN, not the external IP address.

Usually the best fail-safe way to find the "local Internet IP address" is to actually establish a network connection to the remote host and then find out which IP address is being used on the local side. For example:

use IO::Socket::INET; my $sock = IO::Socket::INET->new( PeerAddr=> "", PeerPort=> 80, Proto => "tcp"); my $localip = $sock->sockhost;

Replies are listed 'Best First'.
Re: Answer: How do I get the local internet IP address?
by ysth (Canon) on Jan 08, 2006 at 03:29 UTC
Log In?

What's my password?
Create A New User
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2017-12-17 15:32 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (464 votes). Check out past polls.