Skip to content

Instantly share code, notes, and snippets.

@hadrianw
Created April 9, 2024 20:19
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 hadrianw/2048b722442e2c717d7277dcb338d1ad to your computer and use it in GitHub Desktop.
Save hadrianw/2048b722442e2c717d7277dcb338d1ad to your computer and use it in GitHub Desktop.
Minimal notification service for Avahi services
#!/usr/bin/env python
"""
example use:
./avahi-notify.py - notify for all interfaces
./avahi-notify.py eth0 - notify only for eth0 interface
"""
import io
import subprocess
import sys
if __name__ == "__main__":
ifaces = sys.argv[1:]
avahi = subprocess.Popen(["avahi-browse", "-pa"], stdout=subprocess.PIPE)
for line in io.TextIOWrapper(avahi.stdout, encoding="utf-8"):
(state, iface, proto, name, stype, domain) = line.split(";")
if ifaces and iface not in ifaces:
continue
icon = ()
if state == "+":
icon = ("-i", "list-add")
title = f"Connected {stype} ({proto})"
elif state == "-":
icon = ("-i", "list-remove")
title = f"Disconnected {stype} ({proto})"
p = subprocess.run(("notify-send", *icon, title, name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment