Skip to content

Commit 012cd8a

Browse files
committed
Dynamically add Mininet nodes to /etc/hosts file when the topology is loaded
1 parent d5265ab commit 012cd8a

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

nets/8r-1c-in-band-isis/isis8d.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import os
4+
import python_hosts
45
import shutil
56
from mininet.topo import Topo
67
from mininet.node import Host
@@ -16,6 +17,10 @@
1617

1718
PRIVDIR = '/var/priv'
1819

20+
# Path of the file containing the entries (ip-hostname)
21+
# to be added to /etc/hosts
22+
ETC_HOSTS_FILE = './etc-hosts'
23+
1924
class BaseNode(Host):
2025

2126
def __init__(self, name, *args, **kwargs):
@@ -178,6 +183,27 @@ def create_topo(my_net):
178183
add_link(my_net, controller,r2)
179184

180185

186+
def add_nodes_to_etc_hosts():
187+
# Get /etc/hosts
188+
etc_hosts = python_hosts.hosts.Hosts()
189+
# Import host-ip mapping defined in etc-hosts file
190+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
191+
# Print results
192+
print('*** Added %s entries to /etc/hosts\n' % count['write_result']['total_written'])
193+
194+
195+
def remove_nodes_from_etc_hosts(net):
196+
print('*** Removing entries from /etc/hosts\n')
197+
# Get /etc/hosts
198+
etc_hosts = python_hosts.hosts.Hosts()
199+
for host in net.hosts:
200+
# Remove all the nodes from /etc/hosts
201+
etc_hosts.remove_all_matching(name=str(host))
202+
# Write changes to /etc/hosts
203+
etc_hosts.write()
204+
205+
206+
181207
def stopAll():
182208
# Clean Mininet emulation environment
183209
os.system('sudo mn -c')
@@ -211,7 +237,14 @@ def simpleTest():
211237
for host in net.hosts:
212238
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
213239

240+
# Add Mininet nodes to /etc/hosts
241+
add_nodes_to_etc_hosts()
242+
214243
CLI( net )
244+
245+
# Remove Mininet nodes from /etc/hosts
246+
remove_nodes_from_etc_hosts(net)
247+
215248
net.stop()
216249
stopAll()
217250

nets/8r-1c-out-band-isis/isis8d.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import os
4+
import python_hosts
45
import shutil
56
from mininet.topo import Topo
67
from mininet.node import Host, OVSBridge
@@ -16,6 +17,10 @@
1617

1718
PRIVDIR = '/var/priv'
1819

20+
# Path of the file containing the entries (ip-hostname)
21+
# to be added to /etc/hosts
22+
ETC_HOSTS_FILE = './etc-hosts'
23+
1924
class BaseNode(Host):
2025

2126
def __init__(self, name, *args, **kwargs):
@@ -228,6 +233,26 @@ def create_topo(my_net):
228233
add_link(my_net, r8, sw)
229234

230235

236+
def add_nodes_to_etc_hosts():
237+
# Get /etc/hosts
238+
etc_hosts = python_hosts.hosts.Hosts()
239+
# Import host-ip mapping defined in etc-hosts file
240+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
241+
# Print results
242+
print('*** Added %s entries to /etc/hosts\n' % count['write_result']['total_written'])
243+
244+
245+
def remove_nodes_from_etc_hosts(net):
246+
print('*** Removing entries from /etc/hosts\n')
247+
# Get /etc/hosts
248+
etc_hosts = python_hosts.hosts.Hosts()
249+
for host in net.hosts:
250+
# Remove all the nodes from /etc/hosts
251+
etc_hosts.remove_all_matching(name=str(host))
252+
# Write changes to /etc/hosts
253+
etc_hosts.write()
254+
255+
231256
def stopAll():
232257
# Clean Mininet emulation environment
233258
os.system('sudo mn -c')
@@ -261,7 +286,14 @@ def simpleTest():
261286
for host in net.hosts:
262287
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
263288

289+
# Add Mininet nodes to /etc/hosts
290+
add_nodes_to_etc_hosts()
291+
264292
CLI( net )
293+
294+
# Remove Mininet nodes from /etc/hosts
295+
remove_nodes_from_etc_hosts(net)
296+
265297
net.stop()
266298
stopAll()
267299

