Since tehkain is corrently not working on the network manager applet I decided to do a little work on it on my own.
First of all I try writing some kind a python network manager backend.
It's quite tricky since there is no documentation for the network manager dbus interface at all - only a mailing list archive and NMs source code.
Well, I got some kind a Beta 0.0.1 ready. It should be able to at least manage NMs basic functions. My next step will be encryption handling and then vpnc/openVPN/PPTP.
If anyone here got any idea how to implement this or how the stuff, i marked in the source code works, please post here or send me an eMail ()
pynm2.py
"""
This library is designed to provide an easy access to the network
manager for python programmers by using NMs dBus Interface.
Version: Beta 0.0.1
"""
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#
# This code is based on the netbus network manager backend written
# by Randal Barlow as part of an alpha version awn network applet
#
#
# Sadly there is nearly no Documentation at all.
# An outline of the NetworkManagers dBus api can be found here:
#
http://people.redhat.com/dcbw/NetworkManager/NetworkManager%20DBUS%20API.txt
# Carefull: status should be state
#
# More detailed information is obtainable via the NM Mailing list, although it
# isn't struktured at all:
#
http://mail.gnome.org/archives/networkmanager-list/
#
# imports
import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop
dbus_loop = DBusGMainLoop()
# global stuff
__dService = "org.freedesktop.NetworkManager"
__dObjectPath = "/org/freedesktop/NetworkManager"
__dInterface = "org.freedesktop.NetworkManager"
__deviceInt = "org.freedesktop.NetworkManager.Devices"
__deviceLocationCore = "/org/freedesktop/NetworkManager/Devices/"
__HALLocationCore = "/org/freedesktop/Hal/devices/"
__session_bus = dbus.SystemBus(mainloop=dbus_loop)
__proxy_obj_NetworkManager = __session_bus.get_object(__dService, __dObjectPath)
__deviceInterface = dbus.Interface(__proxy_obj_NetworkManager, __dInterface)
__mainloop = gobject.MainLoop()
# generell stuff
def getDevices():
"""
Returns a list of available network interface devices
"""
devices = []
for device in dbus.Interface(__proxy_obj_NetworkManager, __dInterface).getDevices():
devices.append(device.replace(__deviceLocationCore,""))
return devices
def getConnectionStatus():
"""
Should return corrent connection status (connecting/connected/scanning/disconnected)
Couldn't decifer status messages yet
"""
return dbus.Interface(__proxy_obj_NetworkManager, __dInterface).state()
def getActiveDevice():
"""
corrently not working for unknown reason
"""
return dbus.Interface(__proxy_obj_NetworkManager, __dInterface).getActiveDevice()
# device stuff
def __deviceInterface(device):
"""
Creates a Device map. Mostly used for internal stuff
"""
return dbus.Interface(__session_bus.get_object(__dInterface, __deviceLocationCore + device), __deviceInt)
def getDeviceType(device):
"""
Returns type of device
"""
if (__deviceInterface(device).getType() == 1):
return "wired"
elif (__deviceInterface(device).getType() == 2):
return "wireless"
else:
return "unknown"
def getDeviceHalUdi(device):
"""
Returns devices HAL-UDI
"""
return __deviceInterface(device).getHalUdi().replace(__HALLocationCore,"")
def getDeviceName(device):
"""
Returns device name
not really usefull
"""
return __deviceInterface(device).getName()
def getDecviceLinkActive(device):
"""
Returns if link is active or not
"""
return __deviceInterface(device).getLinkActive()
def getDeviceActiveNetwork(device):
"""
Returns name of corrently active network
"""
return __deviceInterface(device).getActiveNetwork()
def getDeviceNetworks(device):
"""
Returns list of networks available using specified device
"""
return __deviceInterface(device).getNetworks()
def getDeviceIPv4(device):
"""
Returns devices IP-Address (v4)
"""
# most usefull stuff is saved in one big tuple - whos ingenious idea was that?
# return __deviceInterface(device).getIP4Address()
return __deviceInterface(device).getProperties()[6]
def getDeviceIPv4Netmask(device):
"""
Returns devices IP-Netmask (v4)
"""
return __deviceInterface(device).getProperties()[7]
def getDeviceIPv4Broadcast(device):
"""
Returns devices IP-Broacastaddress (v4)
"""
return __deviceInterface(device).getProperties()[8]
def getDeviceMAC(device):
"""
Returns devices MAC-Address
"""
return __deviceInterface(device).getProperties()[9]
def getDeviceIPv4DefaultGateway(device):
"""
Returns devices default IP-Gateway (v4)
"""
# not sure about the next four items, but I guess that's what they are
return __deviceInterface(device).getProperties()[10]
def getDeviceIPv4PrimaryDNS(device):
"""
Returns devices primary IP-DNS-Server (v4)
"""
return __deviceInterface(device).getProperties()[11]
def getDeviceIPv4SecondaryDNS(device):
"""
Returns devices secondary IP-DNS-Server (v4)
"""
return __deviceInterface(device).getProperties()[12]
# network stuff
def __networkInterface(device, network):
"""
Creates a network map. Mostly used for internal stuff
"""
return dbus.Interface(__session_bus.get_object(__dInterface, __deviceLocationCore + device+ '/Networks/' + network), __deviceInt)
def getNetworkName(device, network):
"""
Returns network name
"""
return __networkInterface(device, network).getName()
def getNetworkBasestation(device, network):
"""
Returns the MAC-Address of the active base station
"""
return __networkInterface(device, network).getAddress()
def getNetworkStrength(device, network):
"""
Returns the Network strength as percentage
"""
return __networkInterface(device, network).getStrength()
def getNetworkFrequency(device, network):
"""
Returns the Networka frequency in GHz
"""
return __networkInterface(device, network).getFrequency()
def getNetworkRate(device, network):
"""
Returns the Networks maximum supported data rate in Mbps
"""
return __networkInterface(device, network).getFrequency()
def getNetworkEncrypted(device, network):
"""
Returns weather the network is encrypted
"""
return __networkInterface(device, network).getEncrypted()
def connect(device, network=None):
"""
For wired networks the given device is activated
For wireless networks it will connect to the given network using the given device
"""
if (network == None and getDeviceType(device) == "wired"):
__deviceInterface.setActiveDevice(dbus.ObjectPath(__deviceLocationCore + device))
elif (network != None and getDeviceType(device) == "wireless"):
__deviceInterface.setActiveDevice(dbus.ObjectPath(__deviceLocationCore + device), dbus.String(network))
else:
pass
# signal handling
# variable must be set to a function
onDeviceNoLongerActive = None #Parameter: device
onDeviceNowActive = None #Parameter: device
onDeviceActivating = None #Parameter: device
onDevicesChanged = None #Parameter: device
onDeviceActivationFailed = None #Parameter: device, network
onStrengthChanged = None #Parameter: device, strength
def __DeviceNoLongerActive(device):
if (onDeviceNoLongerActive != None):
onDeviceNoLongerActive(device.replace(__deviceLocationCore,""))
def __DeviceNowActive(device):
if (onDeviceNoLongerActive != None):
onDeviceNoLongerActive(device.replace(__deviceLocationCore,""))
def __DeviceActivating(device):
if (onDeviceNoLongerActive != None):
onDeviceNoLongerActive(device.replace(__deviceLocationCore,""))
def __DevicesChanged(device):
if (onDeviceNoLongerActive != None):
onDeviceNoLongerActive(device.replace(__deviceLocationCore,""))
def __DeviceActivationFailed(device, network):
if (onDeviceNoLongerActive != None):
onDeviceNoLongerActive(device.replace(__deviceLocationCore,""), network)
def __strengthChanged(device, strength):
if (onStrengthChanged != None):
onStrengthChanged(device.replace(__deviceLocationCore,""), strength)
__session_bus.add_signal_receiver(__DeviceNoLongerActive, 'DeviceNoLongerActive')
__session_bus.add_signal_receiver(__DeviceNowActive, 'DeviceNowActive')
__session_bus.add_signal_receiver(__DeviceActivating, 'DeviceActivating')
__session_bus.add_signal_receiver(__DevicesChanged, 'DevicesChanged')
__session_bus.add_signal_receiver(__DeviceActivationFailed, 'DeviceActivationFailed')
__session_bus.add_signal_receiver(__strengthChanged, 'DeviceStrengthChanged')
def startSignalHandlingLoop():
"""
Starts a gobject Mainloop for signal handling
"""
__mainloop.run()
def stopSignalHandlingLoop():
"""
Stops signal handling mainloop
"""
__mainloop.stop()
This is just a little test program:
testsuite.py
import pynm2
print pynm2.getDevices()
def doet(bla, blubb):
print bla
print blubb
pynm2.onStrengthChanged = doet
pynm2.startSignalHandlingLoop()
print "test"
I'm going to write a UI for AWN as soon as the backend is ready.