IPTables转发到dynamicIP

我在servera.example.com上运行一个服务器,在serverb.example.com上运行一个服务器。 我希望将所有端口80stream量redirect到servera.example.com到serverb.example.com,并保留所有其他端口。 难点是serverb.example.com有一个dynamicIP,所以IP不能直接放入iptables规则。 有没有办法做到这一点?

您可以使用简单的脚本来查找serverb.example.com的当前IP。只需运行:

[root@user ~]# dig +short serverb.example.com | sed -e '1d' 

这将返回服务器b的当前IP地址。 但是请注意,只有在使用新IP更新了DNSlogging时,这才会起作用。

作为一个例子,如何使用这个脚本取决于你。

 #!/bin/bash SERVB=`dig +short serverb.example.com | sed -e '1d'` iptables (your rule goes here) -d $SERVB 

你不能直接用iptables / netfilter来做这个事情,因为只有在加载规则时才会查询DNS。 如果serverb.example.com在加载规则时指向192.0.2.1,并且更改为192.0.2.2,则netfilter将不知道数据包将被发送到错误的地址。

也许一个反向代理将帮助你。

首先确保DNSlogging将自动更新server.example.com。

其次 – 如果服务器自动调用他的DNSlogging,请使用@fallen中的脚本,但插入用于刷新iptables表的命令。

第三 – 定期设置一个Cron作业或系统单元来执行脚本。