mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager
synced 2024-07-09 04:05:56 +00:00
examples: add some python examples
This commit is contained in:
parent
d75aa067e3
commit
fa9e9b4227
34
Makefile.am
34
Makefile.am
|
@ -1,20 +1,20 @@
|
|||
SUBDIRS = marshallers
|
||||
|
||||
SUBDIRS += \
|
||||
libnm-util \
|
||||
libnm-glib \
|
||||
src \
|
||||
include \
|
||||
introspection \
|
||||
callouts \
|
||||
system-settings \
|
||||
tools \
|
||||
policy \
|
||||
initscript \
|
||||
test \
|
||||
po \
|
||||
man \
|
||||
docs
|
||||
SUBDIRS = \
|
||||
marshallers \
|
||||
libnm-util \
|
||||
libnm-glib \
|
||||
src \
|
||||
include \
|
||||
introspection \
|
||||
callouts \
|
||||
system-settings \
|
||||
tools \
|
||||
policy \
|
||||
initscript \
|
||||
test \
|
||||
po \
|
||||
man \
|
||||
docs \
|
||||
examples
|
||||
|
||||
EXTRA_DIST = \
|
||||
CONTRIBUTING \
|
||||
|
|
|
@ -494,6 +494,8 @@ docs/Makefile
|
|||
docs/libnm-glib/Makefile
|
||||
docs/libnm-util/Makefile
|
||||
NetworkManager.pc
|
||||
examples/Makefile
|
||||
examples/python/Makefile
|
||||
])
|
||||
AC_OUTPUT
|
||||
|
||||
|
|
1
examples/Makefile.am
Normal file
1
examples/Makefile.am
Normal file
|
@ -0,0 +1 @@
|
|||
SUBDIRS=python
|
4
examples/python/Makefile.am
Normal file
4
examples/python/Makefile.am
Normal file
|
@ -0,0 +1,4 @@
|
|||
EXTRA_DIST = \
|
||||
nm-state.py \
|
||||
add-system-connection.py
|
||||
|
31
examples/python/add-system-connection.py
Normal file
31
examples/python/add-system-connection.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
#!/bin/env python
|
||||
|
||||
import dbus
|
||||
|
||||
s_wired = dbus.Dictionary({'duplex': 'full'})
|
||||
s_con = dbus.Dictionary({
|
||||
'type': '802-3-ethernet',
|
||||
'uuid': '7371bb78-c1f7-42a3-a9db-5b9566e8ca07',
|
||||
'id': 'MyConnection'})
|
||||
|
||||
addr1 = dbus.Array([dbus.UInt32(50462986L), dbus.UInt32(0L), dbus.UInt32(16908554L)], signature=dbus.Signature('u'))
|
||||
s_ip4 = dbus.Dictionary({
|
||||
'addresses': dbus.Array([addr1], signature=dbus.Signature('au')),
|
||||
'method': 'manual'})
|
||||
|
||||
s_ip6 = dbus.Dictionary({'method': 'ignore'})
|
||||
|
||||
con = dbus.Dictionary({
|
||||
'802-3-ethernet': s_wired,
|
||||
'connection': s_con,
|
||||
'ipv4': s_ip4,
|
||||
'ipv6': s_ip6})
|
||||
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManagerSystemSettings", "/org/freedesktop/NetworkManagerSettings")
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManagerSettings")
|
||||
|
||||
settings.AddConnection(con)
|
||||
|
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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user