mininet topology

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010643777/article/details/81458140

2 hosts and 4 routers topology

 This code is refered from[2]. From this example, I know the difference between “ip route add” and “route add”. [1]
2h4s.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#   0   1   2   3   4
#h1--r1--r2--r3--r4--h2
# 10.0.X.0
def rp_disable(host):
    ifaces = host.cmd('ls /proc/sys/net/ipv4/conf')
    ifacelist = ifaces.split()    # default is to split on whitespace
    for iface in ifacelist:
       if iface != 'lo': host.cmd('sysctl net.ipv4.conf.' + iface + '.rp_filter=0')

net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.0.1')
r1 = net.addHost('r1',ip='10.0.0.2')
r2 = net.addHost('r2',ip='10.0.1.2')
r3 = net.addHost('r3',ip='10.0.2.2')
r4 = net.addHost('r4',ip='10.0.3.2')
h2 = net.addHost('h2',ip='10.0.4.2')
c0 = net.addController( 'c0' )

net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(r1,r2,intfName1='r1-eth1',intfName2='r2-eth0')
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth0')
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0')
net.addLink(r4,h2,intfName1='r4-eth1',intfName2='h2-eth0')
net.build()

h1.setIP('10.0.0.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.0.1 netmask 255.255.255.0")
h1.cmd("route add default gw 10.0.0.2 dev h1-eth0")

r1.cmd("ifconfig r1-eth0 10.0.0.2/24")
r1.cmd("ifconfig r1-eth1 10.0.1.1/24")
r1.cmd("ip route add to 10.0.4.0/24 via 10.0.1.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.0.1")
r1.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r1)

r2.cmd("ifconfig r2-eth0 10.0.1.2/24")
r2.cmd("ifconfig r2-eth1 10.0.2.1/24")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.2.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.1.1")
r2.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r2)

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.3.1/24")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.3.2")
r3.cmd("ip route add to 10.0.0.0/24 via 10.0.2.1")
r3.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r3)

r4.cmd("ifconfig r4-eth0 10.0.3.2/24")
r4.cmd("ifconfig r4-eth1 10.0.4.1/24")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r4.cmd("ip route add to 10.0.0.0/24 via 10.0.3.1")
r4.cmd('sysctl net.ipv4.ip_forward=1')
rp_disable(r4)

