Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

I have absolutely no idea how to select which interface to connect through.

Here's the code I use to open a socket:

my $sock = new IO::Socket::INET(PeerAddr    => $server,
                                PeerPort    => $serverPort,
                                Proto       => 'tcp') or
                             die "Can't connect to server: $!";
like image 691
Dieterve Avatar asked Mar 12 '10 12:03

Dieterve


1 Answers

You have to give IO::Socket::INET the address of the interface you want to use as LocalAddr parameter. Imagine that 10.0.0.1 is the IP address of eth0 and 10.0.0.2 the IP address of eth0:0, then it would work like this.

my $sock = new IO::Socket::INET(PeerAddr    => $server,
    PeerPort    => $serverPort,
    Proto       => 'tcp'
    LocalAddr   => '10.0.0.2') or
  die "Can't connect to server: $!";
like image 145
Leon Timmermans Avatar answered Nov 05 '22 06:11

Leon Timmermans