views:

141

answers:

3

I have a machine with Linux CentOS distribution that has more than one internet connection available at the same time.

I'm trying to write some PHP code that will do the following:

  1. Perform an HTTP request to a specific URL "eg. google.com" but through a specific internet connection.
  2. Perform the above for several internet connections at the same time (meaning several processes will be running, each with HTTP requests through a specific connection).

Notice the internet connections are ppp0, ppp1, and ppp2.

Here is the output of "ifconfig":

eth0      Link encap:Ethernet  HWaddr 00:23:7D:3B:04:26
          inet addr:86.111.198.9  Bcast:86.111.198.15  Mask:255.255.255.240
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:33511 errors:0 dropped:0 overruns:0 frame:0
          TX packets:24728 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:3144233 (2.9 MiB)  TX bytes:3930225 (3.7 MiB)
          Interrupt:185 Memory:f8000000-f8012100

eth1      Link encap:Ethernet  HWaddr 00:23:7D:3B:04:1E
          inet addr:192.168.1.64  Bcast:192.168.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:7570 errors:0 dropped:0 overruns:0 frame:0
          TX packets:6730 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:553439 (540.4 KiB)  TX bytes:463989 (453.1 KiB)
          Interrupt:193 Memory:fa000000-fa012100

lo        Link encap:Local Loopback
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:95 errors:0 dropped:0 overruns:0 frame:0
          TX packets:95 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:14908 (14.5 KiB)  TX bytes:14908 (14.5 KiB)

ppp0      Link encap:Point-to-Point Protocol
          inet addr:78.93.176.35  P-t-P:212.93.193.40  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1442  Metric:1
          RX packets:238 errors:0 dropped:0 overruns:0 frame:0
          TX packets:9 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:44486 (43.4 KiB)  TX bytes:294 (294.0 b)

ppp1      Link encap:Point-to-Point Protocol
          inet addr:78.93.70.4  P-t-P:212.93.193.15  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1442  Metric:1
          RX packets:219 errors:0 dropped:0 overruns:0 frame:0
          TX packets:38 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:24237 (23.6 KiB)  TX bytes:2330 (2.2 KiB)

ppp2      Link encap:Point-to-Point Protocol
          inet addr:87.109.229.209  P-t-P:84.235.124.10  Mask:255.255.255.255
          UP POINTOPOINT RUNNING NOARP MULTICAST  MTU:1442  Metric:1
          RX packets:7 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:3
          RX bytes:294 (294.0 b)  TX bytes:54 (54.0 b)

Here is the output of the route command:

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
212.93.193.15   *               255.255.255.255 UH    0      0        0 ppp1
212.93.193.40   *               255.255.255.255 UH    0      0        0 ppp0
84.235.124.10   *               255.255.255.255 UH    0      0        0 ppp2
86.111.198.0    *               255.255.255.240 U     0      0        0 eth0
192.168.1.0     *               255.255.255.0   U     0      0        0 eth1
169.254.0.0     *               255.255.0.0     U     0      0        0 eth1

Anyone with any ideas how this can work?

A: 

You might have to route the connection through curl/wget. These command line utilities give you more explicit control over which interface to use. Just trigger the command from your PHP script and capture the output. As far as I can tell, there's no way to specify an interface via something like fopen/file_get_contents with the http:// stream handler. Even Snoopy doesn't have a way to specify the interface.

Nolte Burke
+1  A: 

You can do this in PHP with curl_multi_*() and curl_setopt(CURLOPT_INTERFACE, 'ppp0'), etc.

As far as actually discovering interfaces goes, I just ran a simple regex over the output of ifconfig. Certainly not a great way to do things, but you generally only need to do it once, at script startup, so it's not exactly a huge performance issue.

Frank Farmer
A: 

Thanks alot! Thanks alot!