mininet-3

所需安装套件:
sudo apt-get install bridge utils

基本指令:
brctl addbr br0 新增bridge(brctl = bridge control)
brctl addif br0 eth0 把目前的哪一个介面加入到桥接器(addif = add interface)

拓朴图:
Mininet(轻量级软件定义网络和测试平台) 之三_第1张图片

  #!/usr/bin/env python
from mininet.cli import CLI
from mininet.net import Mininet
from mininet.link import Link,TCLink

if '__main__' == __name__ :
 net = Mininet(link=TCLink)
 h1 = net.addHost('h1', ip="192.168.10.1/24", mac="00:00:00:00:00:01")
 h2 = net.addHost('h2', ip="192.168.10.2/24", mac="00:00:00:00:00:02")
 h3 = net.addHost('h3', ip="192.168.20.1/24", mac="00:00:00:00:00:03")
 r0 = net.addHost('r0')
 s0 = net.addHost('s0')
 net.addLink(h1,s0)
 net.addLink(h2,s0)
 net.addLink(s0,r0)
 net.addLink(r0,h3)
 net.build()
 r0.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")
 r0.cmd('ifconfig r0-eth0 192.168.10.254 netmask 255.255.255.0')
 r0.cmd('ifconfig ro-eth1 192.168.20.254 netmask 255.255.255.0')
 h1.cmd("ip route add default via 192.168.10.254 dev h1-eth0")
 h2.cmd("ip rotue add default via 192.168.10.254 dev h2-eth0")
 h3.cmd("ip route add default via 192.168.20.254 dev h3-eth0")
 s0.cmd("brctl addbr br0")
 s0.cmd("brctl addif br0 s0-eth0")
 s0.cmd("brctl addif br0 s0-eth1")
 s0.cmd("brctl addif br0 s0-eth2")
 s0.cmd("ifconfig br0 up")
 CLI(net)
 net.stop()

提醒

  1. echo 1 > /proc/sys/net/ipv4/ip_forward 启动路由功能
  2. ip rotue add default via 192.168.10.254 dev h2-eth0设定内定路由器

执行结果

parallels@parallels-vm:~/Desktop/shell$ sudo python test2.py 
mininet> net
h1 h1-eth0:s0-eth0
h2 h2-eth0:s0-eth1
h3 h3-eth0:r0-eth1
r0 r0-eth0:s0-eth2 r0-eth1:h3-eth0
s0 s0-eth0:h1-eth0 s0-eth1:h2-eth0 s0-eth2:r0-eth0
mininet> nodes
available nodes are: 
h1 h2 h3 r0 s0
mininet> links
h1-eth0<->s0-eth0 (OK OK) 
h2-eth0<->s0-eth1 (OK OK) 
s0-eth2<->r0-eth0 (OK OK) 
r0-eth1<->h3-eth0 (OK OK) 

xterm s0 打开switch便可以看到上面所设定的br0
Mininet(轻量级软件定义网络和测试平台) 之三_第2张图片
Mininet(轻量级软件定义网络和测试平台) 之三_第3张图片