linux – 使用iptables轮换传出IP

前端之家收集整理的这篇文章主要介绍了linux – 使用iptables轮换传出IP前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用iptables轮换传出的IP.我想逐个旋转三个IP之间的传出连接.这是我正在用iptables做的事情:
root@server:~# 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 XXX.XXX.XXX.133
root@server:~# 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 XXX.XXX.XXX.134
root@server:~# 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 XXX.XXX.XXX.135

它似乎有点工作,但有些事情并不可靠.传出IP的排序不太可预测,有时它们分布不均匀.

这是一个完全裸的Ubuntu安装,所以我不认为这个端口上有任何其他传出连接,但我可能是错的.

有谁知道如何使这更可靠?或者它已经做了正确的事情,我正在解释错误的结果?

我希望IP一个接一个地完美均匀可靠地分发.

以下是结果:

{"ip":"XXX.XXX.XXX.135","about":"/about","Pro!":"http://getjsonip.com"}root@server:~# curl jsonip.com
{"ip":"XXX.XXX.XXX.134","Pro!":"http://getjsonip.com"}root@server:~# curl jsonip.com
{"ip":"XXX.XXX.XXX.133","Pro!":"http://getjsonip.com"}root@server:~# curl jsonip.com
{"ip":"XXX.XXX.XXX.135","Pro!":"http://getjsonip.com"}root@server:~# curl jsonip.com

解决方法

如果你尝试会发生什么

–mode nth –every 3 –packet 0
–mode nth –every 2 –packet 0
–mode nth –every 1 –packet 0

我问,因为我看到几个引用计数器不是全局的事实.

This is a common misunderstanding – the counters are not shared and
since the rules are all terminal,the second rule will only see the
packets not caught by the first rule etc. So the proportions need to
be adjusted for the “missing” packets

In the old days before nth was part of the statistics module –every 2 –packet 0….–every 2 –packet 1 would have been correct. Now there is no global counter and it is reset per rule. So,I needed to do –every 2 –packet 0…. –every 1 –packet 0 instead. Now it works perfectly.

这是其他人试图做你至少发现的事情.

原文链接:https://www.f2er.com/linux/400028.html

猜你在找的Linux相关文章