Skip to content

Instantly share code, notes, and snippets.

@eydam-prototyping

eydam-prototyping/ping.py Secret

Last active Jan 28, 2021
Embed
What would you like to do?
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