h2.setIP('10.0.4.2', intf='h2-eth0')
h2.cmd("ifconfig h2-eth0 10.0.4.2 netmask 255.255.255.0")
h2.cmd("route add default gw 10.0.4.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

[1]ip route 和 route命令的区别
[2]routeline topology
[3]An intruction to computer networks

star topology

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#        h1
#         |1      10.0.x.0
#        r1
#       /2   \3   
#      h2   h3
#
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
h2 = net.addHost('h2',ip='10.0.2.2')
h3 = net.addHost('h3',ip='10.0.3.2')
r1 = net.addHost('r1',ip='10.0.1.2')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(h2,r1,intfName1='h2-eth0',intfName2='r1-eth1')
net.addLink(h3,r1,intfName1='h3-eth0',intfName2='r1-eth2')
net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("route add default gw 10.0.1.2")

h2.cmd("ifconfig h2-eth0 10.0.2.2/24")
h2.cmd("route add default gw 10.0.2.1")

h3.cmd("ifconfig h3-eth0 10.0.3.2/24")
h3.cmd("route add default gw 10.0.3.1")

r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ifconfig r1-eth2 10.0.3.1/24")

r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.3.0/24 via 10.0.3.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')
net.start()
time.sleep(1)
CLI(net)
net.stop()

mininet multi-interface ring topology

 This trivial cost me nearly one afternoon and a night.
 Thanks this blog[1].
 The tips to use:

1 sudo su
2 python 4h.py
3 xterm h1 h3
4 ping X.X.X.X

4h.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
##https://serverfault.com/questions/417885/configure-gateway-for-two-nics-through-static-routeing
#    ____h2____
#   /          \
# h1           h3
#   \___h4_____/
#  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
h2 = net.addHost('h2',ip='10.0.1.2')
h3 = net.addHost('h3',ip='10.0.2.2')
h4 = net.addHost('h4',ip='10.0.3.2')
c0 = net.addController( 'c0' )
net.addLink(h1,h2,intfName1='h1-eth0',intfName2='h2-eth0')
net.addLink(h2,h3,intfName1='h2-eth1',intfName2='h3-eth0')
net.addLink(h1,h4,intfName1='h1-eth1',intfName2='h4-eth0')
net.addLink(h4,h3,intfName1='h4-eth1',intfName2='h3-eth1')
net.build()
h1.setIP('10.0.1.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.1.1 netmask 255.255.255.0")

h1.setIP('10.0.3.1', intf='h1-eth1')
h1.cmd("ifconfig h1-eth1 10.0.3.1 netmask 255.255.255.0")

h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("route add default gw 10.0.1.2  dev h1-eth0")

h2.setIP('10.0.1.2', intf='h2-eth0')
h2.setIP('10.0.2.1', intf='h2-eth1')
h2.cmd("ifconfig h2-eth0 10.0.1.2/24")
h2.cmd("ifconfig h2-eth1 10.0.2.1/24")
h2.cmd("ip route add 10.0.2.0/24 via 10.0.2.2")
h2.cmd("ip route add 10.0.1.0/24 via 10.0.1.1")
h2.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


h4.setIP('10.0.3.2', intf='h4-eth0')
h4.setIP('10.0.4.1', intf='h4-eth1')
h4.cmd("ifconfig h4-eth0 10.0.3.2/24")
h4.cmd("ifconfig h4-eth1 10.0.4.1/24")
h4.cmd("ip route add 10.0.4.0  dev h4-eth1") #via 10.0.4.2
h4.cmd("ip route add 10.0.3.0 via 10.0.3.1")

h4.cmd("echo 1 > /proc/sys/net/ipv4/ip_forward")


h3.setIP('10.0.2.2', intf='h3-eth0')
h3.cmd("ifconfig h3-eth0 10.0.2.2 netmask 255.255.255.0")
h3.setIP('10.0.4.2', intf='h3-eth1')
h3.cmd("ifconfig h3-eth1 10.0.4.2 netmask 255.255.255.0")

h3.cmd("ip route flush all proto static scope global")
h3.cmd("ip route add 10.0.2.2/24 dev h3-eth0 table 5000")
h3.cmd("ip route add default via 10.0.2.1 dev h3-eth0 table 5000")

h3.cmd("ip route add 10.0.4.2/24 dev h3-eth1 table 5001")
h3.cmd("ip route add default via 10.0.4.1 dev h3-eth1 table 5001")
h3.cmd("ip rule add from 10.0.2.2 table 5000")
h3.cmd("ip rule add from 10.0.4.2 table 5001")

net.start()
time.sleep(1)
CLI(net)
net.stop()

这里写图片描述
[1]Configure gateway for two NICs through static routeing

3 hosts and 4 routers

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
import time
#   0   1   2   3   4
#h1--r1--r2--r3--r4--h2
#             \5
#              h3
# 10.0.X.0
def rp_disable(host):
    ifaces = host.cmd('ls /proc/sys/net/ipv4/conf')
    ifacelist = ifaces.split()    # default is to split on whitespace
    for iface in ifacelist:
       if iface != 'lo': host.cmd('sysctl net.ipv4.conf.' + iface + '.rp_filter=0')

net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.0.1')
r1 = net.addHost('r1',ip='10.0.0.2')
r2 = net.addHost('r2',ip='10.0.1.2')
r3 = net.addHost('r3',ip='10.0.2.2')
r4 = net.addHost('r4',ip='10.0.3.2')
h2 = net.addHost('h2',ip='10.0.4.2')
h3 = net.addHost('h3',ip='10.0.5.2')
c0 = net.addController( 'c0' )

net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0')
net.addLink(r1,r2,intfName1='r1-eth1',intfName2='r2-eth0')
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth0')
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0')
net.addLink(r4,h2,intfName1='r4-eth1',intfName2='h2-eth0')
net.addLink(r3,h3,intfName1='r3-eth2',intfName2='h3-eth0')
net.build()

h1.setIP('10.0.0.1', intf='h1-eth0')
h1.cmd("ifconfig h1-eth0 10.0.0.1 netmask 255.255.255.0")
h1.cmd("route add default gw 10.0.0.2")

h3.cmd("ifconfig h3-eth0 10.0.5.2/24")
h3.cmd("route add default gw 10.0.5.1")

r1.cmd("ifconfig r1-eth0 10.0.0.2/24")
r1.cmd("ifconfig r1-eth1 10.0.1.1/24")
r1.cmd("ip route add to 10.0.4.0/24 via 10.0.1.2")
r1.cmd("ip route add to 10.0.0.0/24 via 10.0.0.1")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.1.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r1)

r2.cmd("ifconfig r2-eth0 10.0.1.2/24")
r2.cmd("ifconfig r2-eth1 10.0.2.1/24")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.2.2")
r2.cmd("ip route add to 10.0.0.0/24 via 10.0.1.1")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r2.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r2)

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.3.1/24")
r3.cmd("ifconfig r3-eth2 10.0.5.1/24")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.3.2")
r3.cmd("ip route add to 10.0.0.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r3)

r4.cmd("ifconfig r4-eth0 10.0.3.2/24")
r4.cmd("ifconfig r4-eth1 10.0.4.1/24")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r4.cmd("ip route add to 10.0.0.0/24 via 10.0.3.1")
r4.cmd("ip route add to 10.0.5.0/24 via 10.0.3.1")
r4.cmd('sysctl net.ipv4.ip_forward=1')
#rp_disable(r4)

h2.cmd("ifconfig h2-eth0 10.0.4.2/24")
h2.cmd("route add default gw 10.0.4.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

mininet multi interface topology

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.cli import CLI
from mininet.link import TCLink
import time
#    ___r1____
#   /          \0  1
# h1            r3---h2
#  \           /2
#   ---r2-----
max_queue_size = 20  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
r1 = net.addHost('r1',ip='10.0.1.2')
r2 = net.addHost('r2',ip='10.0.3.2')
r3 = net.addHost('r3',ip='10.0.5.1')
h2 = net.addHost('h2',ip='10.0.5.2')
c0 = net.addController('c0')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r1,r3,intfName1='r1-eth1',intfName2='r3-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r3,h2,intfName1='r3-eth1',intfName2='h2-eth0',cls=TCLink , bw=10, delay='10ms', max_queue_size=max_queue_size)
net.addLink(h1,r2,intfName1='h1-eth1',intfName2='r2-eth0',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth2',cls=TCLink , bw=2, delay='20ms', max_queue_size=max_queue_size)

net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("ifconfig h1-eth1 10.0.3.1/24")
h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("ip route add default gw 10.0.1.2  dev h1-eth0")
#that be a must or else a tcp client would not know how to route packet out
#h1.cmd("route add default gw 10.0.1.2  dev h1-eth0") would not work for the second part when a tcp client bind a address


r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.5.1/24")
r3.cmd("ifconfig r3-eth2 10.0.4.2/24")
r3.cmd("ip route add to 10.0.1.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.2.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.3.0/24 via 10.0.4.1")
r3.cmd('sysctl net.ipv4.ip_forward=1')

r2.cmd("ifconfig r2-eth0 10.0.3.2/24")
r2.cmd("ifconfig r2-eth1 10.0.4.1/24")
r2.cmd("ip route add to 10.0.3.0/24 via 10.0.3.1")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.4.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')

h2.cmd("ifconfig h2-eth0 10.0.5.2/24")
h2.cmd("route add default gw 10.0.5.1")

net.start()
time.sleep(1)
CLI(net)
net.stop()

 h1.cmd(“route add default gw 10.0.1.2 dev h1-eth0”),this can make ping 10.0.5.2 works ok. If this line is commented out, ping 10.0.5.2 will not work but “ping -I 10.0.1.1 10.0.5.2” works normally.

ring topology

r4 routes packet from 10.0.1.1 to r5 and packets with source ip 10.0.3.1 to r6. This topology was created for multipath protocol test.
ringTopology.py

#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.link import TCLink
from mininet.cli import CLI
import time
import subprocess
import os,signal
import sys
#                  5.0   6.0     7.0
#    ___r1____           ____r5____
#   /          \0 1   0 /1         \  10.0
# h1            r3-----r4            r7---h2
#  \           /2       \____r6____/
#   ---r2-----           8.0    9.0
max_queue_size = 200  
net = Mininet( cleanup=True )
h1 = net.addHost('h1',ip='10.0.1.1')
r1 = net.addHost('r1',ip='10.0.1.2')
r2 = net.addHost('r2',ip='10.0.3.2')
r3 = net.addHost('r3',ip='10.0.5.1')

r4 = net.addHost('r4',ip='10.0.5.2')
r5 = net.addHost('r5',ip='10.0.6.2')
r6 = net.addHost('r6',ip='10.0.8.2')
r7 = net.addHost('r7',ip='10.0.10.1')
h2 = net.addHost('h2',ip='10.0.10.2')
c0 = net.addController('c0')
net.addLink(h1,r1,intfName1='h1-eth0',intfName2='r1-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r1,r3,intfName1='r1-eth1',intfName2='r3-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(h1,r2,intfName1='h1-eth1',intfName2='r2-eth0',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r2,r3,intfName1='r2-eth1',intfName2='r3-eth2',cls=TCLink , bw=500, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r3,r4,intfName1='r3-eth1',intfName2='r4-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=max_queue_size)
net.addLink(r4,r5,intfName1='r4-eth1',intfName2='r5-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r5,r7,intfName1='r5-eth1',intfName2='r7-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r7,h2,intfName1='r7-eth1',intfName2='h2-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r4,r6,intfName1='r4-eth2',intfName2='r6-eth0',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.addLink(r6,r7,intfName1='r6-eth1',intfName2='r7-eth2',cls=TCLink , bw=100, delay='10ms', max_queue_size=10*max_queue_size)
net.build()

h1.cmd("ifconfig h1-eth0 10.0.1.1/24")
h1.cmd("ifconfig h1-eth1 10.0.3.1/24")
h1.cmd("ip route flush all proto static scope global")
h1.cmd("ip route add 10.0.1.1/24 dev h1-eth0 table 5000")
h1.cmd("ip route add default via 10.0.1.2 dev h1-eth0 table 5000")

h1.cmd("ip route add 10.0.3.1/24 dev h1-eth1 table 5001")
h1.cmd("ip route add default via 10.0.3.2 dev h1-eth1 table 5001")
h1.cmd("ip rule add from 10.0.1.1 table 5000")
h1.cmd("ip rule add from 10.0.3.1 table 5001")
h1.cmd("ip route add default gw 10.0.1.2  dev h1-eth0")
#that be a must or else a tcp client would not know how to route packet out
h1.cmd("route add default gw 10.0.1.2  dev h1-eth0") #would not work for the second part when a tcp client bind a address


r1.cmd("ifconfig r1-eth0 10.0.1.2/24")
r1.cmd("ifconfig r1-eth1 10.0.2.1/24")
r1.cmd("ip route add to 10.0.1.0/24 via 10.0.1.1")
r1.cmd("ip route add to 10.0.2.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.5.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.6.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.7.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.8.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.9.0/24 via 10.0.2.2")
r1.cmd("ip route add to 10.0.10.0/24 via 10.0.2.2")
r1.cmd('sysctl net.ipv4.ip_forward=1')

r3.cmd("ifconfig r3-eth0 10.0.2.2/24")
r3.cmd("ifconfig r3-eth1 10.0.5.1/24")
r3.cmd("ifconfig r3-eth2 10.0.4.2/24")
r3.cmd("ip route add to 10.0.1.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.2.0/24 via 10.0.2.1")
r3.cmd("ip route add to 10.0.5.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.4.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.3.0/24 via 10.0.4.1")
r3.cmd("ip route add to 10.0.6.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.7.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.8.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.9.0/24 via 10.0.5.2")
r3.cmd("ip route add to 10.0.10.0/24 via 10.0.5.2")
r3.cmd('sysctl net.ipv4.ip_forward=1')

r2.cmd("ifconfig r2-eth0 10.0.3.2/24")
r2.cmd("ifconfig r2-eth1 10.0.4.1/24")
r2.cmd("ip route add to 10.0.3.0/24 via 10.0.3.1")
r2.cmd("ip route add to 10.0.4.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.5.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.6.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.7.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.8.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.9.0/24 via 10.0.4.2")
r2.cmd("ip route add to 10.0.10.0/24 via 10.0.4.2")
r2.cmd('sysctl net.ipv4.ip_forward=1')

r4.cmd("ifconfig r4-eth0 10.0.5.2/24")
r4.cmd("ifconfig r4-eth1 10.0.6.1/24")
r4.cmd("ifconfig r4-eth2 10.0.8.1/24")
r4.cmd("ip route add to 10.0.1.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.2.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.3.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.4.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.5.0/24 via 10.0.5.1")
r4.cmd("ip route add to 10.0.6.0/24 via 10.0.6.2")
r4.cmd("ip route add to 10.0.7.0/24 via 10.0.6.2")
r4.cmd("ip route add to 10.0.8.0/24 via 10.0.8.2")
r4.cmd("ip route add to 10.0.9.0/24 via 10.0.8.2")

r4.cmd("ip route add 10.0.6.1/24 dev r4-eth1 table 5000")
r4.cmd("ip route add default via 10.0.6.2 dev r4-eth1 table 5000")
r4.cmd("ip route add 10.0.8.1/24 dev r4-eth2 table 5001")
r4.cmd("ip route add default via 10.0.8.2 dev r4-eth2 table 5001")
r4.cmd("ip rule add from 10.0.1.1 table 5000")
r4.cmd("ip rule add from 10.0.3.1 table 5001")
r4.cmd('sysctl net.ipv4.ip_forward=1')

r5.cmd("ifconfig r5-eth0 10.0.6.2/24")
r5.cmd("ifconfig r5-eth1 10.0.7.1/24")
r5.cmd("ip route add to 10.0.1.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.2.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.3.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.4.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.5.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.6.0/24 via 10.0.6.1")
r5.cmd("ip route add to 10.0.7.0/24 via 10.0.7.2")
r5.cmd("ip route add to 10.0.10.0/24 via 10.0.7.2")
r5.cmd('sysctl net.ipv4.ip_forward=1')

r6.cmd("ifconfig r6-eth0 10.0.8.2/24")
r6.cmd("ifconfig r6-eth1 10.0.9.1/24")
r6.cmd("ip route add to 10.0.1.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.2.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.3.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.4.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.5.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.8.0/24 via 10.0.8.1")
r6.cmd("ip route add to 10.0.9.0/24 via 10.0.9.2")
r6.cmd("ip route add to 10.0.10.0/24 via 10.0.9.2")
r6.cmd('sysctl net.ipv4.ip_forward=1')

r7.cmd("ifconfig r7-eth0 10.0.7.2/24")
r7.cmd("ifconfig r7-eth1 10.0.10.1/24")
r7.cmd("ifconfig r7-eth2 10.0.9.2/24")
r7.cmd('sysctl net.ipv4.ip_forward=1')
r7.cmd("ip route add to 10.0.1.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.2.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.5.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.6.0/24 via 10.0.7.1")
r7.cmd("ip route add to 10.0.7.0/24 via 10.0.7.1")

r7.cmd("ip route add to 10.0.3.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.4.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.8.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.9.0/24 via 10.0.9.1")
r7.cmd("ip route add to 10.0.10.0/24 via 10.0.10.2")
r7.cmd('sysctl net.ipv4.ip_forward=1')

h2.cmd("ifconfig h2-eth0 10.0.10.2/24")
h2.cmd("route add default gw 10.0.10.1")

net.start()

time.sleep(1)
CLI(net)
net.stop()

猜你喜欢

转载自blog.csdn.net/u010643777/article/details/81458140
今日推荐