Created
April 25, 2025 20:59
-
-
Save jjo/5dce5b04340c77d144960757475dd328 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
## Ejemplo minimo para resolver caso1: solo dos sucursales, | |
## se debe generalizar teniendo por ej suc_count=6 via codigo. | |
## --jjo, 2025-04-25 | |
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') | |
s1wan = net.addSwitch('s1wan', cls=OVSKernelSwitch, failMode='standalone') | |
s2wan = net.addSwitch('s2wan', cls=OVSKernelSwitch, failMode='standalone') | |
s1lan = net.addSwitch('s1lan', cls=OVSKernelSwitch, failMode='standalone') | |
s2lan = net.addSwitch('s2lan', cls=OVSKernelSwitch, failMode='standalone') | |
rc = net.addHost('rc', cls=Node, ip='0.0.0.0') | |
rc.cmd('sysctl -w net.ipv4.ip_forward=1') | |
r1 = net.addHost('r1', cls=Node, ip='0.0.0.0') | |
r1.cmd('sysctl -w net.ipv4.ip_forward=1') | |
r2 = net.addHost('r2', cls=Node, ip='0.0.0.0') | |
r2.cmd('sysctl -w net.ipv4.ip_forward=1') | |
info( '*** Add hosts\n') | |
h1 = net.addHost('h1', cls=Host, ip='10.0.1.100/24', defaultRoute='via 10.0.1.1') | |
h2 = net.addHost('h2', cls=Host, ip='10.0.2.100/24', defaultRoute='via 10.0.2.1') | |
info( '*** Add links\n') | |
net.addLink(rc, s1wan) | |
net.addLink(rc, s2wan) | |
net.addLink(r1, s1lan) | |
net.addLink(r1, s1wan) | |
net.addLink(r2, s2lan) | |
net.addLink(r2, s2wan) | |
net.addLink(h1, s1lan) | |
net.addLink(h2, s2lan) | |
info( '*** Starting network\n') | |
net.build() | |
info( '*** Starting controllers\n') | |
for controller in net.controllers: | |
controller.start() | |
info( '*** Starting switches\n') | |
net.get('s1wan').start([]) | |
net.get('s2wan').start([]) | |
net.get('s1lan').start([]) | |
net.get('s2lan').start([]) | |
info( '*** Post configure switches and hosts\n') | |
# Addressing (+auto routing a los respectivos links) | |
info( '*** [jjo] Address/ando-arrancando\n') | |
r1.cmd('ip a a 10.0.1.1/24 dev r1-eth0') | |
r1.cmd('ip a a 192.168.100.1/29 dev r1-eth1') | |
r2.cmd('ip a a 10.0.2.1/24 dev r2-eth0') | |
r2.cmd('ip a a 192.168.100.9/29 dev r2-eth1') | |
rc.cmd('ip a a 192.168.100.6/29 dev rc-eth0') | |
rc.cmd('ip a a 192.168.100.14/29 dev rc-eth1') | |
# Routing | |
info( '*** [jjo] Rute/ando-ando\n') | |
r1.cmd('ip r a 10.0.0.0/21 via 192.168.100.6') | |
r2.cmd('ip r a 10.0.0.0/21 via 192.168.100.14') | |
rc.cmd('ip r a 10.0.1.0/24 via 192.168.100.1') | |
rc.cmd('ip r a 10.0.2.0/24 via 192.168.100.9') | |
# Testing | |
info( '*** [jjo] Teste/ando h1 <-> h2 ping\n') | |
output = h1.cmd('ping -W0.5 -qc1 10.0.2.100 && echo ==PASS== || echo ==FAIL==') | |
print(output) | |
CLI(net) | |
net.stop() | |
if __name__ == '__main__': | |
setLogLevel( 'info' ) | |
myNetwork() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment