Our project can detect valid or invalid IPs. If an invalid IP is found, the system will issue a prompt.
import subprocess import winsound import ctypes
以上三个是导入的库,没安装的话请务必下载并安装
def ping_ip(ip_address): try: # Ping command for IPv4 and IPv6 if ':' in ip_address: output = subprocess.check_output(['ping', '-n', '1', ip_address], universal_newlines=True, stderr=subprocess.STDOUT) else: output = subprocess.check_output(['ping', '-n', '1', ip_address], universal_newlines=True, stderr=subprocess.STDOUT)
#Output debug information
print(f"Output from pinging {ip_address}:")
print(output)
# Check if there is a "TTL=" or "Reply" string in the output
if "TTL=" in output or "Reply" in output or "回复" in output:
return True
else:
return False
except subprocess.CalledProcessError as e:
print(f"Ping to {ip_address} failed with error: {e.output}")
return False
def alert_sound_with_message(ip_address): # Play system warning sound winsound.MessageBeep(winsound.MB_ICONHAND)
#A message box pops up to print which one failed
message = f"Ping to {ip_address} failed."
ctypes.windll.user32.MessageBoxW(0, message, "Ping Alert", 0x40 | 0x1)
# check_ip function definition def check_ips(ip_addresses): for ip in ip_addresses: # for loop iterates over all ip if not ping_ip(ip): alert_sound_with_message(ip) print(f"Ping to {ip} failed. Alert sound played.") break
ip_addresses = ["8.8.8.8", "hyyy", ""] #Enter the IP address you want to ping here in the form of a list, such as "2409:8c54:870:34e:0:ff:b024:1916", "8.8.8.8" check_ips(ip_addresses) #Remember to add "" and separate them with commas
Log in or sign up for Devpost to join the conversation.