Created
May 11, 2025 14:14
-
-
Save Grippy98/beb2862d19139ad33623b4fca6271c40 to your computer and use it in GitHub Desktop.
A network ping test for a device to see how long it takes until it drops off the network - useful to see if it fails in similar time intervals.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import subprocess | |
import time | |
import platform | |
import sys | |
def ping(ip): | |
try: | |
result = subprocess.run( | |
["ping", "-c", "1", ip], | |
stdout=subprocess.DEVNULL, | |
stderr=subprocess.DEVNULL | |
) | |
return result.returncode == 0 | |
except Exception as e: | |
print(f"\nError: {e}") | |
return False | |
def main(): | |
ip = "192.168.1.111" # Change to target IP | |
max_retries = 3 | |
retry_count = 0 | |
print(f"Pinging {ip} ... (fail after {max_retries} consecutive failures)\n") | |
start_time = time.time() | |
while True: | |
elapsed = time.time() - start_time | |
timer_str = time.strftime("%H:%M:%S", time.gmtime(elapsed)) | |
success = ping(ip) | |
status = "✓" if success else "✗" | |
print(f"\rElapsed: {timer_str} | Ping: {status} ({retry_count}/{max_retries})", end="", flush=True) | |
if success: | |
retry_count = 0 | |
else: | |
retry_count += 1 | |
if retry_count >= max_retries: | |
print(f"\n\nPing failed {max_retries} times in a row. Exiting after {timer_str}.") | |
break | |
time.sleep(1) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment