Tiny draft notification area icon that wraps wpa for Gnome

Hugh Perkins hughperkins
Sun Jan 3 17:42:20 PST 2010


Here's a very draft notification area icon that wraps wpa for Gnome.

Compared to wpagui, it is very incomplete, but I feel much simpler to
use, and only implements what I feel are very important
functionalities: showing the connection status, available networks,
and allowing wifi activation/deactivation.

Compared to gnome-network-manager, it works directly with
wpa_supplicant.conf, rather than implementing an entirely different
orthogonal set of configuration.  So, my notification icon is
compatible with using wpa_supplicant also from the command-line, with
X shutdown.

It's very short, and simple, so I'll just paste it here.  Simply paste
it into a script.  It needs to run as sudo for now, so please check
that it doesn't do any nefarious before you run it, use it at your own
risk, your milage may vary etc...  Ideally it would be fissioned into
a daemon part and a front-end part, but that can be for the future.

Possible future enhancements:
- fission into a daemon, and a front-end unprivileged client
- make it possible to configure connections to the available networks

----------------------------------------------------------

#!/usr/bin/python

# Copyright Hugh Perkins 2009
# hughperkins at gmail.com http://manageddreams.com
#
# 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 in the file licence.txt; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
# 1307 USA
# You can find the licence also on the web at:
# http://www.opensource.org/licenses/gpl-license.php
#
# ======================================================================================
#

import os
import sys
import gtk
import time
import subprocess

class MyTray(object):
   def __init__(self):
      self.statusIcon = gtk.StatusIcon()
      self.menu = gtk.Menu()
      self.getstatus()
      self.showstatus()
      self.popupmenu = gtk.Menu()
      menuitem = gtk.MenuItem('Activate wifi')
      menuitem.connect('activate', self.activateWifi, None )
      self.popupmenu.append( menuitem )
      menuitem = gtk.MenuItem('Disable wifi')
      menuitem.connect('activate', self.disableWifi, None )
      self.popupmenu.append( menuitem )
      self.statusIcon.connect('activate', self.activate_cb, self.menu )
      self.statusIcon.connect('popup-menu', self.popupmenu_cb, None )
      self.statusIcon.set_visible(True)
      self.statusIcon.set_visible(1)
      gtk.main()

   def activateWifi( self, widget, data = None ):
      popen = subprocess.Popen(['gnome-terminal', '-x',
'/sbin/wpa_cli', 'reconnect'], stdout = subprocess.PIPE)
      popen.wait()
      popen = subprocess.Popen(['gnome-terminal', '-x',
'/sbin/dhclient', self.interface], stdout = subprocess.PIPE)
      popen.wait()
      self.getstatus()
      self.showstatus()

   def disableWifi( self, widget, data = None ):
      popen = subprocess.Popen(['gnome-terminal', '-x',
'/sbin/wpa_cli','disconnect'], stdout = subprocess.PIPE)
      popen.wait()
      self.getstatus()
      self.showstatus()

   def activate_cb(self, widget, data = None ):
      self.getstatus()
      self.showstatus()
      self.menu.show_all()
      et = gtk.get_current_event_time()
      self.menu.popup(None, None, gtk.status_icon_position_menu, 1,
et, self.statusIcon )

   def popupmenu_cb(self, widget, button, time, data = None ):
      self.popupmenu.show_all()
      self.popupmenu.popup(None, None, None, 3, time )

   def showstatus(self):
      self.statusIcon.set_from_stock(gtk.STOCK_NO)
      if self.wpa_state == 'COMPLETED':
         self.statusIcon.set_from_stock(gtk.STOCK_CONNECT)
      self.statusIcon.set_tooltip(str(self.wpa_state) + ' ' + str(self.ssid) )
      self.menu = gtk.Menu()
      for net in self.availablenets:
         if net != '':
            netname = net.split('\t')[4]
            if netname == self.ssid:
               menuItem = gtk.ImageMenuItem(gtk.STOCK_CONNECT, netname)
               menuItem.set_label(netname)
               self.menu.append(menuItem)
            else:
               menuItem = gtk.MenuItem(netname)
               self.menu.append(menuItem)

   def getstatus(self):
      popen = subprocess.Popen(['/sbin/wpa_cli','status'], stdout =
subprocess.PIPE)
      popen.wait()
      (stdout,stderr) = popen.communicate(None)
      self.ssid = None
      self.wpa_state = None
      self.interface = None
      for line in stdout.split('\n'):
         if line.startswith('Selected interface'):
            self.interface = line.split("'")[1]
            print self.interface
         key = line.split('=')[0]
         if key == 'wpa_state':
            self.wpa_state = line.split('=')[1]
         if key == 'ssid':
            self.ssid = line.split('=')[1]

      popen = subprocess.Popen(['/sbin/wpa_cli','scan_results'],
stdout = subprocess.PIPE)
      popen.wait()
      (stdout,stderr) = popen.communicate(None)
      self.availablenets = stdout.split('\n')[2:]

mytray = MyTray()

----------------------------------------------------------



More information about the Hostap mailing list