Hi Dan, I'm playing with the script you provide me but I have a problem :
File "Create_Wireless.py", line 135, in <module> if props['DeviceType'] == 2: # wifi KeyError: 'DeviceType' if I execute step by steps : >>> print nm_iface.GetDevices() dbus.Array([dbus.ObjectPath('/org/freedesktop/Hal/devices/net_00_24_81_56_2e_52'), dbus.ObjectPath('/org/freedesktop/Hal/devices/net_00_22_fa_46_76_9a')], signature=dbus.Signature('o')) >>> dev_proxy = sys_bus.get_object('org.freedesktop.NetworkManager','/org/freedesktop/Hal/devices/net_00_22_fa_46_76_9a') >>> print dev_proxy <ProxyObject wrapping <dbus._dbus.SystemBus (system) at 0xb7a5811c> :1.7 /org/freedesktop/Hal/devices/net_00_22_fa_46_76_9a at 0xb7a5a80c> >>> dev_props_iface = dbus.Interface(dev_proxy, 'org.freedesktop.DBus.Properties') >>> print dev_props_iface <Interface <ProxyObject wrapping <dbus._dbus.SystemBus (system) at 0xb7a5811c> :1.7 /org/freedesktop/Hal/devices/net_00_22_fa_46_76_9a at 0xb7a5a80c> implementing 'org.freedesktop.DBus.Properties' at 0xb7a5a9ac> >>> print dev_props_iface.GetAll('org.freedesktop.NetworkManager.Device') dbus.Dictionary({}, signature=dbus.Signature('sv')) It seems empty ! What's wrong ? Thanks > On Wed, 2009-07-01 at 12:28 -0400, Dan Williams wrote: >> On Wed, 2009-07-01 at 11:26 -0400, ne...@aflb.com wrote: >> > Thanks for this quick answer. >> > >> > That's what I though. Adding a new connection isnot easy to do... >> > >> > when you say "programmatically ask the system settings service to >> create it" >> > >> > What do you mean ? Doing the same stuff as nm-applet but hardcoded ? >> >> Same way nm-connection-editor asks the system settings service to create >> a new connection when the user hits "Apply": you call the >> AddConnection() method on the >> org.freedesktop.NetworkManagerSettings.System interface with the >> connection details you want to set. >> >> Unfortunately we didn't define that method to return the object path of >> the newly created connection in 0.7 (will probably be fixed in 0.8), so >> you have to wait for the NewConnection signal that the >> system-settings-service emits, look for the UUID of the connection you >> just created to get the object path, and then tell NM to activate that >> connection. >> >> It's pretty straightforward actually, once you know what needs to be >> done. If PolicyKit throws up a dialog, use polkit-gnome-authorization >> to allow the user to always have the >> org.freedesktop.network-manager-settings.system.modify permission and >> the user won't ever get asked. > > Check out the attached script. It will create a WEP-enabled adhoc > connection if that connection (identified by UUID) doesn't already > exist, and then direct NetworkManager to activate that connection. The > script is somewhat longer than it needs to be, simply because I made it > more readable, added comments so you can figure out what's going on, and > put some reasonable error checking in. > > Dan > ---- > > #!/bin/env python > # -*- Mode: Python; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 > -*- > # > # Copyright (C) 2009 Red Hat, Inc. > # > # 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. > # > > import dbus > import glib > import sys > import posix > import time > > uuid = "cabfaf9e-4043-4afb-8506-0e6f4a225636" > > s_con = { 'id': 'My AdHoc', > 'uuid': uuid, > 'type': '802-11-wireless', > 'autoconnect': False, > 'name': 'connection' } > > s_wifi = { 'ssid': dbus.ByteArray("foobar"), > 'mode': 'adhoc', > 'security': '802-11-wireless-security', > 'name': '802-11-wireless' } > > s_wsec = { 'key-mgmt': 'none', > 'wep-key0': '0123456789abcdef0123456789', > 'name': '802-11-wireless-security' } > > s_ip4 = { 'method': 'link-local', > 'name': 'ipv4' } > > con = { 'connection': s_con, > '802-11-wireless': s_wifi, > '802-11-wireless-security': s_wsec, > 'ipv4': s_ip4 } > > # init dbus > sys_bus = dbus.SystemBus() > ses_bus = dbus.SessionBus() > > ss_proxy = > sys_bus.get_object('org.freedesktop.NetworkManagerSystemSettings', > '/org/freedesktop/NetworkManagerSettings') > ss_iface = dbus.Interface(ss_proxy, > 'org.freedesktop.NetworkManagerSettings') > ss_sys_iface = dbus.Interface(ss_proxy, > 'org.freedesktop.NetworkManagerSettings.System') > > nm_proxy = sys_bus.get_object('org.freedesktop.NetworkManager', > '/org/freedesktop/NetworkManager') > nm_iface = dbus.Interface(nm_proxy, 'org.freedesktop.NetworkManager') > > pk_proxy = > ses_bus.get_object('org.freedesktop.PolicyKit.AuthenticationAgent', '/') > pk_iface = dbus.Interface(pk_proxy, > 'org.freedesktop.PolicyKit.AuthenticationAgent') > > def find_connection(requested_uuid): > for c in ss_iface.ListConnections(): > # get the details of the connection > c_proxy = > sys_bus.get_object('org.freedesktop.NetworkManagerSystemSettings', > c) > c_iface = dbus.Interface(c_proxy, > 'org.freedesktop.NetworkManagerSettings.Connection') > settings = c_iface.GetSettings() > if settings['connection']['uuid'] == requested_uuid: > # found our connection > return c > return None > > def try_add(connection): > try: > # Ask the system settings service to create the connection > ss_sys_iface.AddConnection(connection) > return None > except Exception, e: > parts = str(e).split(' ') > if > parts[0].find('org.freedesktop.NetworkManagerSettings.System.NotPrivileged') > < 0: > # not a permission denied, give up and exit > print e > sys.exit(1) > # yay, permission denied, we can handle this > return parts[1] > > # MAIN PROGRAM > > con_path = find_connection(uuid) > if not con_path: > # Try to create the connection, which could fail if we need > authorization. > # If auth is required, get the auth and try adding it again > action = try_add(con) > if action: > gained = pk_iface.ObtainAuthorization(action, 0, posix.getpid()) > if gained: > # Yay, we have the privilege now, try adding again > action = try_add(con) > if action: > # hmm, something went wrong and PolicyKit wasn't able to > auth the user > sys.exit(1) > > con_path = find_connection(uuid) > > # Check again in case it was just added > if not con_path: > print "Couldn't get newly created connection from system settings" > > # Find a wifi device to activate this connection on > dev_path = None > for dev in nm_iface.GetDevices(): > dev_proxy = sys_bus.get_object('org.freedesktop.NetworkManager', dev) > dev_props_iface = dbus.Interface(dev_proxy, > 'org.freedesktop.DBus.Properties') > props = > dev_props_iface.GetAll('org.freedesktop.NetworkManager.Device') > if props['DeviceType'] == 2: # wifi > dev_path = dev > break > > if not dev_path: > print "No wifi devices available" > sys.exit(1) > > # Now ask NM to activate that connection > active_path = > nm_iface.ActivateConnection('org.freedesktop.NetworkManagerSystemSettings', > con_path, dev_path, "/") > if not active_path: > print "Couldn't activate connection" > sys.exit(1) > > # Wait for the connection to become active > active_proxy = sys_bus.get_object('org.freedesktop.NetworkManager', > active_path) > active_props_iface = dbus.Interface(active_proxy, > 'org.freedesktop.DBus.Properties') > > state = 0 > while state != 2: # 2 == activated > state = > active_props_iface.Get('org.freedesktop.NetworkManager.Connection.Active', > 'State') > if state != 2: > print "waiting for connection to become active..." > time.sleep(1) > > print "activated!" > > > _______________________________________________ NetworkManager-list mailing list NetworkManager-list@gnome.org http://mail.gnome.org/mailman/listinfo/networkmanager-list