背景:服务器迁移,移动端写死的IP,故需兼容两个IP
1、shell脚本
#!/bin/bash
OLD_IP=A1.A2.A3.A4
NEW_IP=B1.B2.B3.B4
OLD_PORT=12345
NEW_PORT=12345
# enable IP forwarding
echo 1 > /proc/sys/net/ipv4/ip_forward
# redirect the traffic on port $PORT
iptables -t nat -A PREROUTING -p tcp --dport $OLD_PORT -j DNAT --to-destination $NEW_IP:$NEW_PORT
iptables -t nat -A POSTROUTING -p tcp -d $NEW_IP --dport $NEW_PORT -j SNAT --to-source $OLD_IP
# finally, rewrite the IPs (MASQUERADE)
iptables -t nat -A POSTROUTING -j MASQUERADE
service iptables stop
service iptables save
service iptables restart
2、查询
- 查询nat规则表
#PRETOURING链下NAT表,默认查询FILTER表
iptables -t nat -L PREROUTING
3、规则链删除
- 查询nat规则表
iptables -t nat -L -n --line-numbers
- nat规则链删除
iptables -t nat -D PREROUTING 1
iptables -t nat -D POSTROUTING 1//1表示行号