mirror of
https://github.com/edoardoColi/Communication_Sandbox.git
synced 2026-10-20 21:02:43 +00:00
Mininet Updates
This commit is contained in:
+63
-86
@@ -6,7 +6,6 @@ from mininet.node import Node
|
|||||||
from mininet.cli import CLI
|
from mininet.cli import CLI
|
||||||
from mininet.link import TCLink
|
from mininet.link import TCLink
|
||||||
from mininet.log import setLogLevel
|
from mininet.log import setLogLevel
|
||||||
from copy import copy #TODO verify/temporaneo
|
|
||||||
|
|
||||||
class MyRouter (Node):
|
class MyRouter (Node):
|
||||||
def config(self, **params):
|
def config(self, **params):
|
||||||
@@ -18,110 +17,88 @@ class MyRouter (Node):
|
|||||||
|
|
||||||
def build_topology(config_file):
|
def build_topology(config_file):
|
||||||
topo = Topo()
|
topo = Topo()
|
||||||
elementsH = {} # Dictionary to store nodes
|
elements = {} # Dictionary to store nodes
|
||||||
elementsR = {} # Dictionary to store nodes
|
|
||||||
elementsS = {} # Dictionary to store nodes
|
|
||||||
|
|
||||||
h1 = topo.addHost('h1', ip='161.46.247.131/26', defaultRoute='via 161.46.247.129')
|
# elements['Internet'] = topo.addNode('Internet', cls=MyRouter, ip='0.0.0.0/0')
|
||||||
h2 = topo.addHost('h2', ip='161.46.247.196/27', defaultRoute='via 161.46.247.195')
|
with open(config_file, 'r') as file:
|
||||||
|
for line in file:
|
||||||
|
line = line.strip()
|
||||||
|
if line.startswith('#'): #Skip comment
|
||||||
|
continue
|
||||||
|
parts = line.split(" ") #Parse the topology file using spaces
|
||||||
|
if parts[0] == 'host': #Parse hosts
|
||||||
|
host_name = parts[1]
|
||||||
|
host_ip = parts[2]
|
||||||
|
host_nexthop = 'via ' + parts[3]
|
||||||
|
elements[host_name] = topo.addHost(host_name, ip=host_ip, defaultRoute=host_nexthop)
|
||||||
|
|
||||||
r1 = topo.addNode('r1', cls=MyRouter, ip='161.46.247.129/26')
|
elif parts[0] == 'router': #Parse routers
|
||||||
|
router_name = parts[1]
|
||||||
|
router_ip = parts[2]
|
||||||
|
elements[router_name] = topo.addNode(router_name, cls=MyRouter, ip=router_ip)
|
||||||
|
|
||||||
topo.addLink(h1, r1, intfName2='R23', params2={'ip' : '161.46.247.129/26'})
|
elif parts[0] == 'switch': #Parse switches
|
||||||
topo.addLink(h2, r1, intfName2='R22', params2={'ip' : '161.46.247.195/27'})
|
switch_name = parts[1]
|
||||||
|
elements[switch_name] = topo.addSwitch(switch_name)
|
||||||
|
|
||||||
# with open(config_file, 'r') as file:
|
elif parts[0] == 'linkRR': #Parse links routers to routers
|
||||||
# for line in file:
|
router1 = parts[1]
|
||||||
# line = line.strip()
|
router1_intfName = parts[2]
|
||||||
# if line.startswith('#'): #Skip comment
|
router1_intfIP = parts[3]
|
||||||
# continue
|
router2 = parts[4]
|
||||||
|
router2_intfName = parts[5]
|
||||||
|
router2_intfIP = parts[6]
|
||||||
|
topo.addLink(elements.get(router1), elements.get(router2), intfName1=router1_intfName, intfName2=router2_intfName, params1={'ip' : router1_intfIP}, params2={'ip' : router2_intfIP})
|
||||||
|
|
||||||
# parts = line.split(" ") #Parse the topology file using spaces
|
elif parts[0] == 'linkRH': #Parse links routers to hosts
|
||||||
# if parts[0] == 'host': #Parse hosts
|
host = parts[1]
|
||||||
# host_name = parts[1]
|
host_intfName = parts[2]
|
||||||
# host_ip = parts[2]
|
router = parts[3]
|
||||||
# host_nexthop = 'via ' + parts[3]
|
router_intfName = parts[4]
|
||||||
# host = topo.addHost(host_name, ip=host_ip, defaultRoute=host_nexthop)
|
router_intfIP = parts[5]
|
||||||
# host_c = copy(host)
|
topo.addLink(elements.get(host), elements.get(router), intfName1=host_intfName, intfName2=router_intfName, params2={'ip' : router_intfIP})
|
||||||
# elementsH[host_name] = (host_c)
|
|
||||||
|
|
||||||
# elif parts[0] == 'router': #Parse routers
|
elif parts[0] == 'linkRS': #Parse links routers to switches
|
||||||
# router_name = parts[1]
|
node1 = parts[1]
|
||||||
# router_ip = parts[2]
|
node2 = parts[2]
|
||||||
# router = topo.addNode(router_name, cls=MyRouter, ip=router_ip)
|
# topo.addLink(node1, node2)
|
||||||
# router_c = copy(router)
|
|
||||||
# elementsR[router_name] = (router_c)
|
|
||||||
|
|
||||||
# elif parts[0] == 'switch': #Parse switches
|
elif parts[0] == 'linkSS': #Parse links switches to switches
|
||||||
# switch_name = parts[1]
|
node1 = parts[1]
|
||||||
# switch = topo.addSwitch(switch_name)
|
node2 = parts[2]
|
||||||
# switch_c = copy(switch)
|
# topo.addLink(node1, node2)
|
||||||
# elementsS[switch_name] = (switch_c)
|
|
||||||
|
|
||||||
# elif parts[0] == 'linkSH': #Parse links switches to hosts
|
elif parts[0] == 'linkSH': #Parse links switches to hosts
|
||||||
# node1 = parts[1]
|
node1 = parts[1]
|
||||||
# node2 = parts[2]
|
node2 = parts[2]
|
||||||
# # topo.addLink(node1, node2)
|
# topo.addLink(node1, node2)
|
||||||
|
|
||||||
# elif parts[0] == 'linkSS': #Parse links switches to switches
|
|
||||||
# node1 = parts[1]
|
|
||||||
# node2 = parts[2]
|
|
||||||
# # topo.addLink(node1, node2)
|
|
||||||
|
|
||||||
# elif parts[0] == 'linkHR': #Parse links hosts to routers
|
|
||||||
# node1 = parts[1]
|
|
||||||
# node2 = parts[2]
|
|
||||||
# intf_name2 = parts[3]
|
|
||||||
# n2_ip = parts[4]
|
|
||||||
# topo.addLink(elementsH.get(node1), elementsR.get(node2), intfName2=intf_name2, params2={'ip' : n2_ip})
|
|
||||||
|
|
||||||
# elif parts[0] == 'linkRR': #Parse links routers to routers
|
|
||||||
# node1 = parts[1]
|
|
||||||
# node2 = parts[2]
|
|
||||||
# intf_name1 = parts[3]
|
|
||||||
# intf_name2 = parts[4]
|
|
||||||
# ip1 = parts[5]
|
|
||||||
# ip2 = parts[6]
|
|
||||||
# # topo.addLink(node1, node2, intfName1=intf_name1, intfName2=intf_name2, params1={'ip' : ip1}, params2={'ip' : ip2})
|
|
||||||
|
|
||||||
# # elif parts[0] == 'route': #Parse routing tables
|
|
||||||
# # name = parts[1]
|
|
||||||
# # for node in topo.nodes():
|
|
||||||
# # if node == name:
|
|
||||||
# # (node).cmd('ip route add 0.0.0.0/0 via 10.0.0.1 dev r1-eth2')
|
|
||||||
# # break
|
|
||||||
|
|
||||||
return topo
|
return topo
|
||||||
|
|
||||||
def run_topology(config_file):
|
def run_topology(config_file):
|
||||||
setLogLevel('info') #Different logging levels are 'info' 'warning' 'error' 'debug'
|
setLogLevel('info') #Different logging levels are 'info' 'warning' 'error' 'debug'
|
||||||
topo = build_topology(config_file)
|
topo = build_topology(config_file)
|
||||||
net = Mininet(topo=topo, link=TCLink)
|
net = Mininet(topo=topo, link=TCLink)
|
||||||
net.start() #Starting the network
|
net.start() #Starting the network
|
||||||
|
with open(config_file, 'r') as file: #Search in the configuration file for routing table
|
||||||
# net.addLink(h1, r1, intfName2='R23', params2={'ip' : '161.46.247.129/26'})
|
for line in file:
|
||||||
# net.addLink(h2, r1, intfName2='R22', params2={'ip' : '161.46.247.195/27'})
|
line = line.strip()
|
||||||
|
if line.startswith('#'): #Skip comment
|
||||||
# with open(config_file, 'r') as file: #Search in the configuration file for routing table
|
continue
|
||||||
# for line in file:
|
parts = line.split(" ")
|
||||||
# line = line.strip()
|
if parts[0] == 'route': #Parse routing tables
|
||||||
# if line.startswith('#'): #Skip comment
|
name = parts[1]
|
||||||
# continue
|
pck_src = parts[2]
|
||||||
# parts = line.split(" ")
|
pck_nexthop = parts[3]
|
||||||
# if parts[0] == 'route': #Parse routing tables
|
interf = parts[4]
|
||||||
# name = parts[1]
|
cmd = 'ip route add ' + pck_src + ' via ' + pck_nexthop + ' dev ' + interf
|
||||||
# pck_src = parts[2]
|
(net.getNodeByName(name)).cmd(cmd)
|
||||||
# pck_nexthop = parts[3]
|
|
||||||
# interf = parts[4]
|
|
||||||
# cmd = 'ip route add ' + pck_src + ' via ' + pck_nexthop + ' dev ' + interf
|
|
||||||
# (net.getNodeByName(name)).cmd(cmd)
|
|
||||||
|
|
||||||
if net.pingAll():
|
if net.pingAll():
|
||||||
print(Fore.RED + "Network has issues" + Style.RESET_ALL)
|
print(Fore.RED + "Network has issues" + Style.RESET_ALL)
|
||||||
else:
|
else:
|
||||||
print(Fore.GREEN + "Network working properly" + Style.RESET_ALL)
|
print(Fore.GREEN + "Network working properly" + Style.RESET_ALL)
|
||||||
CLI(net)
|
CLI(net)
|
||||||
net.stop() #Stopping the network
|
net.stop() #Stopping the network
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
run_topology('MininetTopo.conf')
|
run_topology('MininetTopo.conf')
|
||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import configparser
|
||||||
|
from colorama import Fore, Style
|
||||||
|
from mininet.net import Mininet
|
||||||
|
from mininet.topo import Topo
|
||||||
|
from mininet.node import Node
|
||||||
|
from mininet.cli import CLI
|
||||||
|
from mininet.link import TCLink
|
||||||
|
from mininet.log import setLogLevel
|
||||||
|
|
||||||
|
class MyRouter (Node):
|
||||||
|
def config(self, **params):
|
||||||
|
super(MyRouter, self).config(**params)
|
||||||
|
self.cmd('sysctl net.ipv4.ip_forward=1') #Enable forwarding on the router
|
||||||
|
def terminate(self):
|
||||||
|
self.cmd('sysctl net.ipv4.ip_forward=0') #Disable forwarding on the router
|
||||||
|
super(MyRouter, self).terminate
|
||||||
|
|
||||||
|
def build_topology(config_file):
|
||||||
|
topo = Topo()
|
||||||
|
elements = {} # Dictionary to store nodes
|
||||||
|
|
||||||
|
h4 = topo.addHost('H4', ip='161.46.247.131/26', defaultRoute='via 161.46.247.129')
|
||||||
|
h3 = topo.addHost('H3', ip='161.46.247.196/27', defaultRoute='via 161.46.247.195')
|
||||||
|
|
||||||
|
r1 = topo.addNode('R1', cls=MyRouter, ip='161.46.247.254/30')
|
||||||
|
r2 = topo.addNode('R2', cls=MyRouter, ip='161.46.247.253/30')
|
||||||
|
|
||||||
|
# Ordine importante
|
||||||
|
topo.addLink(r2, r1, intfName2='R13', intfName1='R21', params2={'ip' : '161.46.247.254/30'}, params1={'ip' : '161.46.247.253/30'})
|
||||||
|
|
||||||
|
topo.addLink(r2, h3, intfName2='H31', intfName1='R22', params1={'ip' : '161.46.247.195/27'})
|
||||||
|
topo.addLink(r2, h4, intfName2='H41', intfName1='R23', params1={'ip' : '161.46.247.129/26'})
|
||||||
|
|
||||||
|
return topo
|
||||||
|
|
||||||
|
def run_topology(config_file):
|
||||||
|
setLogLevel('info') #Different logging levels are 'info' 'warning' 'error' 'debug'
|
||||||
|
topo = build_topology(config_file)
|
||||||
|
net = Mininet(topo=topo, link=TCLink)
|
||||||
|
net.start() #Starting the network
|
||||||
|
|
||||||
|
(net.getNodeByName('R1')).cmd('ip route add 161.46.247.192/27 via 161.46.247.254 dev R13')
|
||||||
|
(net.getNodeByName('R1')).cmd('ip route add 161.46.247.128/26 via 161.46.247.254 dev R13')
|
||||||
|
|
||||||
|
if net.pingAll():
|
||||||
|
print(Fore.RED + "Network has issues" + Style.RESET_ALL)
|
||||||
|
else:
|
||||||
|
print(Fore.GREEN + "Network working properly" + Style.RESET_ALL)
|
||||||
|
CLI(net)
|
||||||
|
net.stop() #Stopping the network
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
run_topology('MininetTopo.conf')
|
||||||
+42
-35
@@ -1,48 +1,55 @@
|
|||||||
######## Set hosts ########
|
# L'ordine deve rimanee questo
|
||||||
## host name ip defRoute ##
|
|
||||||
###########################
|
|
||||||
host h1 161.46.247.131/26 161.46.247.129
|
|
||||||
host h2 161.46.247.196/27 161.46.247.195
|
|
||||||
|
|
||||||
#### Set routers ###
|
############# Set hosts #############
|
||||||
## router name ip ##
|
## host name intfIP/mask defRoute ###
|
||||||
####################
|
#####################################
|
||||||
router r1 161.46.247.129/26
|
host H4 161.46.247.131/26 161.46.247.129
|
||||||
#router r2 10.2.0.1/24
|
host H3 161.46.247.196/27 161.46.247.195
|
||||||
|
|
||||||
|
######## Set routers ########
|
||||||
|
## router name intfIP/mask ##
|
||||||
|
#############################
|
||||||
|
router R1 161.46.247.254/30
|
||||||
|
router R2 161.46.247.253/30
|
||||||
|
|
||||||
## Set switches ##
|
## Set switches ##
|
||||||
## switch name ##
|
## switch name ##
|
||||||
##################
|
##################
|
||||||
# switch s1
|
# switch s1
|
||||||
# switch s2
|
# switch s2
|
||||||
|
|
||||||
####### Set links #######
|
################################################ Set links ################################################
|
||||||
|
## linkRR router1, router1_intfName, router1_intfIP/mask, router2, router2_intfName, router2_intfIP/mask ##
|
||||||
|
###########################################################################################################
|
||||||
|
linkRR R1 R13 161.46.247.254/30 R2 R21 161.46.247.253/30
|
||||||
|
|
||||||
|
######################## Set links and router interfaces #####################
|
||||||
|
## linkRH host, host_intfName, router, router_intfName, router_intfIP/mask ###
|
||||||
|
##############################################################################
|
||||||
|
linkRH H3 H31 R2 R22 161.46.247.195/27
|
||||||
|
linkRH H4 H41 R2 R23 161.46.247.129/26
|
||||||
|
|
||||||
|
##################### Set links and router interfaces #####################
|
||||||
|
## linkRS ##
|
||||||
|
#####################################################
|
||||||
|
# linkRS
|
||||||
|
# linkRS
|
||||||
|
|
||||||
|
###### Set links ######
|
||||||
|
## linkSS switch1, switch2 ##
|
||||||
|
######################
|
||||||
|
# linkSS s2 s1
|
||||||
|
|
||||||
|
###### Set links ######
|
||||||
## linkSH switch, host ##
|
## linkSH switch, host ##
|
||||||
#########################
|
##########
|
||||||
# linkSH s1 h1
|
# linkSH s1 h1
|
||||||
# linkSH s1 h2
|
# linkSH s1 h2
|
||||||
# linkSH s2 h3
|
# linkSH s2 h3
|
||||||
# linkSH s2 h4
|
# linkSH s2 h4
|
||||||
|
|
||||||
######### Set links #########
|
################## route table ################
|
||||||
## linkSS switch1, switch2 ##
|
## route name final_destIP/mask nextHop intf ###
|
||||||
#############################
|
################################################
|
||||||
# linkSS s2 s1
|
route R1 161.46.247.192/27 161.46.247.254 R13
|
||||||
|
route R1 161.46.247.128/26 161.46.247.254 R13
|
||||||
|
|
||||||
##################### Set links #####################
|
|
||||||
## linkHR host, router, router_intfName, router_ip ##
|
|
||||||
#####################################################
|
|
||||||
linkHR h1 r1 R23 161.46.247.129/26
|
|
||||||
linkHR h2 r1 R22 161.46.247.195/27
|
|
||||||
|
|
||||||
######################### Set links #########################
|
|
||||||
## linkRR router1, router2, intfName1, intfName2, ip1, ip2 ##
|
|
||||||
#############################################################
|
|
||||||
# linkRR r1 r2 r1-eth2 r2-eth2 10.0.0.0/31 10.0.0.1/31
|
|
||||||
|
|
||||||
############# route table #############
|
|
||||||
## route name source destRoute interf ##
|
|
||||||
########################################
|
|
||||||
# route r1 0.0.0.0/0 10.0.0.1 r1-eth2
|
|
||||||
# route r2 0.0.0.0/0 10.0.0.0 r2-eth2
|
|
||||||
|
|||||||
Reference in New Issue
Block a user