Re: Creating adhoc wireless Network



On Tue, 2009-07-07 at 03:54 -0400, nexus aflb com wrote:
> > On Mon, 2009-07-06 at 11:18 -0400, nexus aflb com wrote:
> >> 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 ?
> >
> > What version of dbus-glib do you have?  What does 'nm-tool' print out
> > when you run it?
> >
> > Dan
> >
> 
> Version provided by SLED11
> 
> dbus-1-glib-0.76-34.3

Ok, that's not quite new enough, anything < 0.77 has a bug with GetAll
that causes properties to not be found.  The commit in question is:

d1b80d803a0268bd4b3dd5b9a9522230461f2947

Author: Dan Williams <dcbw redhat com>  2008-06-05 17:57:53
Committer: Colin Walters <walters verbum org>  2008-06-05 17:57:53
Follows: dbus-glib_0.76
Precedes: dbus-glib_0.78

    Bug 16114 [patch] wincaps-to-uscore property names for GetAll()
    
    	* dbus/dbus-gobject.c: We need to uscore property names
    	so that we actually find the right properties.


but, the good news is that you could do:

dbus_props_iface.Get('org.freedesktop.NetworkManager.Device', '<property name>')

and get the properties you care about individually instead of all of
them at once.  Somewhat less efficient, but it'll work.

Dan

> dbus-1-1.2.10-3.9.1
> dbus-1-python-0.83.0-22.10
> 
> nm-tool print the two interfaces with parameters :
> 
> NetworkManager Tool
> 
> State: connected
> 
> - Device: eth0
> ----------------------------------------------------------------
>   Type:              Wired
>   Driver:            tg3
>   State:             connected
>   Default:           yes
>   HW Address:        00:24:81:56:2E:52
> 
>   Capabilities:
>     Supported:       yes
>     Carrier Detect:  yes
>     Speed:           100 Mb/s
> 
>   Wired Settings
> 
>   IPv4 Settings:
>     Address:         10.82.109.86
>     Prefix:          24 (255.255.255.0)
>     Gateway:         10.82.109.254
> 
>     DNS:             10.82.161.3
>     DNS:             10.68.161.3
> 
> 
> - Device: eth1
> ----------------------------------------------------------------
>   Type:              802.11 WiFi
>   Driver:            iwlagn
>   State:             connected
>   Default:           no
>   HW Address:        00:22:FA:46:76:9A
> 
>   Capabilities:
>     Supported:       yes
> 
>   Wireless Settings
>     WEP Encryption:  yes
>     WPA Encryption:  yes
>     WPA2 Encryption: yes
> 
>   Wireless Access Points(* = Current AP)
>     TESTIPSL:        Infra, 00:03:2F:1D:61:BF, Freq 2437 MHz, Rate 54
> Mb/s, Strength 79 WEP
>     *iSync:          Ad-Hoc, D6:08:0C:12:06:49, Freq 2412 MHz, Rate 0
> Mb/s, Strength 0
> 
>   IPv4 Settings:
>     Address:         10.42.44.1
>     Prefix:          24 (255.255.255.0)
>     Gateway:         0.0.0.0
> 
> 
> >> Thanks
> >>
> >>
> >> > On Wed, 2009-07-01 at 12:28 -0400, Dan Williams wrote:
> >> >> On Wed, 2009-07-01 at 11:26 -0400, nexus 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!"
> >> >
> >> >
> >> >
> >>
> >>
> >
> >
> 
> 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]