Skip to content

Commit 7580cea

Browse files
WillowSauceRWillowSauceR
authored andcommitted
fix error when write file
1 parent b1ae6c4 commit 7580cea

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

scan.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ def startThreads():
110110
mp.active_children()[0].terminate()
111111
# time.sleep(1)
112112
# print(scanResultList)
113-
with open(fileName, "w") as file:
114-
file.writelines(scanResultList)
113+
if os.path.exists(fileName):
114+
with open(fileName, "w") as file:
115+
file.writelines(scanResultList)
115116
quietMode = False
116117
print()
117118
log(f"BE Server Count: {scanResult['serverCount']}", info="I", quiet=quietMode)
@@ -129,8 +130,11 @@ def recvPackets(socketSendRecv: socket.socket, verboseMode: str, fileName: list,
129130
quietMode = True
130131
else:
131132
quietMode = False
132-
with open(fileName[1], "r") as file:
133-
scanResultList = file.readlines()
133+
if os.path.exists(fileName[1]):
134+
with open(fileName[1], "r") as file:
135+
scanResultList = file.readlines()
136+
else:
137+
scanResultList = []
134138
while True:
135139
try:
136140
data, addr = socketSendRecv.recvfrom(10240)
@@ -226,8 +230,7 @@ def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
226230
infos[1][3]
227231
except:
228232
return scanResultList
229-
# if addr[0] not in scanResultList[index]:
230-
if fileName[0] == fileName[1]:
233+
if fileName[0] == fileName[1] and addr[0] not in scanResultList[index]:
231234
with open("updated.txt", "a") as file:
232235
pervInfo = scanResultList[index].split(" | ")
233236
file.write(f"{formatedScanResult} Pervious: {pervInfo[2]}:{pervInfo[3]}\n")
@@ -280,4 +283,7 @@ def saveResults(fileName, scanResult, addr, date, infos, scanResultList):
280283
t = threading.Thread(target=startThreads, daemon=True)
281284
t.start()
282285
while mp.active_children():
283-
scanResult, scanResultList = pipe1.recv()
286+
try:
287+
scanResult, scanResultList = pipe1.recv()
288+
except:
289+
pass

send.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ def sendPacket(target: str, port, payloadFile: str, loops, interval):
112112
# print(index)
113113
info = fileContent[contentCount-index-1]
114114
if targetInfo in info:
115-
log("Found target info:")
116-
log(info)
115+
if not scanProcess:
116+
log("Found target info:")
117+
log(info)
117118
targetAddr = info.split(" | ")[2]
118119
port = info.split(" | ")[3]
119120
break
@@ -135,11 +136,14 @@ def sendPacket(target: str, port, payloadFile: str, loops, interval):
135136
scanProcess = False
136137
except:
137138
if isDisplayMotd:
139+
if scanProcess:
140+
if scanProcess.poll() != None:
141+
scanProcess = False
138142
if ":" in target and int(autoScan) == 8:
139143
if not scanProcess:
140144
log(f"Target server offline.")
141145
log(f"Starting refresh ip list...")
142-
scanProcess = subprocess.Popen(["python", "scan.py", targetFileToScan, "10000-21000", "nn", "0", targetFileToScan])
146+
scanProcess = subprocess.Popen(["python", "scan.py", targetFileToScan, "10000-20000", "nn", "0", targetFileToScan])
143147
targetFile = "updated.txt"
144148
else:
145149
log(f"Target server offline.")
@@ -152,10 +156,11 @@ def sendPacket(target: str, port, payloadFile: str, loops, interval):
152156
else:
153157
for line in payloads:
154158
socketSend.sendto(line, (targetAddr, int(port)))
155-
log(f"Loop ", str(i),
156-
" done, used local port: ", str(localPort), "\n")
159+
if not scanProcess:
160+
# if scanProcess.poll() != None:
161+
log(f"Loop {i} done, used local port: {localPort} \n")
157162
except:
158-
log(f"Loop ", str(i), " error! Skip...", "\n")
163+
log(f"Loop {i} error! Skip...\n")
159164
pass
160165
if i+1 < loops:
161166
time.sleep(interval)

0 commit comments

Comments
 (0)