您的位置:首页 > 其它

iptables nat 测试

2015-09-10 13:49 405 查看
网络环境:

node01:  eth0: 10.11.1.197/22

node02:  ​eth0: 10.11.1.198/22,  eth1: 192.168.1.198/24

node03:  eth0:  192.168.1.199/24

SNAT 测试


node02始终作为NAT的节点,因为它可以同时访问到node01和 node03

模拟场景, 从node01 ping node03 的ip (192.168.1.199)经过node02的时候source ip通过SNAT转换为node02的ip(192.168.1.199)

当前网络环境 node01 不能ping通 192.168.1.198 和 192.168.1.199 因为node01还没建立192.168.1.x的路由。

1.  在node02建立相应的SNAT规则(SNAT规则一般建在POSTROUTING上):

$ iptables -t nat -A POSTROUTING -s 10.11.1.197 -j DNAT --to-destination 192.168.1.198

2. 在node01上建立相应的路由规则:

$ route add -net 192.168.1.0/24 gw 10.11.1.198

3. 从node01上ping 192.168.1.199

$ ping 192.168.1.199

PING 192.168.1.199 (192.168.1.199) 56(84) bytes of data.

64 bytes from 192.168.1.199: icmp_seq=1 ttl=64 time=2.83 ms

64 bytes from 192.168.1.199: icmp_seq=2 ttl=64 time=1.10 ms

4.  从node03上抓icmp的包, node03查看的是来自192.168.1.198(node02)的包:

$ tcpdump -nei eth0 icmp

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

01:47:46.394650 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32146, seq 5, length 64

01:47:46.394716 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32146, seq 5, length 64

DNAT 测试

为了更好的测试,在测试之前最好清空SNAT测试的规则,路由等。

模拟场景: 在node02 eth0上增加一个secondary ip, node01访问这个ip的时候,其实真正访问的是node03

1.  往node02上增加一个secondary ip

$ ip addr add 10.11.1.199/22 dev eth0

2. 建立DNAT规则, (DNAT规则一般建立在PREROUTING上), 将访问10.11.1.199的包转换为访问192.168.1.199的包。

$ iptables -t nat -A PREROUTING -d 10.11.1.199 -j DNAT --to-destination 192.168.1.199

3. 从node01上ping 10.11.1.199

$ ping 10.11.1.199

PING 10.11.1.199 (10.11.1.199) 56(84) bytes of data.

64 bytes from 10.11.1.199: icmp_seq=1 ttl=64 time=1.42 ms

64 bytes from 10.11.1.199: icmp_seq=2 ttl=64 time=0.905 ms

4. 从node03上抓icmp包, node03上同样抓的是来自192.168.1.198(node02)的包。

tcpdump: verbose output suppressed, use -v or -vv for full protocol decode

listening on eth0, link-type EN10MB (Ethernet), capture size 65535 bytes

01:54:24.446025 00:50:56:b0:74:08 > 00:50:56:b0:52:d7, ethertype IPv4 (0x0800), length 98: 192.168.1.198 > 192.168.1.199: ICMP echo request, id 32156, seq 1, length 64

01:54:24.446266 00:50:56:b0:52:d7 > 00:50:56:b0:74:08, ethertype IPv4 (0x0800), length 98: 192.168.1.199 > 192.168.1.198: ICMP echo reply, id 32156, seq 1, length 64



5. 为了让node01能ssh到node03, 在node02上打开FORWARD chain的ssh端口:

$ iptables -A FORWARD -p tcp --dport 22 -j ACCEPT

6. 从node01上ssh 10.11.1.199, 虽然10.11.1.199绑定在node02上, 真正访问的节点其实是 node03

$ ssh root@10.11.1.199

root@10.11.1.199's password:

Last login: Thu Sep 10 01:37:10 2015 from 192.168.1.198

[root@node03~]#
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  iptables nat