Skip to content

Instantly share code, notes, and snippets.

@rodolfovalentim
Created December 10, 2019 12:53
Show Gist options
  • Save rodolfovalentim/5a505a52de516354333cd4ea1c309061 to your computer and use it in GitHub Desktop.
Save rodolfovalentim/5a505a52de516354333cd4ea1c309061 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from mininet.topo import Topo
from mininet.net import Mininet
from mininet.node import Node
from mininet.log import setLogLevel, info
from mininet.cli import CLI
class LinuxRouter( Node ):
"A Node with IP forwarding enabled."
def config( self, **params ):
super( LinuxRouter, self).config( **params )
# Enable forwarding on the router
self.cmd( 'sysctl net.ipv4.ip_forward=1' )
def terminate( self ):
self.cmd( 'sysctl net.ipv4.ip_forward=0' )
super( LinuxRouter, self ).terminate()
class NetworkTopo( Topo ):
"A LinuxRouter connecting three IP subnets"
def build( self, **_opts ):
r0 = self.addNode( 'r0', cls=LinuxRouter, ip='192.168.1.1/24')
r1 = self.addNode( 'r1', cls=LinuxRouter, ip='172.16.0.2/24')
r2 = self.addNode( 'r2', cls=LinuxRouter, ip='10.10.0.2/24')
s1, s2, s3, s4, s5 = [ self.addSwitch( s ) for s in ( 's1', 's2', 's3', 's4', 's5' ) ]
# Primeiro conecta-se as interfaces ja criadas, conforme falado em sala de aula
self.addLink( s1, r0, intfName2='r0-eth1',
params2={ 'ip' : '192.108.1.1/24' } ) # for clarity
self.addLink( s2, r1, intfName2='r1-eth1',
params2={ 'ip' : '172.16.0.2/24' } )
self.addLink( s4, r2, intfName2='r1-eth1',
params2={ 'ip' : '10.10.0.2/24' } )
# Logo em seguida, cria-se as outras conexoes
# Para o R0
self.addLink( s2, r0, intfName2='r0-eth2',
params2={ 'ip' : '172.16.0.1/24' } )
self.addLink( s4, r0, intfName2='r0-eth3',
params2={ 'ip' : '10.10.0.1/24' } )
# Para o R1
self.addLink( s3, r1, intfName2='r1-eth2',
params2={ 'ip' : '10.0.0.1/24' } )
self.addLink( s5, r1, intfName2='r1-eth3',
params2={ 'ip' : '10.15.0.1/24' } )
#Para o R2
self.addLink( s5, r2, intfName2='r2-eth2',
params2={ 'ip' : '10.15.0.2/24' } )
h1 = self.addHost( 'h1', ip='192.168.1.100/24',
defaultRoute='via 192.168.1.1' )
h2 = self.addHost( 'h2', ip='172.16.0.100/24',
defaultRoute='via 172.16.0.1' )
h3 = self.addHost( 'h3', ip='10.0.0.100/24',
defaultRoute='via 10.0.0.1' )
for h, s in [ (h1, s1), (h2, s2), (h3, s3) ]:
self.addLink( h, s )
def run():
"Test linux router"
topo = NetworkTopo()
net = Mininet( topo=topo ) # controller is used by s1-s3
net.start()
info( '*** Adicionando rota padrao nos roteadores:\n' )
info( net[ 'r0' ].cmd( 'ip route add default via 10.10.0.2 dev r0-eth3' ) )
info( net[ 'r1' ].cmd( 'ip route add default via 172.16.0.1 dev r1-eth1' ) )
info( net[ 'r2' ].cmd( 'ip route add default via 10.15.0.1 dev r2-eth2' ) )
CLI( net )
net.stop()
if __name__ == '__main__':
setLogLevel( 'info' )
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment