Created
March 16, 2018 13:28
-
-
Save spiffomatic64/e6a6c4ea2a8009d2346eb65117f7a9ca to your computer and use it in GitHub Desktop.
python ping thing
This file contains 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
#!/usr/bin/env python | |
from platform import system as system_name | |
import subprocess | |
import signal | |
import sys | |
import time | |
running = True | |
def signal_handler(signal, frame): | |
global running | |
running = False | |
print('You pressed Ctrl+C!') | |
def ping(host): | |
if system_name().lower()=='windows': | |
param = "-n 1" | |
else: | |
param = "-c 1" | |
command = "ping %s %s" % (param, host) | |
try: | |
raw_output = subprocess.check_output(command,shell=True) | |
return True | |
except subprocess.CalledProcessError as err: | |
return False | |
signal.signal(signal.SIGINT, signal_handler) | |
print('Press Ctrl+C to stop') | |
while running: | |
if ping("8.8.8.8") == False: | |
print('\a') | |
else: | |
print('yay') | |
time.sleep(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment