Networking

輪換傳出 IP

  • December 21, 2014

我想為網路抓取輪換 IP 地址,這是我的設置:

  1. 我在我的/etc/network/interfaces文件中配置了多個 IP 地址,如下所示:
# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
allow-hotplug eth0
iface eth0 inet static
   address XX.XXX.XXX.146
   netmask 255.255.255.248
   network XX.XXX.XXX.144
   broadcast XX.XXX.XXX.151
   gateway XX.XXX.XXX.145
   # dns-* options are implemented by the resolvconf package, if installed
   dns-nameservers 208.67.222.222 208.67.220.220
   dns-search host.myhost.com
auto eth0:1
iface eth0:1 inet static
address XX.XXX.XXX.147
netmask 255.255.255.248
broadcast XX.XXX.XXX.151 
network XX.XXX.XXX.144 

auto eth0:2
iface eth0:2 inet static
address XX.XXX.XXX.148
netmask 255.255.255.248
broadcast XX.XXX.XXX.151 
network XX.XXX.XXX.144 

auto eth0:3
iface eth0:3 inet static
address XX.XXX.XXX.149
netmask 255.255.255.248
broadcast XX.XXX.XXX.151 
network XX.XXX.XXX.144 
  1. Cloudflare DNS 對我伺服器的不同 IP 地址執行循環。
  2. 但是,當我使用以下 PHP 腳本檢查我的外部 IP 地址時,我得到了不同的值,$_SERVER['SERVER_ADDR'];但我的外部 IP 地址保持不變(由下面的腳本檢查http://checkip.dyndns.com/)。
<?php
$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: ([\[\]:.[0-9a-fA-F]+)</', $externalContent, $m);
$externalIp = $m[1];
echo $externalIp;
echo '<br/>';
echo $_SERVER['SERVER_ADDR'];
?>

我在這裡缺少什麼,我想執行一個外部執行檔,它應該輪流使用我的伺服器上可用的不同公共 IP 地址?

想通了,解決方案是配置iptables規則如下:

iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 80 -o eth0 -m statistic --mode nth --every 1 --packet 0 -j SNAT --to-source XX.XXX.XXX.146 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 80 -o eth0 -m statistic --mode nth --every 2 --packet 0 -j SNAT --to-source XX.XXX.XXX.147 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 80 -o eth0 -m statistic --mode nth --every 3 --packet 0 -j SNAT --to-source XX.XXX.XXX.148 iptables -t nat -I POSTROUTING -m state --state NEW -p tcp --dport 80 -o eth0 -m statistic --mode nth --every 4 --packet 0 -j SNAT --to-source XX.XXX.XXX.149

引用自:https://serverfault.com/questions/654003