How can I tell Perl IO :: Socket :: INET which interface to use?

I have two interfaces on my server: eth0 and eth0: 0. These are two different external IP addresses and, obviously, two different reverse domains.

When I open the IO :: Socket :: INET connection, Perl uses the eth0 interface by default. I would like to use the second interface (eth0: 0) because it has a different IP address and I do not want to use my primary IP address or domain.

I do not know how to choose an interface to connect.

Here is the code I use to open the socket:

my $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => $serverPort, Proto => 'tcp') or die "Can't connect to server: $!"; 
+6
interface perl sockets
source share
1 answer

You must specify the IO :: Socket :: INET address of the interface that you want to use as the LocalAddr parameter. Imagine that 10.0.0.1 is the IP address of eth0 and 10.0.0.2 IP address of eth0: 0, then it will work as follows.

 my $sock = new IO::Socket::INET(PeerAddr => $server, PeerPort => $serverPort, Proto => 'tcp' LocalAddr => '10.0.0.2') or die "Can't connect to server: $!"; 
+9
source share

All Articles