欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

mininet——简单的路由实验

程序员文章站 2024-02-12 13:27:28
...

mininet——简单的路由实验

参考:(13条消息) mininet多网段主机路由实验配置_caojing1997的博客-CSDN博客

拓扑:

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-crI9Gf0n-1629186433147)(C:\Users\10980\AppData\Roaming\Typora\typora-user-images\image-20210817154325150.png)]

Python代码:

我的文件名是routetest.py

#!/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')
 
    info( '*** Add hosts\n')
    h1 = net.addHost('h1', cls=Host, ip='192.168.8.1/24', defaultRoute=None)
    h2 = net.addHost('h2', cls=Host, ip='192.168.9.1/24', defaultRoute=None)
    h3 = net.addHost('h3', cls=Host, ip='192.168.10.1/24', defaultRoute=None)
    h4 = net.addHost('h4', cls=Host, ip='192.168.11.1/24', defaultRoute=None)
    h5 = net.addHost('h5', cls=Host, ip='192.168.12.1/24', defaultRoute=None)
   
 
    info( '*** Add links\n')
    net.addLink(h1, r1)
    net.addLink(h2, r1)
    net.addLink(h3, r1)
    net.addLink(r1, h4)
    net.addLink(r1, h5)
 
    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')
    r1.cmd('ifconfig r1-eth0 192.168.8.2 netmask 255.255.255.0')
    r1.cmd('ifconfig r1-eth1 192.168.9.2 netmask 255.255.255.0')
    r1.cmd('ifconfig r1-eth2 192.168.10.2 netmask 255.255.255.0')
    r1.cmd('ifconfig r1-eth3 192.168.11.2 netmask 255.255.255.0')
    r1.cmd('ifconfig r1-eth4 192.168.12.2 netmask 255.255.255.0')
 
    h1.cmd('route add default gw 192.168.8.2')
    h2.cmd('route add default gw 192.168.9.2')
    h3.cmd('route add default gw 192.168.10.2')
    h4.cmd('route add default gw 192.168.11.2')
    h5.cmd('route add default gw 192.168.12.2')
 
    r1.cmd('sysctl net.ipv4.ip_forward=1')
  
    CLI(net)
    net.stop()
 
if __name__ == '__main__':
    setLogLevel( 'info' )
    myNetwork()

运行命令:

[email protected]:~/mininet/custom$ su
密码:
[email protected]:/home/ling/mininet/custom# python routetest.py


相关标签: Linux