我想要轮换用于网页抓取的 IP 地址,以下是我的设置:
/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
Cloudflare DNS 对我的服务器的不同 IP 地址执行循环。
但是,当我使用以下 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 地址?
答案1
弄清楚了这一点,解决方案是配置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