Last active
March 18, 2016 23:12
-
-
Save sugiana/c31ca00602c7e16f2654 to your computer and use it in GitHub Desktop.
Say connection status
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
# Debian: apt-get install espeak | |
# Other OS: http://espeak.sourceforge.net/download.html | |
import sys | |
import commands | |
import subprocess | |
from time import sleep | |
from datetime import datetime | |
ip = sys.argv[1] | |
if sys.argv[2:]: | |
name = sys.argv[2] | |
else: | |
name = ip.split('.')[-1] | |
cmd_ping = ['ping', '-c', '3', '-t', '3', ip] | |
last_connected = None | |
while True: | |
s = commands.getoutput(' '.join(cmd_ping)) | |
for line in s.splitlines(): | |
if line.find('packet loss') < 0: | |
continue | |
connected = line.find(' 0% packet loss') > -1 | |
if connected == last_connected: | |
continue | |
last_connected = connected | |
say_connected = connected and 'connected' or 'disconnected' | |
print('{t} {ip} {c}'.format( | |
t=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), ip=ip, | |
c=say_connected)) | |
say = '{n} {connected}'.format(n=name, connected=say_connected) | |
cmd_speak = ['espeak', say] | |
subprocess.call(cmd_speak) | |
sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment