4 natnet.py: Example network with NATs
21 from mininet.topo import Topo
22 from mininet.net import Mininet
23 from mininet.nodelib import NAT
24 from mininet.log import setLogLevel
25 from mininet.cli import CLI
26 from mininet.util import irange
28 class InternetTopo(Topo):
29 "Single switch connected to n hosts."
30 def build(self, n=2, **_kwargs ):
32 inetSwitch = self.addSwitch('s0')
34 inetHost = self.addHost('h0')
35 self.addLink(inetSwitch, inetHost)
38 for i in irange(1, n):
39 inetIntf = 'nat%d-eth0' % i
40 localIntf = 'nat%d-eth1' % i
41 localIP = '192.168.%d.1' % i
42 localSubnet = '192.168.%d.0/24' % i
43 natParams = { 'ip' : '%s/24' % localIP }
45 nat = self.addNode('nat%d' % i, cls=NAT, subnet=localSubnet,
46 inetIntf=inetIntf, localIntf=localIntf)
47 switch = self.addSwitch('s%d' % i)
48 # connect NAT to inet and local switches
49 self.addLink(nat, inetSwitch, intfName1=inetIntf)
50 self.addLink(nat, switch, intfName1=localIntf, params1=natParams)
51 # add host and connect to local switch
52 host = self.addHost('h%d' % i,
53 ip='192.168.%d.100/24' % i,
54 defaultRoute='via %s' % localIP)
55 self.addLink(host, switch)
58 "Create network and run the CLI"
60 net = Mininet(topo=topo)
65 if __name__ == '__main__':