-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathScanner.py
71 lines (52 loc) · 1.91 KB
/
Scanner.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
import subprocess, re
import asyncio
from aiocoap import *
def scanIps():
ips = set()
result = subprocess.run(['ping6', '-i', '1', '-c', '5', 'ff02::1%lowpan0'], stdout=subprocess.PIPE)
regex = r"\d* bytes from ([0-9,a-f]*::[0-9,a-f]*:[0-9,a-f]*:[0-9,a-f]*:[0-9,a-f]*): icmp_seq=\d* ttl=\d* time=\d*.\d*"
matches = re.finditer(regex, str(result.stdout))
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
ip = match.group(groupNum)
ips.add(ip)
return ips
async def main(ip):
protocol = await Context.create_client_context()
request = Message(code=GET, uri='coap://[' + ip + '%lowpan0]/.well-known/core')
try:
response = await protocol.request(request).response
except Exception as e:
print('Failed to fetch resource:')
print(e)
else:
return response.payload
def writeToHostFans(ip, endpoint):
regex = r"<\/fans\/0>,<\/fans\/1>"
matches = re.finditer(regex, endpoint)
for matchNum, match in enumerate(matches):
matchNum += 1
match = match.group()
if match is not None:
f = open("hostnamesFans.json", "w")
f.write('{ "fans" : "' + ip + '"}')
f.close()
def writeToHostTemperature(ip, endpoint):
regex = r"<\/temperature>"
matches = re.finditer(regex, endpoint)
for matchNum, match in enumerate(matches):
matchNum += 1
match = match.group()
if match is not None:
f = open("hostnamesTemperature.json", "w")
f.write('{ "temperature" : "' + ip + '"}')
f.close()
ipset = scanIps()
print(ipset)
for ip in ipset:
result = asyncio.get_event_loop().run_until_complete(main(ip))
print(str(result) + ' \n')
writeToHostTemperature(ip, str(result))
writeToHostFans(ip, str(result))