| 1 | == Esempio di topologia pura == |
| 2 | |
| 3 | {{{#!python |
| 4 | from mininet.topo import Topo |
| 5 | |
| 6 | class MyTopo(Topo): |
| 7 | def __init__(self): |
| 8 | Topo.__init__(self) |
| 9 | s1 = self.addSwitch('s1') |
| 10 | s2 = self.addSwitch('s2') |
| 11 | |
| 12 | h = [] |
| 13 | for i in xrange(4): |
| 14 | h.append(self.addHost('h' + str(i+1))) |
| 15 | |
| 16 | self.addLink(s1, h[0]) |
| 17 | self.addLink(s1, h[1]) |
| 18 | self.addLink(s2, h[2]) |
| 19 | self.addLink(s2, h[3]) |
| 20 | self.addLink(s1, s2) |
| 21 | |
| 22 | |
| 23 | topos = { 'prova' : (lambda : MyTopo()) } |
| 24 | |
| 25 | }}} |
| 26 | |
| 27 | Se il file si chiama `lab/mytopo.py`, da eseguire con |
| 28 | |
| 29 | {{{#!sh |
| 30 | sudo mn --custom lab/mytopo.py --topo prova |
| 31 | }}} |
| 32 | |
| 33 | == Esempio con IP e parametri del link variati == |
| 34 | |
| 35 | {{{#!python |
| 36 | from mininet.topo import Topo |
| 37 | from mininet.net import Mininet |
| 38 | from mininet.link import TCLink |
| 39 | from mininet.cli import CLI |
| 40 | |
| 41 | class MyTopo(Topo): |
| 42 | def __init__(self): |
| 43 | Topo.__init__(self) |
| 44 | s1 = self.addSwitch('s1') |
| 45 | s2 = self.addSwitch('s2') |
| 46 | |
| 47 | h = [] |
| 48 | for i in xrange(4): |
| 49 | h.append(self.addHost('h' + str(i+1))) |
| 50 | |
| 51 | self.addLink(s1, h[0]) |
| 52 | self.addLink(s1, h[1]) |
| 53 | self.addLink(s2, h[2]) |
| 54 | self.addLink(s2, h[3]) |
| 55 | self.addLink(s1, s2, bw=10) |
| 56 | |
| 57 | |
| 58 | if __name__ == '__main__': |
| 59 | net = Mininet(topo=MyTopo(), link=TCLink) |
| 60 | h1, h2 = net.get('h1','h2') |
| 61 | h1.setIP('192.168.1.10/24') |
| 62 | h2.setIP('192.168.1.20/24') |
| 63 | |
| 64 | h3, h4 = net.get('h3','h4') |
| 65 | h3.setIP('192.168.2.100/24') |
| 66 | h4.setIP('192.168.2.200/24') |
| 67 | |
| 68 | net.start() |
| 69 | CLI(net) |
| 70 | net.stop() |
| 71 | |
| 72 | }}} |
| 73 | |
| 74 | Se il file si chiama `lab/mytopo.py`, da eseguire con |
| 75 | |
| 76 | {{{#!sh |
| 77 | sudo python lab/mytopo.py |
| 78 | }}} |