SDN 编写Python脚本创建自定义网络拓扑

发布时间 2023-05-04 15:05:24作者: 幻非

编写Python脚本创建自定义网络拓扑,包括5台交换机5台主机

from mininet.topo import Topo


class RingTopo(Topo):
    def __init__(self):
        Topo.__init__(self)

        # Create switches
        s_num = 5
        h_num = 5
        switches = []
        hosts = []
        for i in range(s_num):
            switches.append(self.addSwitch('s%d' % (i + 1)))

        # Create hosts
        for i in range(h_num):
            hosts.append(self.addHost('h%d' % (i + 1)))

        # Create links
        for i in range(s_num - 1):
            self.addLink(switches[i], switches[i + 1])

        self.addLink(switches[0], switches[s_num-1])

        for i in range(s_num):
            self.addLink(switches[i], hosts[i])


topos = {'ring': (lambda: RingTopo())}

执行

mn --custom topo_ring.py --topo ring --controller=remote

image