将mininet连接到外部主机

2024-10-04 03:26:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我刚刚设置了一个mininet拓扑。 现在我想通过Ubuntu的接口将Mininet交换机上的一个端口连接到一个外部端口。 Ubuntu服务器有两个端口:

  • ens33连接到实际网络
  • ens38已连接到VMnet2

我的Python脚本:

from mininet.net import Mininet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import Intf
from mininet.log import setLogLevel, info

from mininet.topo import Topo

class MyTopo( Topo ):
    "Simple topology example."

def __init__( self ):
    "Create custom topo."

    # Initialize topology
    Topo.__init__( self )

    # Add hosts and switches
    '*** Add switches\n'
    s1 = self.addSwitch('s1')
    Intf('ens38', node=s1)

    s2 = self.addSwitch('s2')

    '*** Add hosts\n'
    h2 = self.addHost('h2')
    # Add links
    '*** Add links\n'
    self.addLink(h2, s2)

topos = { 'mytopo': ( lambda: MyTopo() ) }

但当我使用以下命令行运行它时:

mn --custom qtho-topo.py --topo mytopo \
  --controller=remote,ip=192.168.1.128,port=6633 \
  --switch ovsk,protocols=OpenFlow13

有错误:

Caught exception. Cleaning up...

AttributeError: 'str' object has no attribute 'addIntf'

有没有人有这方面的经验


Tags: 端口fromimportselfaddnodeubuntuh2
2条回答

这个问题是不久前提出的,但我希望我的建议能有所帮助。 self.addSwitch()函数返回字符串,因此s1是字符串,而Intf函数需要Node类型

如果要使用命令行运行,一个简单的解决方案是创建网络,然后使用添加接口的测试函数,如下例所示:

from mininet.net import Mininet
from mininet.node import Controller
from mininet.cli import CLI
from mininet.link import Intf
from mininet.log import setLogLevel, info

from mininet.topo import Topo

class MyTopo( Topo ):
    "Simple topology example."

    def build( self ):
        "Create custom topo."

        # Add hosts and switches
        '*** Add switches\n'
        s1 = self.addSwitch('s1')
        info("**type s1  > ", type(s1), "\n")

        s2 = self.addSwitch('s2')

        '*** Add hosts\n'
        h2 = self.addHost('h2')
        # Add links
        '*** Add links\n'
        self.addLink(h2, s2)

def addIface(mn):
    s1_node = mn.getNodeByName("s1")
    Intf("ens38", node=s1_node)
    CLI(mn)

tests = { 'addIf': addIface }
topos = { 'mytopo': ( lambda: MyTopo() ) }

要在命令行中运行它,假设您已将文件命名为test.py:

mn  custom=test.py  topo=mytopo  test=addIf

除了朱塞佩的回答之外,这条代码对我也很有用:

def addIface(mn):
    s1_node = mn.getNodeByName("s1")
    s1_node.attach("ens38")
    CLI(mn)

相关问题 更多 >