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


in reply to How do I get the local internet IP address?

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=> "example.com", PeerPort=> 80, Proto => "tcp"); my $localip = $sock->sockhost;