-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchDevices.py
More file actions
53 lines (45 loc) · 1.57 KB
/
Copy pathfetchDevices.py
File metadata and controls
53 lines (45 loc) · 1.57 KB
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
import subprocess
import re
import ipaddress
import socket
from concurrent.futures import ThreadPoolExecutor
def get_ipconfig_subnets():
output = subprocess.check_output("ipconfig", shell=True).decode()
ip_pattern = re.compile(r"IPv4 Address[^\d]*([\d\.]+)")
mask_pattern = re.compile(r"Subnet Mask[^\d]*([\d\.]+)")
ips = ip_pattern.findall(output)
masks = mask_pattern.findall(output)
subnets = []
for ip, mask in zip(ips, masks):
try:
network = ipaddress.IPv4Network(f"{ip}/{mask}", strict=False)
subnets.append(network)
except ValueError:
continue
return subnets, ips # also return local IPs
def scan_ip(ip, port, results, local_ips):
if str(ip) in local_ips: # skip own machine’s IPs
return
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(0.5)
try:
s.connect((str(ip), port))
results.append(str(ip))
except:
pass
def scan_all_subnets(port=1878):
subnets, local_ips = get_ipconfig_subnets()
print(f"Scanning port {port} on these subnets:")
for subnet in subnets:
print(f" - {subnet}")
results = []
with ThreadPoolExecutor(max_workers=100) as executor:
for subnet in subnets:
for ip in subnet.hosts():
executor.submit(scan_ip, ip, port, results, local_ips)
return results
if __name__ == "__main__":
open_ips = scan_all_subnets(1878)
print("\nDevices with port 1878 open:")
for ip in open_ips:
print(f" - {ip}")