2016-04-29 142 views
1

我想用--custom選項在Mininet中運行.py腳本。我的代碼如下:如何在Mininet中運行sudo mn --custom選項?

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. 
[email protected]:~$ 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. 

你能告訴我爲什麼得到錯誤:無效的地形名稱?

def

回答

1

添加行不應該有縮進

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

現在可以執行

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

你寫了上面的程序可以直接執行使用

sudo python <file_name>.py 

如果您需要使用帶有值的sudo mn運行,更新代碼如圖所示

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

非常感謝Karthik! – KristinaP

+0

我還有一個問題。你可能知道爲什麼拓撲廣告只有k = 2個交換機和n = 1個主機,而不是k = 4個交換機和n = 8個主機,因爲它是在def simpleTest()中指定的?我嘗試在def simpleTest()之前和之後添加TOPOS,但結果相同。 – KristinaP

+0

@KristinaP請參閱更新的答案 –