1

看起来这个问题更多地与 Python 而不是 Mininet 相关。

我正在尝试使用 mininet 构建客户网络拓扑,但卡在这里。

当我运行以下代码时,出现以下错误。

Traceback (most recent call last):
  File "CustomTopo_Router2.py", line 37, in <module>
    customTopo = NetworkTopo()
  File "CustomTopo_Router2.py", line 28, in __init__
    r1_obj = MininetRouter('122.1.1.1')
  File "CustomTopo_Router2.py", line 12, in __init__
    self.cmd('sysctl net.ipv4.ip_forward=1')
  File "build/bdist.linux-x86_64/egg/mininet/node.py", line 353, in cmd
AttributeError: 'MininetRouter' object has no attribute 'name'

我什name至在MininetRouter课堂上没有任何属性。

7 class MininetRouter ( Node ):
8
9     def __init__( self , loo_addr):    
10
11         #Enable IP forwarding on the ROuter
12         self.cmd('sysctl net.ipv4.ip_forward=1')
13
14         # Create Loopback Interface and Assign Loopback Address to the Router
15         # All loopback addresses have mask 32
16         loo_config = 'ifconfig lo:1 ' + loo_addr + ' netmask 255.255.255.255 up'
17         self.cmd(loo_config)
18
19
20 class NetworkTopo( Topo ):
21
22     def __init__( self ):
23
24         # Initialize topology
25         Topo.__init__( self )
26
27         # Create a Router Object
28         r1_obj = MininetRouter('122.1.1.1')
29
30         # Add Router object to the topology
31         r1 = self.addNode('r1', r1_obj)
32
33
34
35 if __name__ == '__main__':
36
37     customTopo = NetworkTopo()
38
39     #Get handle to net to manage your topology
40     net = Mininet(topo=customTopo)
41
42     #start/deploy the Topology
43     net.start()
44
45     #Get Halt at Mininet CLI prompt
46     CLI(net)
47
48     #Destroy and stop the topology
49     net.stop()
50
51     # program ends here

我在这里做错了什么?我有交叉检查的缩进。PS:几年后我正在编写python代码。

4

1 回答 1

0

我对代码进行了一些修改,以使其工作并更符合 Mininet API。对于您要执行的操作,这应该有效。注意我没有放控制器,因为我没有安装它,但是你可以使用Openflow Controller,RemoteController等,希望这对你有用。

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI

class MininetRouterModified( Node ):
    "A Node with IP forwarding enabled."

    def config( self, loo_addr, **params ):
        super( MininetRouterModified, self).config( **params )
        # Enable forwarding on the router
        # Create Loopback Interface and Assign Loopback Address to the Router
        # All loopback addresses have mask 32
        loo_config = 'ifconfig lo:1 ' + loo_addr + ' netmask 255.255.255.255 up'
        self.cmd(loo_config)
        self.cmd( 'sysctl net.ipv4.ip_forward=1' )

    def terminate( self ):
        self.cmd( 'sysctl net.ipv4.ip_forward=0' )
        super( MininetRouterModified, self ).terminate()

class NetworkTopo( Topo ):

    def build(self, **_opts):

        # Create a Router Object
        #r1_obj = MininetRouter('122.1.1.1', name="r1")
        # Add Router object to the topology
        r1 = self.addNode(name='r1',loo_addr='122.1.1.1', cls=MininetRouterModified)


if __name__ == '__main__':
    customTopo = NetworkTopo()

    #Get handle to net to manage your topology
    net = Mininet(topo=customTopo, controller=None)

    #start/deploy the Topology
    net.start()

    #Get Halt at Mininet CLI prompt
    CLI(net)

    #Destroy and stop the topology
    net.stop()
于 2020-06-14T12:29:46.280 回答