nstack/mininet_topo.py
2021-05-16 11:05:27 -05:00

63 lines
2.2 KiB
Python

from mininet.topo import Topo
from mininet.net import Mininet
from mininet.util import dumpNodeConnections
from mininet.log import setLogLevel
from mininet.cli import CLI
from mininet.nodelib import NAT
class MyTopo(Topo):
def build(self):
switch1 = self.addSwitch('s1')
switch2 = self.addSwitch('s2')
host1 = self.addHost('h1')
host2 = self.addHost('h2')
host3 = self.addHost('h3')
host4 = self.addHost('h4')
host5 = self.addHost('h5')
host6 = self.addHost('h6')
nat0 = self.addHost('nat0', cls=NAT, subnet='10.1.0.0/16', inNamespace=False)
self.addLink(host1,switch1)
self.addLink(host2,switch1)
self.addLink(host3,switch1)
self.addLink(host4,switch1)
self.addLink(host1,switch2)
self.addLink(host5,switch2)
self.addLink(host6,switch2)
self.addLink(nat0,switch1)
def simpleTest():
topo = MyTopo()
net = Mininet(topo,ipBase="10.1.0.0/16")
net.start()
net['h1'].sendCmd("ip a del 10.1.0.1 dev h1-eth0")
net['h2'].sendCmd("ip a del 10.1.0.2 dev h2-eth0")
net['h3'].setIP("10.1.0.3",24,'h3-eth0')
net['h4'].setIP("10.1.0.4",24,'h4-eth0')
net['h5'].setIP("10.1.1.2",24,'h5-eth0')
net['h6'].setIP("10.1.1.3",24,'h6-eth0')
net['h2'].waitOutput()
net['h2'].sendCmd("route add -net 10.1.1.0 netmask 255.255.255.0 gw 10.1.0.1")
net['h3'].sendCmd("route add -net 10.1.1.0 netmask 255.255.255.0 gw 10.1.0.1")
net['h4'].sendCmd("route add -net 10.1.1.0 netmask 255.255.255.0 gw 10.1.0.1")
net['h5'].sendCmd("route add -net 10.1.0.0 netmask 255.255.255.0 gw 10.1.1.1")
net['h6'].sendCmd("route add -net 10.1.0.0 netmask 255.255.255.0 gw 10.1.1.1")
net['h1'].waitOutput()
net['h1'].sendCmd("kitty &")
net['h1'].waitOutput()
net['h2'].waitOutput()
net['h3'].waitOutput()
net['h4'].waitOutput()
net['h5'].waitOutput()
net['h6'].waitOutput()
net['h3'].setDefaultRoute('via 10.1.0.7')
net['h4'].setDefaultRoute('via 10.1.1.1')
net['h5'].setDefaultRoute('via 10.1.1.1')
net['h6'].setDefaultRoute('via 10.1.1.1')
CLI(net)
net.stop()
if __name__ == '__main__':
# Tell mininet to print useful information
setLogLevel('info')
simpleTest()