-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetsniff.py
87 lines (73 loc) · 2.88 KB
/
netsniff.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
import subprocess as subp
from multiprocessing.pool import ThreadPool
import ipaddress as net
import re
from string import ascii_letters
class Netscanner():
@staticmethod
def arp_command():
process = subp.Popen(["arp", "-a"], stdout=subp.PIPE)
output = str(process.communicate())
return output
@staticmethod
def ping_obj(host):
process = subp.Popen(["ping", "-n", "1", host], stdout=subp.PIPE)
streamdata = process.communicate()
if not 'Reply from {}'.format(host) in str(streamdata):
return False
else:
print(f'Response from {host} received.')
return True
def get_interface_subnet(self):
output = self.arp_command()
interface = re.search('................ *-', output)[0].replace(':', '').replace(' ', '').replace('-', '')
for i in ascii_letters:
interface = interface.replace(i, '')
interface = interface.rpartition('.')[0] + '.0/'
subaddr = input(f'\nPlease enter a Net Mask!'
f'\nDefault mask is 24\n{interface}')
if subaddr == '':
subaddr = '24'
network = interface + subaddr
return network
def arp_dump(self):
ipx = []
print('\n| ARP Cache dump')
output = self.arp_command()
findip = re.findall('............... +..-..-..-..-..-..', output)
for item in findip:
clean = item.replace('e\\r\\n', '').replace('r\\n', '').replace('\\n', '').replace('\\', '')
ips = re.findall('.+ ', clean)
for item in ips:
cleanedip = item.replace(' ', '')
ipx.append(cleanedip)
maclist = re.findall('..-..-..-..-..-..', output)
findmac = ['ff-ff-ff-ff-ff-ff', '01-00-5e-00-00-16']
if findmac[0] in maclist:
indx = maclist.index(findmac[0])
else:
indx = maclist.index(findmac[1])
stopatbc = maclist[:indx]
x = -1
for obj in stopatbc:
x += 1
print(f'IP {ipx[x]} has a MAC of {stopatbc[x].replace("-", ":")}')
def main(self):
network = self.get_interface_subnet()
HOSTLIST = [str(item) for item in net.ip_network(network).hosts()]
print('\n| Pinging...')
pool = ThreadPool(254).imap(self.ping_obj, HOSTLIST)
for item in pool:
pass
def __init__(self):
try:
self.main()
self.arp_dump()
except Exception:
print('\n!Something went wrong!')
self.__init__()
Netscanner()
# use re.search to find subnet of main interface and set that as target subnet
# ask for IP that user is searching for (or a list)
# map IP variables to MAC variables
# if MAC of IP is found in ARP Dump output then print (device is online)