-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-test-blink.py
311 lines (287 loc) · 9.48 KB
/
main-test-blink.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import sys
import os
import time
import re
import subprocess
from multiprocessing import Process
import bluetooth
import random
from scripts.distance import *
os.chdir("/home/kali/ciscn/UAVWarning/stable/")
macDrone = "60:60:1F:49:2D:69"
droneWatchFlag = 1
#channel = "13"
macRedmi = "E6:25:CC:8D:6E:4D"
#channel = "11"
dangerMac = []
fpvMac = []
dangerMacDict = {}
def scan():
excuteCommand("./scripts/mac.sh 3")
print("scan over")
#def detect(mac=mac, channel=channel):
# return excuteCommand("./scripts/detect.sh " + mac + " " + channel)
def initialize():
excuteCommand("./scripts/mac.sh 10")
f = open("./data/mac.safe", "w")
x = [l for l in open("./tmp/mac")]
if not x:
f.close()
return False
for line in x:
print(str(line))
if macDrone in str(line) or "tion" in str(line):
#print(str(line))
continue
else:
f.write(line)
f.close()
return True
#os.popen("rm ./data/mac2.safe")
#os.popen("mv ./data/mac2.safe ./data/mac.safe")
def blinkLED():
resultDrone = {}
for line in open("./tmp/mac.fpv"):
if(len(line) < 2):
continue
sumBlink = 0
sumRSSI = 0
predictall = []
for i in range(3):
#client_sock.send("blink")
lines = 0
channel = line.split(" ")[1]
mac = line.split(" ")[0]
#channel = "6"
#mac = macRedmi
#print("Judging " + mac)
excuteCommand("./scripts/tshark.sh " + mac + " blink " + channel)
#print("one time")
with open('./tmp/res.txt', 'r') as f:
predict = f.read()
print(predict)
sumBlink += float(predict)
predictall.append(float(predict))
distance = getDistance()
print(distance)
'''with open('./tmp/rssi') as f:
for lline in f:
field = lline.strip().split()
second = float(field[0])
if(field[1] != '0'):
lines += 1
sumRSSI += int(field[1])
sumRSSI /= lines'''
#list1 = list1.split(' ')
#for i in range(0, len(list1)):
# list1[i] = list1[i].rstrip('\n')
#predict = "predict:" + list1[0]
#y_predict = "y_predict:" + list1[1]
#print(predict)
#print(y_predict)
#client_sock.send(predict)
#print("RSSI: " + str(sumRSSI))
sumBlink /= 3
print("this time check:" + str(sumBlink) + "\n\n")
#client_sock.send(sumBlink)
return [predictall, sumBlink]
def stayLED():
for line in open("./tmp/mac.fpv"):
channel = line.split(" ")[1]
mac = line.split(" ")[0]
print("Judging " + mac)
excuteCommand("./scripts/tshark.sh " + mac + " stay " + channel)
printBlank()
def filter():
x = [l.split(',')[0] for l in open("./data/mac.safe")]
y = [l.split(',') for l in open("./tmp/mac")]
global dangerMac
dangerMac = []
for item in y:
item[1] = int(item[1])
for i in range(0, len(y)):
if y[i][0] not in x:
dangerMac += y[i]
f = open("./tmp/mac.danger", "w")
if dangerMac:
f = open("./tmp/mac.danger", "w")
for i in range(0, len(dangerMac), 2):
f.write(dangerMac[i] + " " + str(dangerMac[i + 1] ) + '\n')
f.close()
def detect():
global strStatus, fpvMac
dangerMac = [l.split(' ') for l in open("./tmp/mac.danger")]
if dangerMac:
for item in dangerMac:
result = excuteCommand("./scripts/detect.sh " + item[0] + " " + item[1][:-1])
printBlank()
print(item[0] + " -> " + result[:-1])
if not re.search("not", result) and (fpvMac == [] or item[0] not in fpvMac):
fpvMac += item
else:
if item[0] not in dangerMacDict.keys():
dangerMacDict[item[0]] = 1
else:
dangerMacDict[item[0]] += 1
if dangerMacDict[item[0]] > 1:
# print("add mac!!!")
with open("./data/mac.safe", "a+") as f:
f.write(item[0] + ", " + item[1])
f = open("./tmp/mac.fpv", "w")
for i in range(0, len(fpvMac), 2):
f.write(fpvMac[i] + " " + fpvMac[i+1])
data = fpvMac[i] + " " + fpvMac[i+1]
client_sock.send(data)
#print(fpvMac[i] + " " + fpvMac[i+1])
f.close()
def excuteCommand(com):
command = ['/bin/bash'] + com.split()
ex = subprocess.Popen(command, stdout=subprocess.PIPE)
out, err = ex.communicate()
status = ex.wait()
#print("cmd in ", " ".join(command))
#print("cmd out ", out.decode())
try:
return out.decode()
except:
return "excute decode error"
def printBlank():
pass
#print("\r\b\r\b\r\b\r\b\r\b\r\b")
#for i in range(6):
# print(" ")
#print("\r\b\r\b\r\b\r\b\r\b\r\b\r\b")
def locate(flag = 0):
realAngle = 30
ret = []
angle = 0
distence = 3
if(flag == 0):
angle = 90
elif(flag == 1):
seed = random.randint(0,10)
if(seed < 8):
angle = random.randint(90 - realAngle/2 - 5 , 90 - realAngle/2 + 5)
else:
angle = random.randint(90 - realAngle, 90)
else:
seed = random.randint(0,10)
if(seed < 8):
angle = random.randint(90 + realAngle/2 - 5, 90 - realAngle/2 + 5)
else:
angle = random.randint(90, 90 + realAngle)
distence = distence + random.uniform(-0.5, 0.5)
return [distence, angle, 0]
def locateDrone():
#if droneWatchFlag == 0:
# return -1;
for line in open("./tmp/drone.danger"):
channel = line.split(" ")[1]
mac = line.split(" ")[0]
# if predictDrone[mac] == -1:
# continue;
client_sock.send(str(macDrone))
print("locating " + mac)
excuteCommand("./scripts/tshark.sh " + mac + " locate " + channel)
printBlank()
position = locate()
# client_sock.send(position[0])
# client_sock.send(position[1])
# client_sock.send(position[2])
def autoProcess():
'''try:
os.popen("rm -f ./tmp/mac ./data/mac.safe ./tmp/mac.danger ./tmp/mac.fpv")
os.popen("touch ./tmp/mac.fpv ./tmp/mac.danger ./data/mac2.safe")
except:
print("rm and touch error!")
print("Begin to initialize the system(about 10s)")
while(not initialize()):
continue
os.popen("cp ./tmp/mac ./data/mac2.safe")
print("Initialize Over. Begin to Detect")
client_sock.send("start")'''
predictDrone = []
for i in range(20):
'''scan()
#filter()
if dangerMac:
print("Danger Mac: ")
os.system("cat ./tmp/mac.danger")
detect()'''
predictDrone.append(blinkLED())
#print(predictDrone)
#status = client_sock.recv(1024)
#status = str(status, 'utf-8')
#print(status)
#if status == "1":
# locateDrone()
fp = open('./result.txt', "a+", encoding="utf-8")
fp.write(str(predictDrone[-1]) + '\n')
fp.close()
for item in predictDrone:
print(item)
#fp = open('./result.txt', "a+", encoding="utf-8")
#for item in predictDrone:
# fp.write(str(item))
os.system("clear")
print("Welcome to the UAVWarning system!\n")
'''server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "SampleServer", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port)
try:
client_sock, client_info = server_sock.accept()
except:
server_sock.close()
print("Accepted connection from", client_info)
statusMonitor = os.popen(r"iwconfig", "r").read()
if not re.search("Monitor", statusMonitor):
print("Start the Monitor Mode on wlan1")
excuteCommand("airmon-ng check kill")
excuteCommand("airmon-ng start wlan1")
print("Success!")
else:
print("Already in Monitor Mode!\n")
'''
while(1):
#print(os.path.abspath(__file__))
#order = client_sock.recv(1024)
#order = str(order, 'utf-8')
order = "auto"
if order == "scan":
print("Begin to Scan(about 4s)...")
scan()
filter()
if dangerMac:
print("Danger Mac:")
os.system("cat ./tmp/mac.danger")
elif order == "detect":
detect()
elif order == "initialize":
print("Begin to Initialize(about 10s)...")
initialize()
os.popen("cp ./tmp/mac ./data/mac.safe")
print("Initialize Over!")
elif order== "blink":
blinkLED()
print("Blink over")
elif order== "help":
os.system("cat README")
elif order== "stay":
stayLED()
print("Stay Over")
elif order== "exit":
print("Bye~\n")
exit(0)
elif order=="auto":
autoProcess()
else:
print("Unknown command: " + order)
print()