examples: group python examples - dbus vs. gi
Move examples using dbus-python ('dbus' module) and GObject introspection into their own directories.
This commit is contained in:
11
examples/python/dbus/Makefile.am
Normal file
11
examples/python/dbus/Makefile.am
Normal file
@@ -0,0 +1,11 @@
|
||||
EXTRA_DIST = \
|
||||
nm-state.py \
|
||||
add-connection.py \
|
||||
add-system-wifi-connection.py
|
||||
vpn.py \
|
||||
update-secrets.py \
|
||||
list-connections.py \
|
||||
show-bssids.py \
|
||||
disconnect-device.py \
|
||||
get-active-connection-uuids.py \
|
||||
list-devices.py
|
63
examples/python/dbus/add-connection.py
Executable file
63
examples/python/dbus/add-connection.py
Executable file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 - 2012 Red Hat, Inc.
|
||||
#
|
||||
|
||||
#
|
||||
# This example adds a new ethernet connection via AddConnection() D-Bus call.
|
||||
#
|
||||
# Configuration settings are described at
|
||||
# http://projects.gnome.org/NetworkManager/developers/api/09/ref-settings.html
|
||||
#
|
||||
|
||||
import socket, struct, dbus, uuid
|
||||
|
||||
# Helper functions
|
||||
def ip_to_int(ip_string):
|
||||
return struct.unpack("=I", socket.inet_aton(ip_string))[0]
|
||||
|
||||
def int_to_ip(ip_int):
|
||||
return socket.inet_ntoa(struct.pack("=I", ip_int))
|
||||
|
||||
s_wired = dbus.Dictionary({'duplex': 'full'})
|
||||
s_con = dbus.Dictionary({
|
||||
'type': '802-3-ethernet',
|
||||
'uuid': str(uuid.uuid4()),
|
||||
'id': 'MyConnectionExample'})
|
||||
|
||||
addr1 = dbus.Array([ip_to_int("10.1.2.3"), dbus.UInt32(8L), ip_to_int("10.1.2.1")], 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})
|
||||
|
||||
|
||||
print "Creating connection:", s_con['id'], "-", s_con['uuid']
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
|
||||
|
||||
settings.AddConnection(con)
|
||||
|
63
examples/python/dbus/add-system-wifi-connection.py
Executable file
63
examples/python/dbus/add-system-wifi-connection.py
Executable file
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2011 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus
|
||||
|
||||
def path_to_value(path):
|
||||
return dbus.ByteArray("file://" + path + "\0")
|
||||
|
||||
s_con = dbus.Dictionary({
|
||||
'type': '802-11-wireless',
|
||||
'uuid': '7371bb78-c1f7-42a3-a9db-5b9566e8ca07',
|
||||
'id': 'My Wifi'})
|
||||
|
||||
s_wifi = dbus.Dictionary({
|
||||
'ssid': dbus.ByteArray("homewifi"),
|
||||
'security': '802-11-wireless-security'})
|
||||
|
||||
s_wsec = dbus.Dictionary({'key-mgmt': 'wpa-eap'})
|
||||
|
||||
s_8021x = dbus.Dictionary({
|
||||
'eap': ['tls'],
|
||||
'identity': 'Bill Smith',
|
||||
'client-cert': path_to_value("/some/place/client.pem"),
|
||||
'ca-cert': path_to_value("/some/place/ca-cert.pem"),
|
||||
'private-key': path_to_value("/some/place/privkey.pem"),
|
||||
'private-key-password': "12345testing"})
|
||||
|
||||
s_ip4 = dbus.Dictionary({'method': 'auto'})
|
||||
s_ip6 = dbus.Dictionary({'method': 'ignore'})
|
||||
|
||||
con = dbus.Dictionary({
|
||||
'connection': s_con,
|
||||
'802-11-wireless': s_wifi,
|
||||
'802-11-wireless-security': s_wsec,
|
||||
'802-1x': s_8021x,
|
||||
'ipv4': s_ip4,
|
||||
'ipv6': s_ip6
|
||||
})
|
||||
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
|
||||
|
||||
settings.AddConnection(con)
|
||||
|
62
examples/python/dbus/disconnect-device.py
Executable file
62
examples/python/dbus/disconnect-device.py
Executable file
@@ -0,0 +1,62 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus, sys
|
||||
|
||||
# This example takes a device interface name as a parameter and tells
|
||||
# NetworkManager to disconnect that device, closing down any network
|
||||
# connection it may have
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
raise Exception("Usage: %s <interface>" % sys.argv[0])
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
# Get a proxy for the base NetworkManager object
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
|
||||
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
|
||||
|
||||
dpath = None
|
||||
|
||||
# Find the device the user wants to disconnect
|
||||
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")
|
||||
iface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
|
||||
if iface == sys.argv[1]:
|
||||
dpath = d
|
||||
break
|
||||
|
||||
if not dpath or not len(dpath):
|
||||
raise Exception("NetworkManager knows nothing about %s" % sys.argv[1])
|
||||
|
||||
dev_proxy = bus.get_object("org.freedesktop.NetworkManager", dpath)
|
||||
dev_iface = dbus.Interface(dev_proxy, "org.freedesktop.NetworkManager.Device")
|
||||
prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
|
||||
|
||||
# Make sure the device is connected before we try to disconnect it
|
||||
state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
|
||||
if state <= 3:
|
||||
raise Exception("Device %s isn't connected" % sys.argv[1])
|
||||
|
||||
# Tell NM to disconnect it
|
||||
dev_iface.Disconnect()
|
||||
|
56
examples/python/dbus/get-active-connection-uuids.py
Executable file
56
examples/python/dbus/get-active-connection-uuids.py
Executable file
@@ -0,0 +1,56 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus, sys
|
||||
|
||||
# This example takes a device interface name as a parameter and tells
|
||||
# NetworkManager to disconnect that device, closing down any network
|
||||
# connection it may have
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
# Get a proxy for the base NetworkManager object
|
||||
m_proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
|
||||
manager = dbus.Interface(m_proxy, "org.freedesktop.NetworkManager")
|
||||
mgr_props = dbus.Interface(m_proxy, "org.freedesktop.DBus.Properties")
|
||||
|
||||
s_proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
|
||||
settings = dbus.Interface(s_proxy, "org.freedesktop.NetworkManager.Settings")
|
||||
|
||||
# Find the device the user wants to disconnect
|
||||
active = mgr_props.Get("org.freedesktop.NetworkManager", "ActiveConnections")
|
||||
for a in active:
|
||||
a_proxy = bus.get_object("org.freedesktop.NetworkManager", a)
|
||||
|
||||
# Get the UUID directly; apps could use this to perform certain operations
|
||||
# based on which network you're connected too
|
||||
a_props = dbus.Interface(a_proxy, "org.freedesktop.DBus.Properties")
|
||||
uuid = a_props.Get("org.freedesktop.NetworkManager.Connection.Active", "Uuid")
|
||||
|
||||
# Grab the connection object path so we can get all the connection's settings
|
||||
connection_path = a_props.Get("org.freedesktop.NetworkManager.Connection.Active", "Connection")
|
||||
c_proxy = bus.get_object("org.freedesktop.NetworkManager", connection_path)
|
||||
connection = dbus.Interface(c_proxy, "org.freedesktop.NetworkManager.Settings.Connection")
|
||||
settings = connection.GetSettings()
|
||||
print "%s (%s)" % (settings['connection']['id'], uuid)
|
||||
|
||||
if len(active) == 0:
|
||||
print "No active connections"
|
||||
|
107
examples/python/dbus/list-connections.py
Executable file
107
examples/python/dbus/list-connections.py
Executable file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 - 2011 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus
|
||||
|
||||
# This example asks settings service for all configured connections.
|
||||
# It also asks for secrets, demonstrating the mechanism the secrets can
|
||||
# be handled with.
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
def merge_secrets(proxy, config, setting_name):
|
||||
try:
|
||||
# returns a dict of dicts mapping name::setting, where setting is a dict
|
||||
# mapping key::value. Each member of the 'setting' dict is a secret
|
||||
secrets = proxy.GetSecrets(setting_name)
|
||||
|
||||
# Copy the secrets into our connection config
|
||||
for setting in secrets:
|
||||
for key in secrets[setting]:
|
||||
config[setting_name][key] = secrets[setting][key]
|
||||
except Exception, e:
|
||||
pass
|
||||
|
||||
def dict_to_string(d, indent):
|
||||
# Try to trivially translate a dictionary's elements into nice string
|
||||
# formatting.
|
||||
dstr = ""
|
||||
for key in d:
|
||||
val = d[key]
|
||||
str_val = ""
|
||||
add_string = True
|
||||
if type(val) == type(dbus.Array([])):
|
||||
for elt in val:
|
||||
if type(elt) == type(dbus.Byte(1)):
|
||||
str_val += "%s " % int(elt)
|
||||
elif type(elt) == type(dbus.String("")):
|
||||
str_val += "%s" % elt
|
||||
elif type(val) == type(dbus.Dictionary({})):
|
||||
dstr += dict_to_string(val, indent + " ")
|
||||
add_string = False
|
||||
else:
|
||||
str_val = val
|
||||
if add_string:
|
||||
dstr += "%s%s: %s\n" % (indent, key, str_val)
|
||||
return dstr
|
||||
|
||||
def connection_to_string(config):
|
||||
# dump a connection configuration to a the console
|
||||
for setting_name in config:
|
||||
print " Setting: %s" % setting_name
|
||||
print dict_to_string(config[setting_name], " ")
|
||||
print ""
|
||||
|
||||
|
||||
def print_connections():
|
||||
# Ask the settings service for the list of connections it provides
|
||||
service_name = "org.freedesktop.NetworkManager"
|
||||
proxy = bus.get_object(service_name, "/org/freedesktop/NetworkManager/Settings")
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
|
||||
connection_paths = settings.ListConnections()
|
||||
|
||||
# List each connection's name, UUID, and type
|
||||
for path in connection_paths:
|
||||
con_proxy = bus.get_object(service_name, path)
|
||||
settings_connection = dbus.Interface(con_proxy, "org.freedesktop.NetworkManager.Settings.Connection")
|
||||
config = settings_connection.GetSettings()
|
||||
|
||||
# Now get secrets too; we grab the secrets for each type of connection
|
||||
# (since there isn't a "get all secrets" call because most of the time
|
||||
# you only need 'wifi' secrets or '802.1x' secrets, not everything) and
|
||||
# merge that into the configuration data
|
||||
merge_secrets(settings_connection, config, '802-11-wireless')
|
||||
merge_secrets(settings_connection, config, '802-11-wireless-security')
|
||||
merge_secrets(settings_connection, config, '802-1x')
|
||||
merge_secrets(settings_connection, config, 'gsm')
|
||||
merge_secrets(settings_connection, config, 'cdma')
|
||||
merge_secrets(settings_connection, config, 'ppp')
|
||||
|
||||
# Get the details of the 'connection' setting
|
||||
s_con = config['connection']
|
||||
print " name: %s" % s_con['id']
|
||||
print " uuid: %s" % s_con['uuid']
|
||||
print " type: %s" % s_con['type']
|
||||
print " ------------------------------------------"
|
||||
connection_to_string(config)
|
||||
|
||||
print ""
|
||||
|
||||
print_connections()
|
||||
|
78
examples/python/dbus/list-devices.py
Executable file
78
examples/python/dbus/list-devices.py
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- Mode: python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2011 - 2012 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus, sys
|
||||
|
||||
# This example lists basic information about network interfaces known to NM
|
||||
|
||||
devtypes = { 1: "Ethernet",
|
||||
2: "Wi-Fi",
|
||||
5: "Bluetooth",
|
||||
6: "OLPC",
|
||||
7: "WiMAX",
|
||||
8: "Modem",
|
||||
9: "InfiniBand",
|
||||
10: "Bond",
|
||||
11: "VLAN",
|
||||
12: "ADSL" }
|
||||
|
||||
states = { 0: "Unknown",
|
||||
10: "Unmanaged",
|
||||
20: "Unavailable",
|
||||
30: "Disconnected",
|
||||
40: "Prepare",
|
||||
50: "Config",
|
||||
60: "Need Auth",
|
||||
70: "IP Config",
|
||||
80: "IP Check",
|
||||
90: "Secondaries",
|
||||
100: "Activated",
|
||||
110: "Deactivating",
|
||||
120: "Failed" }
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
# Get a proxy for the base NetworkManager object
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
|
||||
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
|
||||
|
||||
# Get all devices known to NM and print their properties
|
||||
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")
|
||||
props = prop_iface.GetAll("org.freedesktop.NetworkManager.Device")
|
||||
print "============================"
|
||||
|
||||
print "Interface: %s" % props['Interface']
|
||||
try:
|
||||
devtype = devtypes[props['DeviceType']]
|
||||
except KeyError:
|
||||
devtype = "Unknown"
|
||||
print "Type: %s" % devtype
|
||||
|
||||
print "Driver: %s" % props['Driver']
|
||||
|
||||
try:
|
||||
state = states[props['State']]
|
||||
except KeyError:
|
||||
state = "Unknown"
|
||||
print "State: %s" % state
|
||||
|
81
examples/python/dbus/nm-state.py
Executable file
81
examples/python/dbus/nm-state.py
Executable file
@@ -0,0 +1,81 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 - 2012 Red Hat, Inc.
|
||||
#
|
||||
|
||||
import dbus
|
||||
|
||||
# Device states from include/NetworkManager.h
|
||||
device_states = { 0: "Unknown",
|
||||
10: "Unmanaged",
|
||||
20: "Unavailable",
|
||||
30: "Disconnected",
|
||||
40: "Prepare",
|
||||
50: "Config",
|
||||
60: "Need Auth",
|
||||
70: "IP Config",
|
||||
80: "IP Check",
|
||||
90: "Secondaries",
|
||||
100: "Activated",
|
||||
110: "Deactivating",
|
||||
120: "Failed" }
|
||||
|
||||
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 == 100 : # activated
|
||||
print "Device %s is activated" % name
|
||||
else:
|
||||
print "Device %s is not activated (state=%s)" % (name, device_states[state])
|
||||
|
||||
|
||||
# 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.Connection.Active", "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.Connection.Active", "Connection")
|
||||
service_proxy = bus.get_object("org.freedesktop.NetworkManager", con_path)
|
||||
con_iface = dbus.Interface(service_proxy, "org.freedesktop.NetworkManager.Settings.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
|
||||
|
||||
|
78
examples/python/dbus/show-bssids.py
Executable file
78
examples/python/dbus/show-bssids.py
Executable file
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2010 Red Hat, Inc.
|
||||
#
|
||||
|
||||
|
||||
# This example prints out all the AP BSSIDs that all WiFi devices on the
|
||||
# machine can see. Useful for location-based services like Skyhook that
|
||||
# can geolocate you based on the APs you can see.
|
||||
|
||||
import dbus
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
# Get a proxy for the base NetworkManager object
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager")
|
||||
manager = dbus.Interface(proxy, "org.freedesktop.NetworkManager")
|
||||
|
||||
all_aps = []
|
||||
|
||||
print "Associated APs:"
|
||||
|
||||
# Get all network devices
|
||||
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")
|
||||
|
||||
# Make sure the device is enabled before we try to use it
|
||||
state = prop_iface.Get("org.freedesktop.NetworkManager.Device", "State")
|
||||
if state <= 2:
|
||||
continue
|
||||
|
||||
# Get device's type; we only want wifi devices
|
||||
iface = prop_iface.Get("org.freedesktop.NetworkManager.Device", "Interface")
|
||||
dtype = prop_iface.Get("org.freedesktop.NetworkManager.Device", "DeviceType")
|
||||
if dtype == 2: # WiFi
|
||||
# Get a proxy for the wifi interface
|
||||
wifi_iface = dbus.Interface(dev_proxy, "org.freedesktop.NetworkManager.Device.Wireless")
|
||||
wifi_prop_iface = dbus.Interface(dev_proxy, "org.freedesktop.DBus.Properties")
|
||||
|
||||
# Get the associated AP's object path
|
||||
connected_path = wifi_prop_iface.Get("org.freedesktop.NetworkManager.Device.Wireless", "ActiveAccessPoint")
|
||||
|
||||
# Get all APs the card can see
|
||||
aps = wifi_iface.GetAccessPoints()
|
||||
for path in aps:
|
||||
ap_proxy = bus.get_object("org.freedesktop.NetworkManager", path)
|
||||
ap_prop_iface = dbus.Interface(ap_proxy, "org.freedesktop.DBus.Properties")
|
||||
bssid = ap_prop_iface.Get("org.freedesktop.NetworkManager.AccessPoint", "HwAddress")
|
||||
|
||||
# Cache the BSSID
|
||||
if not bssid in all_aps:
|
||||
all_aps.append(bssid)
|
||||
|
||||
# Print the current AP's BSSID
|
||||
if path == connected_path:
|
||||
print "%s (%s)" % (bssid, iface)
|
||||
|
||||
# and print out all APs the wifi devices can see
|
||||
print"\nFound APs:"
|
||||
for bssid in all_aps:
|
||||
print bssid
|
||||
|
116
examples/python/dbus/update-secrets.py
Executable file
116
examples/python/dbus/update-secrets.py
Executable file
@@ -0,0 +1,116 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2011 - 2012 Red Hat, Inc.
|
||||
#
|
||||
|
||||
#
|
||||
# The example shows how to update secrets in a connection by means of D-Bus
|
||||
# Update() method. The method replaces all previous settings with new ones
|
||||
# including possible secrets.
|
||||
# So, we get all settings using GetSettings() and then find out what secrets
|
||||
# are associated with the connection using GetSecrets(), ask for new secret
|
||||
# values, and add them to the settings that we pass to Update().
|
||||
#
|
||||
|
||||
import dbus
|
||||
import sys
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
|
||||
def change_secrets_in_one_setting(proxy, config, setting_name):
|
||||
# Add new secret values to the connection config
|
||||
try:
|
||||
# returns a dict of dicts mapping name::setting, where setting is a dict
|
||||
# mapping key::value. Each member of the 'setting' dict is a secret
|
||||
secrets = proxy.GetSecrets(setting_name)
|
||||
print "Current secrets:", secrets
|
||||
|
||||
# Ask user for new secrets and put them into our connection config
|
||||
for setting in secrets:
|
||||
for key in secrets[setting]:
|
||||
new_secret = raw_input ("Enter new secret for '%s' in '%s': " % (key, setting))
|
||||
config[setting_name][key] = new_secret
|
||||
except Exception, e:
|
||||
#code = str(e).split(':')[0]
|
||||
#print "Exception:", str(e)
|
||||
pass
|
||||
|
||||
def change_secrets(con_path, config):
|
||||
# Get existing secrets; we grab the secrets for each type of connection
|
||||
# (since there isn't a "get all secrets" call because most of the time
|
||||
# you only need 'wifi' secrets or '802.1x' secrets, not everything) and
|
||||
# set new values into the connection settings (config)
|
||||
con_proxy = bus.get_object("org.freedesktop.NetworkManager", con_path)
|
||||
connection_secrets = dbus.Interface(con_proxy, "org.freedesktop.NetworkManager.Settings.Connection")
|
||||
change_secrets_in_one_setting(connection_secrets, config, '802-11-wireless')
|
||||
change_secrets_in_one_setting(connection_secrets, config, '802-11-wireless-security')
|
||||
change_secrets_in_one_setting(connection_secrets, config, '802-1x')
|
||||
change_secrets_in_one_setting(connection_secrets, config, 'gsm')
|
||||
change_secrets_in_one_setting(connection_secrets, config, 'cdma')
|
||||
change_secrets_in_one_setting(connection_secrets, config, 'ppp')
|
||||
|
||||
def find_connection(name):
|
||||
# Ask the settings service for the list of connections it provides
|
||||
global con_path
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", "/org/freedesktop/NetworkManager/Settings")
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings")
|
||||
connection_paths = settings.ListConnections()
|
||||
|
||||
# Get the settings and look for connection's name
|
||||
for path in connection_paths:
|
||||
con_proxy = bus.get_object("org.freedesktop.NetworkManager", path)
|
||||
connection = dbus.Interface(con_proxy, "org.freedesktop.NetworkManager.Settings.Connection")
|
||||
try:
|
||||
config = connection.GetSettings()
|
||||
except Exception, e:
|
||||
pass
|
||||
|
||||
# Find connection by the id
|
||||
s_con = config['connection']
|
||||
if name == s_con['id']:
|
||||
con_path = path
|
||||
return config
|
||||
# Find connection by the uuid
|
||||
if name == s_con['uuid']:
|
||||
con_path = path
|
||||
return config
|
||||
|
||||
return None
|
||||
|
||||
|
||||
# Main part
|
||||
con_path = None
|
||||
|
||||
if len(sys.argv) != 2:
|
||||
sys.exit("Usage: %s <connection name/uuid>" % sys.argv[0])
|
||||
|
||||
# Find the connection
|
||||
con = find_connection(sys.argv[1])
|
||||
|
||||
print "Connection found: ", con_path
|
||||
|
||||
if con:
|
||||
# Obtain new secrets and put then into connection dict
|
||||
change_secrets(con_path, con)
|
||||
|
||||
# Change the connection with Update()
|
||||
proxy = bus.get_object("org.freedesktop.NetworkManager", con_path)
|
||||
settings = dbus.Interface(proxy, "org.freedesktop.NetworkManager.Settings.Connection")
|
||||
settings.Update(con)
|
||||
else:
|
||||
sys.exit("No connection '%s' found" % sys.argv[1])
|
||||
|
152
examples/python/dbus/vpn.py
Executable file
152
examples/python/dbus/vpn.py
Executable file
@@ -0,0 +1,152 @@
|
||||
#!/usr/bin/env python
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU General Public License as published by
|
||||
# the Free Software Foundation; either version 2 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License along
|
||||
# with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
#
|
||||
# Copyright (C) 2009 Novell, Inc.
|
||||
# Copyright (C) 2009 Red Hat, Inc.
|
||||
#
|
||||
|
||||
# Run this script without any arguments to list the available connection uuids.
|
||||
|
||||
# The uuid of the connection to activate
|
||||
CONNECTION_UUID="ac6dc9b2-85ef-4311-83d8-add5d7db3f59"
|
||||
|
||||
# UID to use. Note that NM only allows the owner of the connection to activate it.
|
||||
#UID=1000
|
||||
UID=0
|
||||
|
||||
import sys
|
||||
import os
|
||||
import dbus
|
||||
from dbus.mainloop.glib import DBusGMainLoop
|
||||
import gobject
|
||||
|
||||
DBusGMainLoop(set_as_default=True)
|
||||
|
||||
def get_connections():
|
||||
bus = dbus.SystemBus()
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager/Settings')
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager.Settings')
|
||||
return iface.ListConnections()
|
||||
|
||||
|
||||
def get_connection_by_uuid(uuid):
|
||||
bus = dbus.SystemBus()
|
||||
for c in get_connections():
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', c)
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager.Settings.Connection')
|
||||
settings = iface.GetSettings()
|
||||
if settings['connection']['uuid'] == uuid:
|
||||
return c
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def list_uuids():
|
||||
bus = dbus.SystemBus()
|
||||
for c in get_connections():
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', c)
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager.Settings.Connection')
|
||||
settings = iface.GetSettings()
|
||||
conn = settings['connection']
|
||||
print "%s - %s (%s)" % (conn['uuid'], conn['id'], conn['type'])
|
||||
|
||||
|
||||
def get_active_connection_path(uuid):
|
||||
bus = dbus.SystemBus()
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
|
||||
active_connections = iface.Get('org.freedesktop.NetworkManager', 'ActiveConnections')
|
||||
all_connections = get_connections()
|
||||
|
||||
for a in active_connections:
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', a)
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
|
||||
path = iface.Get('org.freedesktop.NetworkManager.Connection.Active', 'Connection')
|
||||
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', path)
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager.Settings.Connection')
|
||||
settings = iface.GetSettings()
|
||||
|
||||
if settings['connection']['uuid'] == uuid:
|
||||
return a
|
||||
|
||||
return None
|
||||
|
||||
|
||||
def get_wifi_device_path():
|
||||
bus = dbus.SystemBus()
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager')
|
||||
devices = iface.GetDevices()
|
||||
for d in devices:
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', d)
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.DBus.Properties')
|
||||
devtype = iface.Get('org.freedesktop.NetworkManager.Device', 'DeviceType')
|
||||
if devtype == 2:
|
||||
return d
|
||||
return None
|
||||
|
||||
def activate_connection(connection_path, device_path):
|
||||
|
||||
def reply_handler(opath):
|
||||
print "Success: device activating"
|
||||
sys.exit(0)
|
||||
|
||||
def error_handler(*args):
|
||||
print "Error activating device: %s" % args
|
||||
sys.exit(1)
|
||||
|
||||
bus = dbus.SystemBus()
|
||||
proxy = bus.get_object('org.freedesktop.NetworkManager', '/org/freedesktop/NetworkManager')
|
||||
iface = dbus.Interface(proxy, dbus_interface='org.freedesktop.NetworkManager')
|
||||
iface.ActivateConnection('org.freedesktop.NetworkManager',
|
||||
connection_path,
|
||||
device_path,
|
||||
"/",
|
||||
reply_handler=reply_handler,
|
||||
error_handler=error_handler)
|
||||
|
||||
|
||||
# Change the UID first if required
|
||||
if UID != 0:
|
||||
os.setuid(UID)
|
||||
|
||||
# Are we configured?
|
||||
if not len(CONNECTION_UUID):
|
||||
print "missing connection UUID"
|
||||
sys.exit(0)
|
||||
|
||||
connection_path = get_connection_by_uuid(CONNECTION_UUID)
|
||||
if not connection_path:
|
||||
# Configured VPN connection is not known to NM, check CONNECTION_UUID.
|
||||
print "couldn't find the connection"
|
||||
sys.exit(1)
|
||||
|
||||
device_path = get_wifi_device_path()
|
||||
if not device_path:
|
||||
print "no wifi device found"
|
||||
sys.exit(1)
|
||||
|
||||
# Is it already activated?
|
||||
if get_active_connection_path(CONNECTION_UUID):
|
||||
print "already connected"
|
||||
sys.exit(0)
|
||||
|
||||
print "Activating connection..."
|
||||
activate_connection(connection_path, device_path)
|
||||
loop = gobject.MainLoop()
|
||||
loop.run()
|
||||
|
Reference in New Issue
Block a user