sunshine_august has asked for the wisdom of the Perl Monks concerning the following question:

hi, all monks:

My host has two ethernet cards, eth0 and eth1, and I want to get some URL by LWP via eth1, but I can't find LWP has the similar parameter, like --interface to curl, which can be used to specify the outlet interface.

Is there any chance to specify the interface for LWP?

Thanks advance.

  • Comment on How to specify outlet interface for LWP?

Replies are listed 'Best First'.
Re: How to specify outlet interface for LWP?
by Anonymous Monk on May 22, 2009 at 08:33 UTC
      Doesn't work... It seems that there is no way to specify an interface for LWP. The only way works for me seems to add a proper rule into the route table, but doing this needs root privileges.
        Doesn't work... It seems that there is no way to specify an interface for LWP

        ?? bullpucky

        #!/usr/bin/perl -- use strict; use warnings; use LWP 5.826; use LWP::Protocol::http; #perldoc -q "How do I find out my hostname/domainname/IP address?" use Socket; use Sys::Hostname; my $host = hostname(); my $addr = inet_ntoa(scalar gethostbyname($host || 'localhost')); my $ua = LWP::UserAgent->new(); { print "# works like default\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => $addr ) +; print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# bad hostname\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "nohost +:666" ); print $ua->get("http://www.example.com")->status_line,"\n"; } { print "# unknown error\n"; local @LWP::Protocol::http::EXTRA_SOCK_OPTS = ( LocalAddr => "localh +ost" ); print $ua->get("http://www.example.com")->status_line,"\n"; printf "$^E\n"; } __END__ # works like default 200 OK # bad hostname 500 Can't connect to www.example.com:80 (Bad hostname 'nohost:666') # unknown error 500 Can't connect to www.example.com:80 (connect: Unknown error) A connection attempt failed because the connected party did not proper +ly respond after a period of time, or established connection failed because connected host has failed to respond
Re: How to specify outlet interface for LWP?
by lakshmananindia (Chaplain) on May 22, 2009 at 08:34 UTC

    Not a perl solution

    You can configure your routing table to outlet via the eth1 inteface. I did this for my own purpose in *nix machine. You can also refer "route" command in unix

    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: How to specify outlet interface for LWP?
by dmgo (Initiate) on Aug 07, 2011 at 01:30 UTC
    Hi, It's possible with LWP::UserAgent::local_address function. Regards, Dmytro Gorbunov