作者做實(shí)驗(yàn)用的是EVE-NG模擬器,相關(guān)的請(qǐng)參考:EVE-NG干貨
1.實(shí)驗(yàn)拓?fù)?/h1>

其中VPC1~VPC3模擬普通電腦,交換機(jī)S1用做二層交換機(jī)匯聚VPC1~VPC2的流量,nat操作在路由器R1上進(jìn)行。交換機(jī)S1使用的鏡像為IOL L2-ADVENTERPRISE-M-15.1-20140814.bin,路由器R1使用的鏡像為i86bi-linux-l3-adventerprisek9-12.4.bin
2.對(duì)交換機(jī)路由器等進(jìn)行配置
配置VPC的IP地址和網(wǎng)關(guān)
VPC1:
VPCS> ip 192.168.1.11/24 192.168.1.1
Checking for duplicate address...
PC1 : 192.168.1.11 255.255.255.0 gateway 192.168.1.1
VPC2:
VPCS> ip 192.168.1.12/24 192.168.1.1
Checking for duplicate address...
PC1 : 192.168.1.12 255.255.255.0 gateway 192.168.1.1
VPC3:
VPCS> ip 100.100.100.100/24
Checking for duplicate address...
PC1 : 100.100.100.100 255.255.255.0
配置交換機(jī),匯聚VPC1~VPC2的流量
交換機(jī)S1
Switch>en
Switch#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
Switch(config)#hostname S1
S1(config)#vlan 10
S1(config-vlan)#exi
S1(config)#int e0/0
S1(config-if)#switchport mode access
S1(config-if)#switchport access vlan 10
S1(config-if)#no shutdown
S1(config-if)#end
S1(config)#int e0/1
S1(config-if)#switchport mode access
S1(config-if)#switchport access vlan 10
S1(config-if)#no shutdown
S1(config-if)#end
S1(config)#int e0/2
S1(config-if)#switchport mode access
S1(config-if)#switchport access vlan 10
S1(config-if)#no shutdown
S1(config-if)#end
在路由器上配置NAT規(guī)則
路由器R1
Router>en
Router#configure terminal
Router(config)#hostname R1
配置nat服務(wù)和地址池
R1(config)#ip nat pool wlsyahz 100.100.100.11 100.100.100.20 netmask 255.255.255.0
配置將192.168.1.0/24網(wǎng)段的地址轉(zhuǎn)換為地址池里的地址
R1(config)#ip nat inside source list 1 pool wlsyahz
R1(config)#access-list 1 permit 192.168.1.0 0.0.0.255
在入端口和出端口上啟動(dòng)nat轉(zhuǎn)換
R1(config)#interface e0/0
R1(config-if)#ip nat inside
R1(config-if)#exit
R1(config)#interface e0/1
R1(config-if)#ip nat outside
R1(config-if)#end
配置端口0和端口1的ip地址
R1(config)#int ethernet 0/0
R1(config-if)#ip address 192.168.1.1 255.255.255.0
R1(config-if)#no shutdown
R1(config-if)#int ethernet 0/1
R1(config-if)#ip address 100.100.100.1 255.255.255.0
R1(config-if)#no shutdown
3.NAT過程分析
配置完成后我們?cè)赗1上使能ip nat debug
R1#debug ip nat
然后從VPC1上ping VPC3
VPCS> ping 100.100.100.100
84 bytes from 100.100.100.100 icmp_seq=1 ttl=63 time=1.073 ms
84 bytes from 100.100.100.100 icmp_seq=2 ttl=63 time=1.079 ms
84 bytes from 100.100.100.100 icmp_seq=3 ttl=63 time=1.072 ms
84 bytes from 100.100.100.100 icmp_seq=4 ttl=63 time=0.552 ms
84 bytes from 100.100.100.100 icmp_seq=5 ttl=63 time=1.071 ms
這時(shí)會(huì)在R1上會(huì)看到VPC1的源地址被轉(zhuǎn)換成了100.100.10.11
*Dec 8 04:43:49.953: NAT*: s=192.168.1.11->100.100.100.11, d=100.100.100.100 [32645]
*Dec 8 04:43:49.953: NAT*: s=100.100.100.100, d=100.100.100.11->192.168.1.11 [32645]
*Dec 8 04:43:50.954: NAT*: s=192.168.1.11->100.100.100.11, d=100.100.100.100 [32646]
*Dec 8 04:43:50.955: NAT*: s=100.100.100.100, d=100.100.100.11->192.168.1.11 [32646]
在R1的0/1口抓包如下

在R1的0/0口抓包如下

從VPC2上ping VPC3,原理相同
VPCS> ping 100.100.100.100
84 bytes from 100.100.100.100 icmp_seq=1 ttl=63 time=0.643 ms
84 bytes from 100.100.100.100 icmp_seq=2 ttl=63 time=0.992 ms
84 bytes from 100.100.100.100 icmp_seq=3 ttl=63 time=0.909 ms
84 bytes from 100.100.100.100 icmp_seq=4 ttl=63 time=0.792 ms
84 bytes from 100.100.100.100 icmp_seq=5 ttl=63 time=1.886 ms
*Dec 8 05:03:43.960: NAT*: s=192.168.1.12->100.100.100.12, d=100.100.100.100 [33839]
*Dec 8 05:03:43.960: NAT*: s=100.100.100.100, d=100.100.100.12->192.168.1.12 [33839]
*Dec 8 05:03:44.962: NAT*: s=192.168.1.12->100.100.100.12, d=100.100.100.100 [33840]
*Dec 8 05:03:44.963: NAT*: s=100.100.100.100, d=100.100.100.12->192.168.1.12 [33840]