Skip to content

Instantly share code, notes, and snippets.

@SteveClement
Created June 26, 2017 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SteveClement/dd4263dd73717d95ddc1a5ca76359b3e to your computer and use it in GitHub Desktop.
Save SteveClement/dd4263dd73717d95ddc1a5ca76359b3e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Source: # https://github.com/arvydas/blinkstick-python/wiki/Example:-Display-Internet-connectivity-status
import socket
import time
try:
import netifaces
gws = netifaces.gateways()
advanced = True
except:
print("netifaces module not found, disabling advanced status")
"""
This functions checks connection to Google DNS server by default
If DNS server is reachable on port 53, then it means that
the internet is up and running (which is not really true)
"""
def internet_connected(host="8.8.8.8", port=53):
"""
Host: 8.8.8.8 (google-public-dns-a.google.com)
OpenPort: 53/tcp
Service: domain (DNS/TCP)
"""
try:
socket.setdefaulttimeout(1)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except Exception as ex:
pass
return False
def iPibrella():
try:
# Store value of last state in this variable
connected = False
defGw = gws['default'][netifaces.AF_INET][0]
if advanced: print("Current default gateway: {}".format(defGw))
while (True):
if internet_connected():
# If previously internet was disconnected, then print message
# and set pibrella to green
if not connected:
print("Internet is up!")
connected = True
pibrella.light.green.on()
print(advanced)
if advanced and internet_connected(host=defGw, port=22):
print("Default gateway is reacheable via port 22")
pibrella.light.amber.on()
time.sleep(0.3)
pibrella.light.amber.off()
# Wait for 1 second before checking for internet connectivity
time.sleep(1)
else:
# If previously internet connected, then print message
if connected:
print("Internet is down...")
pibrella.light.green.off()
pibrella.light.red.on()
connected = False
except KeyboardInterrupt:
pibrella.lights.off()
print("Exiting... Bye!")
exit()
def bStick():
# Can't do anything if BlinkStick is not connected
if led is None:
print("BlinkStick not found...\r\nExiting...")
else:
try:
# Store value of last state in this variable
connected = False
while (True):
if internet_connected():
# If previously internet was disconnected, then print message
# and set BlinkStick to green
if not connected:
print("Internet is up!")
connected = True
led.set_color(name="green")
# Wait for 1 second before checking for internet connectivity
time.sleep(1)
else:
# If previously internet connected, then print message
if connected:
print("Internet is down...")
connected = False
# BlinkStick pulse API call lasts for 1 second so this acts
# as delay before next check for internet is performed
led.pulse(name="red")
except KeyboardInterrupt:
print("Exiting... Bye!")
led.turn_off()
exit()
try:
from blinkstick import blinkstick
# Find first BlinkStick
led = blinkstick.find_first()
bStick()
except ImportError:
print("Skipping blinkstick integration")
try:
import pibrella
pibrella.lights.on()
pibrella.lights.off()
print("Enabling pibrella")
iPibrella()
except ImportError:
print("Skipping pibrella integration")
if __name__ == "__main__":
try:
# Store value of last state in this variable
connected = False
while (True):
if internet_connected():
# If previously internet was disconnected, then print message
if not connected:
print("Internet is up!")
connected = True
# Wait for 1 second before checking for internet connectivity
time.sleep(1)
else:
# If previously internet connected, then print message
if connected:
print("Internet is down...")
connected = False
except KeyboardInterrupt:
print("Exiting... Bye!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment