-
-
Save eydam-prototyping/f189dbf5ec489b60f6e3798b13b9b851 to your computer and use it in GitHub Desktop.
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
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