examples: add some python examples
This commit is contained in:
52
examples/python/nm-state.py
Normal file
52
examples/python/nm-state.py
Normal file
@@ -0,0 +1,52 @@
|
||||
#!/bin/env python
|
||||
|
||||
import dbus
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
|
||||
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
|
||||
|
||||
# Get device-specific state
|
||||
devices = manager.GetDevices()
|
||||
for d in devices:
|
||||
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", d)
|
||||
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
|
||||
|
||||
# Get the device's current state and interface name
|
||||
state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
|
||||
name = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
|
||||
|
||||
# and print them out
|
||||
if state == 8: # activated
|
||||
print "Device %s is activated" % name
|
||||
else:
|
||||
print "Device %s is not activated" % name
|
||||
|
||||
|
||||
# Get active connection state
|
||||
manager_prop_iface = dbus.Interface(proxy, "org.freedesktop.DBus.Properties")
|
||||
active = manager_prop_iface.Get("org.freedesktop.NetworkManager", "ActiveConnections")
|
||||
for a in active:
|
||||
ac_proxy = bus.get_object("org.freedesktop.NetworkManager", a)
|
||||
prop_iface = dbus.Interface(ac_proxy, "org.freedesktop.DBus.Properties")
|
||||
state = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "State")
|
||||
|
||||
# Connections in NM are a collection of settings that describe everything
|
||||
# needed to connect to a specific network. Lets get those details so we
|
||||
# can find the user-readable name of the connection.
|
||||
con_path = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "Connection")
|
||||
con_service = prop_iface.Get("org.freedesktop.NetworkManager.ActiveConnection", "ServiceName")
|
||||
|
||||
# ask the provider of the connection for its details
|
||||
service_proxy = bus.get_object(con_service, con_path)
|
||||
con_iface = dbus.Interface(service_proxy, "org.freedesktop.NetworkManagerSettings.Connection")
|
||||
con_details = con_iface.GetSettings()
|
||||
con_name = con_details['connection']['id']
|
||||
|
||||
if state == 2: # activated
|
||||
print "Connection '%s' is activated" % con_name
|
||||
else:
|
||||
print "Connection '%s' is activating" % con_name
|
||||
|
||||
|
Reference in New Issue
Block a user