/
methods.py
165 lines (137 loc) · 5.21 KB
/
methods.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
#!/usr/bin/env python3
import re
import json
import collections
import logging
from uuid import getnode as get_mac
from datetime import datetime
from nmap import PortScanner
from requests.exceptions import HTTPError
import netifaces
logger = logging.getLogger('pyfi.methods')
def get_ip():
"""
Find the local IP to be scanned. Utilize netifaces to be platform
independent.
"""
gws = netifaces.gateways()
addresses = netifaces.ifaddresses(gws['default'][netifaces.AF_INET][1])
return addresses[netifaces.AF_INET][0]['addr']
class Scanner(object):
"""
Simplified nmap scanning and retrieval of MAC addresses
"""
def __init__(self, ip=get_ip()):
self.ip = ip
self.nmap = PortScanner()
self.scanned = False
self.unknown = 0
self.known = 0
self.total = 0
self.previous_total = 0
self.previous_mac_vendors = []
self.mac_vendors = None
def scan(self):
self.previous_mac_vendors = self.mac_vendors
self.simple_scan()
self.scanned = True
self.total = self._connections()
self.mac_vendors = self._mac_vendors()
self.known = len(self.mac_vendors)
self.unknown = self.total - self.known
def simple_scan(self):
self.nmap.scan(hosts=self.ip + '/24',
arguments='-n -sn -PE -PA21,23,80,3389')
def _mac_vendors(self):
if not self.scanned:
self.simple_scan()
hosts = self.nmap.all_hosts()
macs = {host: self.nmap[host]['addresses'].get('mac', None)
for host in hosts}
mac_vendors = [(host, mac, vendor) for host in hosts
for mac, vendor in self.nmap[host]['vendor'].items()]
logger.debug("Scanner._mac_vendors() - macs: %s\n mac_vendors: %s"
% (macs, mac_vendors))
# nmap often doesn't produce current device MAC address
if not macs[self.ip]:
mac_vendors.append((self.ip, ":".join(re.findall('..', '%012x'
% get_mac())).upper(), None))
return mac_vendors
def _connections(self):
try:
return int(self.nmap.scanstats()['uphosts'])
except AssertionError:
self.simple_scan()
return self._connections()
def get_mac_vendors_json(self):
if not self.scanned:
self.scan()
return json.dumps(self.mac_vendors)
class Controller(object):
"""
Class for interacting with m2x device with known stream names
"""
def __init__(self, client, devicename, non_numeric_name, *numeric_names):
self.client = client
self.device = self._get_device(devicename)
self.mac_addresses = self._get_stream(non_numeric_name, numeric=False)
self.num_macs, self.num_connects, self.num_unknowns = \
self._get_streams(*numeric_names)
def update_all(self, scanner):
# Perform the scan
scanner.scan()
# If the connections change, update the stream.
if (collections.Counter(scanner.previous_mac_vendors) !=
collections.Counter(scanner.mac_vendors)):
logger.info('Updating stream')
posttime = datetime.now()
values = {
self.mac_addresses.name: [{
'timestamp': posttime,
'value': scanner.get_mac_vendors_json()
}],
self.num_macs.name: [{
'timestamp': posttime,
'value': scanner.known
}],
self.num_unknowns.name: [{
'timestamp': posttime,
'value': scanner.unknown
}],
self.num_connects.name: [{
'timestamp': posttime,
'value': scanner.total
}]
}
logger.debug("Controller.update_all() - values: %s\n "
% values)
self.device.post_updates(values=values)
logger.info('Scan and update complete')
else:
logger.info('Stream update not required')
def _get_device(self, devicename):
try:
device = [d for d in self.client.devices(q=devicename)
if d.name == devicename][0]
except IndexError:
device = self.client.create_device(name=devicename,
description="Local Network"
" Monitor",
visibility="private")
return device
def _get_stream(self, name, numeric=True):
try:
stream = self.device.stream(name)
except HTTPError:
if numeric:
stream = self.device.create_stream(name)
else:
stream = self.device.create_stream(name, type='alphanumeric')
return stream
def _get_streams(self, *names, **kwargs):
streams = []
numeric = kwargs.get('numeric', True)
# Get the stream if it exists, if not create the stream.
for name in names:
streams.append(self._get_stream(name, numeric))
return streams