-
Notifications
You must be signed in to change notification settings - Fork 1
/
def_func.py
69 lines (50 loc) · 2.53 KB
/
def_func.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
import numpy as np
from PIL import Image
import subprocess
from math import ceil, sqrt
from concurrent.futures import ThreadPoolExecutor, as_completed
def ping_ip(ip):
"""Ping a single IP address and return the result."""
print(f"Pinging {ip}") # Print the IP being pinged
# Use subprocess to call the system ping command
try:
output = subprocess.run(["ping", "-c", "1", "-W", "5", ip], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return 1 if output.returncode == 0 else 0 # Return 1 for success, 0 for failure
except Exception as e:
print(f"Error pinging {ip}: {e}")
return 0 # Return 0 for failure in case of an exception
def ping_ip_range(start_ip, end_ip):
# Convert IP addresses to integers
start = ip_to_int(start_ip)
end = ip_to_int(end_ip)
# Create a list to hold the IP addresses to ping
ip_list = [int_to_ip(ip_int) for ip_int in range(start, end + 1)]
results = [0] * len(ip_list) # Preallocate results list
# Use ThreadPoolExecutor to ping IPs concurrently
with ThreadPoolExecutor(max_workers=100) as executor:
future_to_index = {executor.submit(ping_ip, ip): index for index, ip in enumerate(ip_list)}
for future in as_completed(future_to_index):
# Multithreading without having a race condition softwa
index = future_to_index[future] # Get the index of the completed future
result = future.result()
results[index] = result # Write the result to the correct index
return results
def ip_to_int(ip):
return sum([int(x) << (8 * (3 - i)) for i, x in enumerate(ip.split('.'))])
def int_to_ip(ip_int):
return '.'.join([str((ip_int >> (8 * (3 - i))) & 0xFF) for i in range(4)])
def create_image(results, width, img_format):
if img_format == "S":
height = width = ceil(sqrt(len(results)))
else:
height = len(results) // width + (1 if len(results) % width > 0 else 0)
image_data = np.zeros((height, width), dtype=np.uint8)
for i, result in enumerate(results):
image_data[i // width, i % width] = 255 if result == 1 else 0 # White for success, black for failure
# If the last row is not completely filled, fill it with black pixels
if len(results) % width != 0:
image_data[-1, len(results) % width:] = 0
image = Image.fromarray(image_data, 'L') # 'L' mode for grayscale
return image
def scale_image(image, scale): # Scale the image
return image.resize((image.width * scale, image.height * scale), resample=Image.BOX)