Skip to content

Instantly share code, notes, and snippets.

@LevitatingBusinessMan
Created May 27, 2024 16:48
Show Gist options
  • Save LevitatingBusinessMan/85c600f20cb55479f9b52de78323b9d5 to your computer and use it in GitHub Desktop.
Save LevitatingBusinessMan/85c600f20cb55479f9b52de78323b9d5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "dbus"
system_bus = DBus::SystemBus.instance
session_bus = DBus::SessionBus.instance
$networkd_service = system_bus.service("org.freedesktop.network1")
$networkd_object = $networkd_service.object("/org/freedesktop/network1")
$networkd_properties = $networkd_object["org.freedesktop.DBus.Properties"]
$networkd_manager = $networkd_object["org.freedesktop.network1.Manager"]
def listen name, object
$networkd_service.object(object)["org.freedesktop.DBus.Properties"].on_signal("PropertiesChanged") do |iface, props, inv_props|
puts "Property change on #{name}: #{props}"
notify name, props["OnlineState"] if props.include? "OnlineState"
end
end
def notify name, state
`notify-send "Link #{name} is #{state}"`
end
$networkd_properties.on_signal("PropertiesChanged") do |iface, props, inv_props|
# Listen on the generic /network object
puts "Generic change for #{iface}: #{props}"
end
for index, name, object in $networkd_manager.ListLinks[0]
listen name, object
end
main = DBus::Main.new
main << system_bus
main.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment