-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnmap.py
56 lines (42 loc) · 1.53 KB
/
nmap.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
#!/usr/bin/env python3
"""
clipi:
Emulate, organize, burn, manage a variety of sbc distributions for Raspberry Pi
Written by Jess Sullivan
@ https://github.com/Jesssullivan/clipi
@ https://transscendsurvival.org/
"""
import subprocess
import socket
from common import common
"""
nmap.py:
control nmap functions to search the local network by MAC address-
Pi devices are matched by OUI bits
"""
class nmap(object):
oui_dict = {'B8:27:EB': 'Pi Device <= 3', 'DC:A6:32': 'Pi Device >= 4'}
@staticmethod
def _search(oui=''):
# find the first two ip quadrants from which to increment:
get_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
get_socket.connect(("8.8.8.8", 80))
ip_quad = str(get_socket.getsockname()[0]).split('.')
get_socket.close()
# execute nmap:
print('\n ...starting search for Pi devices, this may take a while... \n')
cmd = "sudo nmap -sP " \
+ ip_quad[0] + \
"." + ip_quad[1] + ".1.1/24" + \
" | awk '/^Nmap/{ip=$NF}/" + oui + "/{print ip}'"
search = subprocess.Popen(cmd, shell=True).wait()
# todo- parse & sort by wifi module version (3|4);
# - search.stdout.read() ...
@classmethod
def nmap_search(cls):
# just to make sure nmap is available
common.main_install()
print('Uses nmap to find local Pi devices by OUI --> MAC address....')
for oui in nmap.oui_dict.keys():
nmap._search(oui=oui)
print('\n ...search complete. \n')