-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
189 lines (132 loc) · 6.05 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from __future__ import print_function
import os
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
from mininet.link import Intf
from mininet.node import Controller
import networkx as nx
import matplotlib.pyplot as plt
class NetworkTopoPythonAPI(Topo):
def build(self, **opts):
s1 = self.addSwitch('s1', failMode='standalone')
s2 = self.addSwitch('s2', failMode='standalone')
# Adding hosts
d1 = self.addHost('d1', ip='192.168.0.1/28')
d2 = self.addHost('d2', ip='192.168.0.2/28')
d3 = self.addHost('d3', ip='192.168.0.3/28')
d4 = self.addHost('d4', ip='192.168.0.9/28')
d5 = self.addHost('d5', ip='192.168.0.10/28')
d6 = self.addHost('d6', ip='192.168.0.11/28')
d7 = self.addHost('d7', ip='192.168.0.12/28')
self.addLink(d7, s2)
# Connecting hosts to switches
for d, s in [(d1, s1), (d2, s1), (d3, s1)]:
self.addLink(d, s)
for d, s in [(d4, s1), (d5, s1), (d6, s1)]:
self.addLink(d, s)
class NetworkTopo(Topo):
# Builds network topology
def build(self, **_opts):
choice = 0
switch_list = []
while choice != 5:
print("\nWhat do you want to do?\n"
"Add switch? Type 1\n"
"Add hosts? Type 2\n"
"Add link between switch? Type 3\n"
"Visualize topology? Type 4\n"
"Exit? Type 5\n")
choice = int(input("Choice: "))
if choice == 1:
switch_name = input("Input name of switch: ")
switch_list.append([self.addSwitch(f"{switch_name}", failMode='standalone'), 0])
if choice == 2:
count_of_hosts = 0
while count_of_hosts == 0:
hosts = int(input("How many hosts do you want to add? range between 1-254: "))
if 1 <= hosts <= 254:
count_of_hosts = hosts
for i, switch in enumerate(switch_list):
print(f"Index: [{i}] - Switch Name and hosts: {switch}")
user_switch_choice = int(input("Type index of switch are you interested in: "))
user_switch_hosts = switch_list[user_switch_choice][1]
user_switch = switch_list[user_switch_choice][0]
if count_of_hosts + user_switch_hosts >= 255:
print("Sorry but we cant add more hosts to this IP")
else:
switch_list[user_switch_choice][1] = int(count_of_hosts) + int(user_switch_hosts)
hosts_ip = [f"192.168.0.{x}/28" for x in range(count_of_hosts)]
hosts_list = [self.addHost(f"{str(user_switch)}_d{index}", ip=f"{ip}") for index, ip in enumerate(hosts_ip)]
for h in hosts_list:
self.addLink(user_switch, h)
print(f"\nLinked {count_of_hosts} hosts to switch '{user_switch}'")
if choice == 3:
for i, switch in enumerate(switch_list):
print(f"Index: [{i}] - Switch Name and hosts: {switch}")
switch_1 = switch_list[int(input("Type index of switch 1 are you interested in: "))][0]
switch_2 = switch_list[int(input("Type index of switch 2 are you interested in:"))][0]
self.addLink(switch_1, switch_2)
print(f"Added link between {switch_1} and {switch_2}")
if choice == 4:
topo_links = self.links()
graph = nx.Graph()
graph.add_edges_from(topo_links)
# Saving graph to file
file_name = input("Type filename: ")
nx.draw(graph, with_labels=True)
plt.savefig(f"{file_name}.jpg", dpi=300)
def run():
choice = int(input("If you want to run our CLI type 1\n"
"If you want to run created topology by Python API type 2\n"
"If you want to put Mininet Dump and Mininet Links type 3\n"))
if choice == 1:
topo = NetworkTopo()
if choice == 2:
topo = NetworkTopoPythonAPI()
choice = input("Visualize topology to jpg? (y/n): ")
if choice.lower() == "y":
# Exporting mininet topology to list of edges by .links() function, and creating it in NetworkX
# https://github.com/bigswitch/mininet/blob/master/mininet/topo.py
topo_links = topo.links()
print(topo_links)
graph = nx.Graph()
graph.add_edges_from(topo_links)
# Saving graph to file
file_name = input("Type filename: ")
nx.draw(graph, with_labels=True)
plt.savefig(f"{file_name}.jpg", dpi=300)
if choice == 3:
with open("MininetLinks.txt", mode="r") as file:
mininet_raw_links = [line.rsplit() for line in file]
if mininet_raw_links is None:
print("Please paste 'mininet links' from mininet into Mininetlinks.txt")
return None
mininet_links = []
# for link in mininet_raw_links:
for raw_link in mininet_raw_links:
link = raw_link[0].split("<->")
e1 = link[0].split('-')[0]
e2 = link[1].split('-')[0]
mininet_links.append((e1, e2))
# print(mininet_links)
graph = nx.Graph()
graph.add_edges_from(mininet_links)
# Saving graph to file
file_name = input("Type filename: ")
nx.draw(graph, with_labels=True)
plt.savefig(f"{file_name}.jpg", dpi=500)
# To run Mininet CLI uncomment below code.
# net = Mininet(topo=topo, controller=None)
# net.start()
# Make Switch act like a normal switch
# net['s1'].cmd('ovs-ofctl add-flow s1 action=normal')
# Make Switch act like a hub
# net['s1'].cmd('ovs-ofctl add-flow s1 action=flood')
# CLI(net)
# net.stop()
if __name__ == '__main__':
setLogLevel('info')
run()