Useful Python Tricks

Photo by me

1
2
>>> print("-"*20)
--------------------

Scan ip address in LAN

Get from CHATGPT

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
import socket, struct

# Define the IP range to scan
ip_range = '192.168.1.'
start_ip = 1
end_ip = 255

# Create an empty list to store available IPs
available_ips = []

# Loop through the range of IP addresses and attempt to connect to them
for i in range(start_ip, end_ip+1):
ip = ip_range + str(i)
try:
socket.inet_aton(ip)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1) # Set the socket timeout
s.connect((ip, 22)) # Attempt to connect to port 20 (SSH)
available_ips.append(ip) # Add the IP to the list if it is available
s.close() # Close the socket
print(ip)
except:
pass

# Print the available IPs
print('Available IP addresses:', available_ips)