nets/8routers-isis-ipv6/isis8d.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import os
4+
import python_hosts
45
import shutil
56
from mininet.topo import Topo
67
from mininet.node import Host
@@ -16,6 +17,10 @@
1617

1718
PRIVDIR = '/var/priv'
1819

20+
# Path of the file containing the entries (ip-hostname)
21+
# to be added to /etc/hosts
22+
ETC_HOSTS_FILE = './etc-hosts'
23+
1924
class BaseNode(Host):
2025

2126
def __init__(self, name, *args, **kwargs):
@@ -174,6 +179,26 @@ def create_topo(my_net):
174179
add_link(my_net, hdc2,r8)
175180

176181

182+
def add_nodes_to_etc_hosts():
183+
# Get /etc/hosts
184+
etc_hosts = python_hosts.hosts.Hosts()
185+
# Import host-ip mapping defined in etc-hosts file
186+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
187+
# Print results
188+
print('*** Added %s entries to /etc/hosts\n' % count['write_result']['total_written'])
189+
190+
191+
def remove_nodes_from_etc_hosts(net):
192+
print('*** Removing entries from /etc/hosts\n')
193+
# Get /etc/hosts
194+
etc_hosts = python_hosts.hosts.Hosts()
195+
for host in net.hosts:
196+
# Remove all the nodes from /etc/hosts
197+
etc_hosts.remove_all_matching(name=str(host))
198+
# Write changes to /etc/hosts
199+
etc_hosts.write()
200+
201+
177202
def stopAll():
178203
# Clean Mininet emulation environment
179204
os.system('sudo mn -c')
@@ -207,7 +232,14 @@ def simpleTest():
207232
for host in net.hosts:
208233
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
209234

235+
# Add Mininet nodes to /etc/hosts
236+
add_nodes_to_etc_hosts()
237+
210238
CLI( net )
239+
240+
# Remove Mininet nodes from /etc/hosts
241+
remove_nodes_from_etc_hosts(net)
242+
211243
net.stop()
212244
stopAll()
213245

nets/8routers/ospf8r.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/python
22

33
import os
4+
import python_hosts
45
import shutil
56
from mininet.topo import Topo
67
from mininet.node import Host
@@ -16,6 +17,10 @@
1617

1718
PRIVDIR = '/var/priv'
1819

20+
# Path of the file containing the entries (ip-hostname)
21+
# to be added to /etc/hosts
22+
ETC_HOSTS_FILE = './etc-hosts'
23+
1924
class BaseNode(Host):
2025

2126
def __init__(self, name, *args, **kwargs):
@@ -163,6 +168,27 @@ def create_topo(my_net):
163168
add_link(h82,r8)
164169
add_link(h83,r8)
165170

171+
172+
def add_nodes_to_etc_hosts():
173+
# Get /etc/hosts
174+
etc_hosts = python_hosts.hosts.Hosts()
175+
# Import host-ip mapping defined in etc-hosts file
176+
count = etc_hosts.import_file(ETC_HOSTS_FILE)
177+
# Print results
178+
print('*** Added %s entries to /etc/hosts\n' % count['write_result']['total_written'])
179+
180+
181+
def remove_nodes_from_etc_hosts(net):
182+
print('*** Removing entries from /etc/hosts\n')
183+
# Get /etc/hosts
184+
etc_hosts = python_hosts.hosts.Hosts()
185+
for host in net.hosts:
186+
# Remove all the nodes from /etc/hosts
187+
etc_hosts.remove_all_matching(name=str(host))
188+
# Write changes to /etc/hosts
189+
etc_hosts.write()
190+
191+
166192
def stopAll():
167193
# Clean Mininet emulation environment
168194
os.system('sudo mn -c')
@@ -196,7 +222,14 @@ def simpleTest():
196222
for host in net.hosts:
197223
file.write("%s %d\n" % (host, extractHostPid( repr(host) )) )
198224

225+
# Add Mininet nodes to /etc/hosts
226+
add_nodes_to_etc_hosts()
227+
199228
CLI( net )
229+
230+
# Remove Mininet nodes from /etc/hosts
231+
remove_nodes_from_etc_hosts(net)
232+
200233
net.stop()
201234
stopAll()
202235

0 commit comments

Comments
 (0)