-
Notifications
You must be signed in to change notification settings - Fork 0
/
ip.py
54 lines (41 loc) · 1.5 KB
/
ip.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
import subprocess
from PyQt5.QtCore import QCoreApplication, QObject, QRunnable, pyqtSignal, QThread
from multiprocessing import Process
import threading
import os
from re import findall
class ScanIPRange(QThread):
prompt = pyqtSignal(str, bool)
def __init__(self, gateway):
super().__init__()
self.gateway = gateway
def chunk(self, list, n):
return [list[i:i+n] for i in range(0, len(list), n)]
def getIps(self):
return [".".join(self.gateway.split(".")[:3] + [str(i)]) for i in range(2, 255)]
def scanAll(self):
ips = self.getIps()
chunkRange = 10
ipChunk = self.chunk(ips, chunkRange)
for ipList in ipChunk:
for ips in ipList:
x = threading.Thread(target=self.scan, args=(ips,), daemon=True)
x.start()
print(x.isDaemon())
# self.scan(ips)
def scan(self, ip):
if ("nt" in os.name):
res = subprocess.Popen(f"ping -n 3 {ip}", stdout=subprocess.PIPE)
isInclude = len(findall("TTL", str(res.stdout.readlines()))) >= 1
if isInclude:
self.prompt.emit(ip, True)
else:
self.prompt.emit(ip, False)
else:
res = subprocess.call(['ping', '-c', '3', ip])
if res == 0:
self.prompt.emit(ip, True)
elif res == 2:
self.prompt.emit(ip, False)
def run(self):
self.scanAll()