|
25 | 25 | exec(code, {'__file__': venv_path})
|
26 | 26 |
|
27 | 27 | from argparse import ArgumentParser
|
| 28 | +import python_hosts |
28 | 29 | import shutil
|
29 | 30 | from dotenv import load_dotenv
|
30 | 31 | from mininet.topo import Topo
|
|
41 | 42 |
|
42 | 43 | PRIVDIR = '/var/priv'
|
43 | 44 |
|
| 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 |
44 | 50 | START_NODE_MANAGERS = False
|
45 | 51 |
|
46 | 52 | # Load environment variables from .env file
|
@@ -275,6 +281,31 @@ def create_topo(my_net):
|
275 | 281 | add_link(my_net, r8, sw)
|
276 | 282 |
|
277 | 283 |
|
| 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 | + |
278 | 309 | def stopAll():
|
279 | 310 | # Clean Mininet emulation environment
|
280 | 311 | os.system('sudo mn -c')
|
@@ -308,7 +339,14 @@ def simpleTest():
|
308 | 339 | for host in net.hosts:
|
309 | 340 | file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
|
310 | 341 |
|
| 342 | + # Add Mininet nodes to /etc/hosts |
| 343 | + add_nodes_to_etc_hosts() |
| 344 | + |
311 | 345 | CLI( net )
|
| 346 | + |
| 347 | + # Remove Mininet nodes from /etc/hosts |
| 348 | + remove_nodes_from_etc_hosts(net) |
| 349 | + |
312 | 350 | net.stop()
|
313 | 351 | stopAll()
|
314 | 352 |
|
|
0 commit comments