mininet雙主機雙路由配置

本文主要描述如何在mininet配置下圖所示的拓撲,讓h1和h2能夠通信。拓撲的可視化界面生成請參考上篇論文。

具體的主機ip和路由配置如下:

完整的拓撲代碼如下,注意:我一開始將r1-eth1設置爲10.0.2.0,r2-eth0設置爲10.0.2.1,個人認爲沒什麼問題,但是總是ping不通,於是將兩個接口的ip換成了172.16.2.*就成功了,非常迷惑,如果有了解的大佬可以在評論區留言交流一下:

#!/usr/bin/python

from mininet.net import Mininet
from mininet.node import Controller, RemoteController, OVSController
from mininet.node import CPULimitedHost, Host, Node
from mininet.node import OVSKernelSwitch, UserSwitch
from mininet.node import IVSSwitch
from mininet.cli import CLI
from mininet.log import setLogLevel, info
from mininet.link import TCLink, Intf
from subprocess import call

def myNetwork():

    net = Mininet( topo=None,
                   build=False,
                   ipBase='10.0.0.0/8')

    info( '*** Adding controller\n' )
    info( '*** Add switches\n')
    r1 = net.addHost('r1', cls=Node, ip='0.0.0.0')
    r2 = net.addHost('r2', cls=Node, ip='0.0.0.0')
    
    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='10.0.0.2/24', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='10.0.1.2/24', defaultRoute=None)

    info( '*** Add links\n')
    net.addLink(h1, r1)
    net.addLink(r1, r2)
    net.addLink(r2, h2)

    info( '*** Starting network\n')
    net.build()
    info( '*** Starting controllers\n')
    for controller in net.controllers:
        controller.start()

    info( '*** Starting switches\n')

    info( '*** Post configure switches and hosts\n')
    h1.cmd('ifconfig h1-eth0 10.0.0.2/24')
    h2.cmd('ifconfig h2-eth0 10.0.1.2/24')
    r1.cmd('ifconfig r1-eth0 10.0.0.1/24')
    r1.cmd('ifconfig r1-eth1 172.16.2.1/24')
    

    r2.cmd('ifconfig r2-eth0 172.16.2.2/24')
    r2.cmd('ifconfig r2-eth1 10.0.1.1/24')
    
    r1.cmd('route add -net 10.0.1.0/24 gw 172.16.2.2')
    r2.cmd('route add -net 10.0.0.0/24 gw 172.16.2.1')  
    h1.cmd('route add default gw 10.0.0.1')  
    h2.cmd('route add default gw 10.0.1.1')

    r1.cmd('sysctl -w net.ipv4.ip_forward=1')
    r2.cmd('sysctl -w net.ipv4.ip_forward=1')
    CLI(net)
    net.stop()

if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

最終的結果如下:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章