最近在做静态路由转发实验(计算机网络课程实验),实验课程给出了一定的代码,不过老师在实验上挖了一些坑需要我们填补。
先贴出实验代码:
#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
class RouterTopo(Topo):
def build(self):
h1 = self.addHost('h1')
h2 = self.addHost('h2')
h3 = self.addHost('h3')
r1 = self.addHost('r1')
self.addLink(h1, r1)
self.addLink(h2, r1)
self.addLink(h3, r1)
if __name__ == '__main__':
topo = RouterTopo()
net = Mininet(topo = topo, controller = None)
h1, h2, h3, r1 = net.get('h1', 'h2', 'h3', 'r1')
h1.cmd('ifconfig h1-eth0 10.0.1.11/24')
h2.cmd('ifconfig h2-eth0 10.0.2.22/24')
h3.cmd('ifconfig h3-eth0 10.0.3.33/24')
h1.cmd('route add default gw 10.0.1.1')
h2.cmd('route add default gw 10.0.2.1')
h3.cmd('route add default gw 10.0.3.1')
for h in (h1, h2, h3):
h.cmd('./scripts/disable_offloading.sh')
h.cmd('./scripts/disable_ipv6.sh')
r1.cmd('ifconfig r1-eth0 10.0.1.1/24')
r1.cmd('ifconfig r1-eth1 10.0.2.1/24')
r1.cmd('ifconfig r1-eth2 10.0.3.1/24')
r1.cmd('./scripts/disable_arp.sh')
r1.cmd('./scripts/disable_icmp.sh')
r1.cmd('./scripts/disable_ip_forward.sh')
net.start()
CLI(net)
net.stop()
在Ubuntu下运行该router_topo.py程序时,使用命令sudo mn --custom router_topo.py --topo router 出现了invalid topo name错误。截图如下:
后面查看stackflow后解决了该问题。
在class RouterTopo定义前添加一行TOPOS = {'router':(lambda:RouterTopo())}
这样就可以解决了,注意不要缩进。成功截图如下: