Skip to content

Commit 7addd22

Browse files
committed
Add Mininet entries to /etc/hosts in 8r-1c-srv6-pm topology
1 parent 11c45c3 commit 7addd22

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

nets/8r-1c-srv6-pm/isis8d.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
exec(code, {'__file__': venv_path})
2626

2727
from argparse import ArgumentParser
28+
import python_hosts
2829
import shutil
2930
from dotenv import load_dotenv
3031
from mininet.topo import Topo
@@ -41,6 +42,11 @@
4142

4243
PRIVDIR = '/var/priv'
4344

45+
# Path of the file containing the entries (ip-hostname)
46+
# to be added to /etc/hosts
47+
ETC_HOSTS_FILE = './etc-hosts'
48+
49+
# Define whether to start the node managers on the routers or not
4450
START_NODE_MANAGERS = False
4551

4652
# Load environment variables from .env file
@@ -275,6 +281,31 @@ def create_topo(my_net):
275281
add_link(my_net, r8, sw)
276282

277283

284+
def add_nodes_to_etc_hosts():
285+
# Get /etc/hosts
286+
etc_hosts = python_hosts.hosts.Hosts()
287+
# Import host-ip mapping defined in etc-hosts file
288+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
289+
# Print results
290+
print('*** Added %s entries to /etc/hosts\n' % count['write_result']['total_written'])
291+
292+
293+
def remove_nodes_from_etc_hosts(net):
294+
print('*** Removing entries from /etc/hosts\n')
295+
# Get /etc/hosts
296+
etc_hosts = python_hosts.hosts.Hosts()
297+
for host in net.hosts:
298+
# Remove all the nodes from /etc/hosts
299+
etc_hosts.remove_all_matching(name=str(host))
300+
# Remove entries related to the management network
301+
# These entries are in the form *.m (e.g. r1.m, controller.m)
302+
# therefore they are not removed during the previous loop
303+
for host in net.hosts:
304+
etc_hosts.remove_all_matching(name='%s.m' % host)
305+
# Write changes to /etc/hosts
306+
etc_hosts.write()
307+
308+
278309
def stopAll():
279310
# Clean Mininet emulation environment
280311
os.system('sudo mn -c')
@@ -308,7 +339,14 @@ def simpleTest():
308339
for host in net.hosts:
309340
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
310341

342+
# Add Mininet nodes to /etc/hosts
343+
add_nodes_to_etc_hosts()
344+
311345
CLI( net )
346+
347+
# Remove Mininet nodes from /etc/hosts
348+
remove_nodes_from_etc_hosts(net)
349+
312350
net.stop()
313351
stopAll()
314352

0 commit comments

Comments
 (0)