Skip to content

Instantly share code, notes, and snippets.

@eydam-prototyping
Last active January 28, 2021 16:10
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 eydam-prototyping/f189dbf5ec489b60f6e3798b13b9b851 to your computer and use it in GitHub Desktop.
Save eydam-prototyping/f189dbf5ec489b60f6e3798b13b9b851 to your computer and use it in GitHub Desktop.
import subprocess
import re
import paho.mqtt.client as mqtt
import json
import time
client = mqtt.Client()
client.connect("localhost", 1883, 60)
data = {}
while True:
for host in ["fritz.box", "google.com"]:
try:
x = str(subprocess.Popen(
["ping", "-c", "1", host], stdout=subprocess.PIPE).communicate()[0])
m = re.search(
"\d* bytes from (.*?): icmp_seq=\d* ttl=\d* time=(\d*\.?\d*) ms", x)
data[host] = {
"addr": m.group(1),
"duration": float(m.group(2))
}
except:
pass
print(data)
client.publish(topic="test/ping", payload=json.dumps(data))
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment