如何在Mininet中运行sudo mn --custom选项?

时间:2016-04-29 15:51:23

标签: python mininet

我正在尝试使用--custom选项在Mininet中运行.py脚本。我的代码如下:

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.util import irange

class LinearTopo( Topo ):
    "Linear topology of k switches, with n hosts per switch."

    def build( self, k=2, n=1, **_opts):
        """k: number of switches
           n: number of hosts per switch"""
        self.k = k
        self.n = n

        if n == 1:
            genHostName = lambda i, j: 'h%s' % i
        else:
            genHostName = lambda i, j: 'h%ss%d' % ( j, i )

        lastSwitch = None
        for i in irange( 1, k ):
            # Add switch
            switch = self.addSwitch( 's%s' % i )
            # Add hosts to switch
            for j in irange( 1, n ):
                host = self.addHost( genHostName( i, j ) )
                self.addLink( host, switch )
            # Connect switch to previous
            if lastSwitch:
                self.addLink( switch, lastSwitch )
            lastSwitch = switch


def simpleTest():
        "Create and test a simple network"
        topo = LinearTopo(k=4,n=8)
        net = Mininet(topo)
        net.start()
        print "Dumping host connections"
        dumpNodeConnections(net.hosts)
        print "Testing network connectivity"
        net.pingAll()
        net.stop()

if __name__ == '__main__':
        # Tell mininet to print useful information
        setLogLevel('info')
        simpleTest()

当我尝试:

sudo mn --custom topo.py --topo LinearTopo

我收到以下错误:

*** Cleanup complete.
mininet@mininet-vm:~$ sudo mn --custom topo.py --topo LinearTopo
--------------------------------------------------------------------------------
Caught exception. Cleaning up...

Exception: Invalid topo name LinearTopo
--------------------------------------------------------------------------------
*** Removing excess controllers/ofprotocols/ofdatapaths/pings/noxes
killall controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> /dev/null
killall -9 controller ofprotocol ofdatapath ping nox_core lt-nox_core ovs-openflowd ovs-controller udpbwtest mnexec ivs 2> /dev/null
pkill -9 -f "sudo mnexec"
*** Removing junk from /tmp
rm -f /tmp/vconn* /tmp/vlogs* /tmp/*.out /tmp/*.log
*** Removing old X11 tunnels
*** Removing excess kernel datapaths
ps ax | egrep -o 'dp[0-9]+' | sed 's/dp/nl:/'
***  Removing OVS datapaths
ovs-vsctl --timeout=1 list-br
ovs-vsctl --timeout=1 list-br
*** Removing all links of the pattern foo-ethX
ip link show | egrep -o '([-_.[:alnum:]]+-eth[[:digit:]]+)'
ip link show
*** Killing stale mininet node processes
pkill -9 -f mininet:
*** Shutting down stale tunnels
pkill -9 -f Tunnel=Ethernet
pkill -9 -f .ssh/mn
rm -f ~/.ssh/mn/*
*** Cleanup complete.

你能否告诉我为什么会出现错误:topo名称无效?

1 个答案:

答案 0 :(得分:1)

def之前添加一行不应该缩进

TOPOS = {'LinearTopo' : (lambda : LinearTopo())}

现在你可以执行

sudo mn --custom topo.py --topo LinearTopo

您编写的上述程序可以使用

直接执行
sudo python <file_name>.py

如果您需要使用值sudo mn运行,请更新代码,如下所示

 TOPOS = {'LinearTopo' : (lambda : LinearTopo(4,5))}