-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
executable file
·354 lines (300 loc) · 15.5 KB
/
utilities.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!
# This file has utility functions that will be generally useful
import json
import os
import platform
import subprocess
import sys
from typing import List, Tuple
# import application, presentation, session, transport, routes, datalink, physical
import application
import constants
import datalink
import physical
import presentation
import routes
import session
import transport
from constants import ErrorLevels
from constants import OperatingSystems
from constants import type_application_dict, type_presentation_dict, type_session_dict, \
type_transport_dict, type_network_4_dict, type_network_6_dict, type_datalink_dict, \
type_physical_dict
# from nbmdt import SystemDescription
"""
>>> platform.system()
'Linux'
>>>
>>> platform.linux_distribution()
('Ubuntu', '18.04', 'bionic')
>>> platform.dist()
('Ubuntu', '18.04', 'bionic')
>>>
>>> platform.mac_ver()
('', ('', '', ''), '')
>>>
>>> platform.win32_ver()
('', '', '', '')
>>>
>>> platform.mac_ver()
('', ('', '', ''), '')
>>>
"""
class SystemDescription(object):
"""Refer to the OSI stack, for example, at https://en.wikipedia.org/wiki/OSI_model. Objects of this class describe
the system, including devices, datalinks, IPv4 and IPv6 addresses and routes, TCP and UDP, sessions,
presentations, applications. Each of these objects have a test associated with them"""
# Issue 13
# CURRENT = None
def __init__(self, applications: type_application_dict = None,
presentations: type_presentation_dict = None,
sessions: type_session_dict = None,
transports: type_transport_dict = None,
networks_4: type_network_4_dict = None,
networks_6: type_network_6_dict = None,
# interfaces: type_interface_dict = None, # Issue 25
datalinks: type_datalink_dict = None,
physicals: type_physical_dict = None,
# Removed mode - it's not part of the system description, it's how nbmdt processes a system description
configuration_filename: str = None,
system_name: str = platform.node()
) -> None:
"""
Populate a description of the system. Note that this method is
a constructor, and all it does is create a SystemDescription object.
:rtype: None: Constructors don't return anything
:param applications:
:param presentations:
:param sessions:
:param transports:
:param networks_4: IPv4 networks
:param networks_6: IPv6 networks
:param datalinks:
:param configuration_filename:
:param system_name: str The name of this computer
"""
self.applications = applications
self.presentations = presentations
self.sessions = sessions
self.transports = transports # TCP, UDP
self.networks_4 = networks_4 # IPv4
self.networks_6 = networks_6 # IPv6
self.datalinks = datalinks # MAC address
self.physicals = physicals
self.configuration_filename: str = configuration_filename
self.system_name: str = system_name
@classmethod
def discover(cls): # class SystemDescription
"""
:return: SystemDescription this method returns a SystemDescription object based on examining the
current system
"""
print(f"In {cls.__name__}, the type of cls is {type(cls)}.", file=sys.stderr)
# Examine each layer in the protocol stack and discover what's in it
applications: type_application_dict = application.Application.discover()
presentations: type_presentation_dict = presentation.Presentation.discover()
sessions: type_session_dict = session.Session.discover()
transports: constants.type_transport_dict = transport.Transport.discover()
networks_4: constants.type_network_4_dict = routes.IPv4Route.discover()
networks_6: type_network_6_dict = routes.IPv6Route.discover()
datalinks: constants.type_datalink_dict = datalink.DataLink.discover()
physicals: constants.type_physical_dict = physical.Physical.discover()
# cls.__init__(cls,
sd: SystemDescription = SystemDescription (
sessions=sessions,
applications=applications,
presentations=presentations,
transports=transports,
networks_4=networks_4,
networks_6=networks_6,
datalinks=datalinks,
physicals=physicals)
return sd
@classmethod
def system_description_from_file(cls, filename: str) -> 'SystemDescription':
"""
Read a system description from a file and create a SystemDescription object.
I couldn't figure out how to return a SystemDescription object, so I return an object
:param filename:
:return:
"""
if not os.path.isfile(filename):
raise FileNotFoundError(f"The file {filename} does not exist")
with open(filename, "r") as f:
so = json.load(f)
# There might be an issue some day because the output from the json.load is going to be a dictionary of
# dictionaries
return SystemDescription(
applications=so.applications,
presentations=so.presentations,
sessions=so.presentations,
transports=so.transports,
networks_4=so.networks_4,
networks_6=so.networks_6,
datalinks=so.datalinks,
physicals=so.physicals,
configuration_filename=filename,
system_name=so.system_name
)
# Method discover has to be moved out of utilities and put somewhere
# else because discover depends on classes Application, Presentation,
# Session, Transport, Routes, Interfaces, Datalink, and Physical.
# But these classes all depend on module utilities, so there are
# circular dependencies.
print("Log the above comment as a bug", file=sys.stderr)
# method discover was here, but is now moved to nbmdt.py
def file_from_system_description(self, filename: str) -> None:
"""Write a system description to a file
:param filename
:return:
"""
# In the future, detect if a configuration file already exists, and if so, create
# a new version.
with open(filename, "w") as f:
json.dump(self, f)
"""
def __init__(self, configuration_file: str = None, description: Descriptions = None) -> None:
if configuration_file is None:
# This is what the system is currently is
# Create a dictionary, keyed by link name, of the physical interfaces
self.phys_db = interfaces.PhysicalInterface.get_all_physical_interfaces()
# Create a dictionary, keyed by link name, of the logical interfaces, that is, interfaces with addresses
self.data_link_db = interfaces.LogicalInterface.get_all_logical_interfaces()
# Create lists, sorted from smallest netmask to largest netmask of IPv4 and IPv6 routes
self.ipv4_routes = network.IPv4Route.find_ipv4_routes()
self.default_ipv4_gateway : network.IPv4Address = network.IPv4Route.get_default_ipv4_gateway()
self.ipv6_routes = network.IPv6Route.find_ipv6_routes()
self.default_ipv6_gateway : network.IPv6Address = network.IPv6Route.get_default_ipv6_gateway()
# Create something... what? to track transports (OSI layer 4)
self.transports_4 = transports.ipv4
self.transports_6 = transports.ipv6
# Issue 13
if description is None or description == self.Descriptions.CURRENT:
# Assume current
self.description = self.Descriptions.CURRENT
else:
raise NotImplemented("configuration file is not implemented yet")
if not hasattr(self, "IP_COMMAND"):
self.IP_COMMAND = configuration.Configuration.find_executable('ip')
if not hasattr(self, "PING4_COMMAND"):
self.PING4_COMMAND = configuration.Configuration.find_executable('ping')
if not hasattr(self, "PING6_COMMAND"):
self.PING6_COMMAND = configuration.Configuration.find_executable('ping6')
if DEBUG:
import os
assert os.stat.S_IXUSR & os.stat(os.path)[os.stat.ST_MODE]
assert os.stat.S_IXUSR & os.stat(os.path)[os.stat.ST_MODE]
self.applications: dict = {} # For now
# self.name_servers = nameservers.nameservers()
# self.applications = applications
# To find all IPv4 machines on an ethernet, use arp -a See ipv4_neighbors.txt
# To find all IPv6 machines on an ethernet, use ip -6 neigh show
# self.networks = networks
# self.name = name
else:
y = yaml.safe_load(configuration_file)
self.interfaces = y['interfaces']
for interface in self.interfaces:
self.ipv4_address = interfaces
"""
"""
@staticmethod
def describe_current_state():
"This method goes through a system that is nominally configured and operating and records the configuration
"
# applications = Applications.find_applications()
# applications = None
# ipv4_routes = IPv4_route.find_ipv4_routes()
# ipv6_routes = IPv6_route.find_ipv6_routes()
# ipv6_addresses = interfaces.LogicalInterface.find_ipv6_addresses()
# ipv4_addresses = interfaces.LogicalInterface.find_ipv4_addresses()
# networks = Networks.find_networks()
# networks = None
# nominal = SystemDescription.describe_current_state()
pass
# return (applications, ipv4_routes, ipv6_routes, ipv4_addresses, ipv6_addresses, networks)
"""
@property
def __str__(self):
"""This generates a nicely formatted report of the state of this system
This method is probably most useful in BOOT mode.
The classes should be re-written with __str__ methods which are sensitive to the mode
"""
result = "{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}".format(str(self.applications),
str(self.presentations),
str(self.sessions),
str(self.transports),
str(self.networks_4), str(self.networks_6),
str(self.datalinks),
str(self.physicals) )
return result
def nominal(self, filename) -> ErrorLevels:
print(f"The nominal configuration file is going to be {filename}", file=sys.stderr)
self.file_from_system_description(filename)
return ErrorLevels.NORMAL
def boot(self) -> None:
print("Going to test in boot mode", file=sys.stderr)
raise NotImplementedError
def monitor(self, port) -> None:
print(f"going to monitor on port {port}", file=sys.stderr)
raise NotImplementedError
def diagnose(self, filename) -> ErrorLevels:
print(f"In diagnostic mode, nomminal filename is {filename}", file=sys.stderr)
nominal_system: SystemDescription = SystemDescription.system_description_from_file(filename)
print(nominal_system)
return ErrorLevels.NORMAL
def test(self, test_specification) -> None:
print(f"The test_specification is {test_specification}", file=sys.stderr)
raise NotImplementedError
class SystemDescriptionFile(SystemDescription):
"""This class handles interfacing between the program and the configuration file. Thus, when the system is in its
'nominal' state, then that's a good time to write the configuration file. When the state of the system is
questionable, then that's a good time to read the configuration file"""
def __init__(self, configuration_filename):
"""Create a SystemDescription object which has all of the information in a system configuration file"""
system_name="FAKE_NAME"
with open(configuration_filename, "r") as json_fp:
c_dict = json.load(json_fp) # Configuration dictionary
# Issue 31 Instead of raising a KeyError exception, just use None
super(SystemDescriptionFile, self).__init__(applications=c_dict.get("applications"),
presentations=c_dict.get("presentations"),
sessions=c_dict.get("sessions"),
transports=c_dict.get("networks"),
datalinks=c_dict.get("datalinks"),
physicals=c_dict.get("physicals"),
configuration_filename=configuration_filename,
system_name=system_name # for now.
)
self.version = c_dict['version']
self.timestamp = c_dict['timestamp']
def save_state(self, filename):
with open(filename, "w") as json_fp:
json.dump(self, json_fp, ensure_ascii=False)
def compare_state(self, the_other):
"""This method compares the 'nominal' state, which is in self, with another state, 'the_other'. The output is
a dictionary which is keyed by field. The values of the dictionary are a dictionary with three keys:
Comparison.NOMINAL, Comparison.OTHER. The values of these dictonaries will be objects
appropriate for what is being compared. If something is not in Comparison.NOMINAL and not in Comparison.DIFFERENT,
then there is no change.
"""
pass
# Globally note the operating system name. Note that this section of the code *must* follow the definition
# of class OsCliInter or else the compiler will raise a NameError exception at compile time
# Access the_os using utilities.the_os The variable is so named to avoid confusion with the os package name
print("About to import osCliInter", file="sys.stderr")
os_name: str = osclinter.OsCliInter.system.lower()
the_os = constants.OperatingSystems.UNKNOWN
if 'linux' == os_name:
the_os = constants.OperatingSystems.LINUX
elif 'windows' == os_name:
the_os = constants.OperatingSystems.WINDOWS
elif 'darwin' == os_name:
the_os = constants.OperatingSystems.MAC_OS_X
else:
raise ValueError(f"System is {os_name} and I don't recognize it")
if "__main__" == __name__:
print(f"System is {os_name} A.K.A. {the_os}")
if OperatingSystems.LINUX == the_os:
print(f"In linux, the uname -a command output is \n{OsCliInter.run_command(['uname', '-a'])}\n.")
else:
raise NotImplemented("This program ONLY runs on linux at this time")