-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathdevice.py
63 lines (50 loc) · 2.1 KB
/
device.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
from datetime import datetime
from colors import r, g, n
class Device(object):
def __init__(self, mac, ip, network_name, data={}):
self.mac = mac
self.ip = ip
self.network_name = network_name
self.name = None
self.allowed = None
self.location = None
device_check = self.device_known(data)
if device_check:
self.name = device_check['name']
self.allowed = device_check['allowed']
self.location = device_check['location']
def device_known(self, data):
'''Return a str (given name of the device) or None
Checks whether the device is contained in the Dictonary (based on the mac address)
'''
mac = ''
if self.mac in data:
mac = self.mac
elif self.mac.upper() in data:
mac = self.mac.upper()
if mac:
name = '{} of {}'.format(data[mac]['type'], data[mac]['owner'])
return {'name': name, 'allowed': data[mac]['allowed'], 'location': data[mac]['location']}
return None
def to_list(self):
'''Return a list [mac, ip, network_name, name, location, allowed]
Creates a list of device attributes as colored strings
green: allowed in the network
red: not allowed
'''
if self.allowed:
color = g
else:
color = r
mac = '{}{}{}'.format(color, self.mac, n)
ip = '{}{}{}'.format(color, self.ip, n)
network_name = '{}{}{}'.format(color, self.network_name, n)
name = '{}{}{}'.format(color, self.name, n)
location = '{}{}{}'.format(color, self.location, n)
allowed = '{}{}{}'.format(color, self.allowed, n)
return [mac, ip, network_name, name, location, allowed]
def to_string(self):
'''Return a str
Device information as a string with indentations for the log file
'''
return 'Log: {} \n\t Mac Address: {} \n\t Name in network: {} \n\t Given name: {} \n\t Allowed on network: {}'.format(datetime.now(), self.mac, self.network_name, self.name, self.allowed)