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

G'Day Monks,

I have a RedHat Linux machine that has multiple virtual interfaces on one NIC. E.g., "ifconfig eth1" might produce something like this:
eth1      Link encap:Ethernet  HWaddr 00:11:22:33:44:55  
          inet addr:172.16.1.1  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22799 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:3391110 (3.2 Mb)  TX bytes:1560024 (1.4 Mb)
          Interrupt:11 Base address:0xdd80 Memory:feafd000-feafd038 

eth1:1    Link encap:Ethernet  HWaddr 00:11:22:33:44:55  
          inet addr:172.16.1.2  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22799 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:3391110 (3.2 Mb)  TX bytes:1560024 (1.4 Mb)
          Interrupt:11 Base address:0xdd80 Memory:feafd000-feafd038 

eth1:2    Link encap:Ethernet  HWaddr 00:11:22:33:44:55  
          inet addr:172.16.1.3  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22799 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:3391110 (3.2 Mb)  TX bytes:1560024 (1.4 Mb)
          Interrupt:11 Base address:0xdd80 Memory:feafd000-feafd038 

eth1:3    Link encap:Ethernet  HWaddr 00:11:22:33:44:55  
          inet addr:172.16.1.4  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:72857 errors:0 dropped:0 overruns:0 frame:0
          TX packets:22799 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:100 
          RX bytes:3391110 (3.2 Mb)  TX bytes:1560024 (1.4 Mb)
          Interrupt:11 Base address:0xdd80 Memory:feafd000-feafd038 

etc, etc, etc...
I'm working on a script that uses LWP::UserAgent to fetch some stuff from another server. Is there any way I can force a given HTTP request to go out a specific virtual interface? E.g., can I force the source ip address to be 172.16.1.3 for a given request, and then have the next request come from 172.16.1.4? Or is this the sort of thing that needs to be taken care of by the OS rather than the script?
  • Comment on Force HTTP Requests from a Specific Interface

Replies are listed 'Best First'.
•Re: Force HTTP Requests from a Specific Interface
by merlyn (Sage) on Nov 18, 2003 at 13:45 UTC
      Thanks Randal! FYI, I tested it out with a packet sniffer and setting @LWP::Protocol::http::EXTRA_SOCK_OPTS to ("LocalAddr", $yourhostalias) just before making the request does indeed do the trick.