
# scent.py, a simple wifi AP browser 
# It allows you to select a card, select and set an access-point based on signal strength and request an IP.
#
# Copyright Julian Oliver <julian at selectparks dot net>
#
# Requires wireless-tools from http://www.hpl.hp.com/personal/Jean_Tourrilhes/Linux/Tools.html or via your distro.
# If you've been using wifi, you already have it.
# I didn't want to handle passwords here, so run as root or use sudo.
#
# To run just 'python scent.py'
#
# [updated 23.04.05 to accomodate version differences in iwlist output]
# 
# This program 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 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
# Place - Suite 330, Boston, MA 02111-1307, USA, or go to
# http://www.gnu.org/copyleft/lesser.txt.

import os, sys, re, string

x = []
count = 0
list = []
ap = 0
apList = []
apIndex = 0
n = 0
strList = []

setCard = raw_input("choose your wireless interface (eg eth0, eth1): ")
scan = os.popen("iwlist " +setCard +" scanning").read()
scanLines = os.popen("iwlist " +setCard +" scanning").readlines()
search = re.findall("ESSID", scan)

for p in scan:
    count = count+1
    if p == '"':
	d = str(count)
	x = x +string.split(d)

	for j in scanLines:
	    
	    # Depending on version, iwlist may capture 'RSSI' or 'Signal Level'.
	    # This is a workaround.
		if re.findall("RSSI", j):
			for k in re.findall("RSSI", j):
				strList = strList +string.split(string.strip(j)[12:16])
		else:
			for k in re.findall("Signal level=", j):
				strList = strList +string.split(string.strip(j)[29:32])

if len(search) >= 1:

    for h in search:
	ap = ap+2
	apIndex = apIndex +1
	r = 'r'+string.join(str(ap))
	r = scan[(int(x[ap-2])):((int(x[ap-1]))-1)]
	apList = apList +string.split(r)
	print "found:", apIndex, '"',apList[apIndex -1],'"', "of signal strength", strList[apIndex -1], "dBm"

    accessPoint = raw_input("select an access point: ")

    for choice in apList:
	os.popen("iwconfig " +setCard +" essid " + apList[(int(accessPoint))-1])
    
    yesno = raw_input("do you want me to ask for an IP? (Yy/Nn)\n")
    
    while 1:
	if yesno == 'Y' or 'y':
	    os.popen("ifdown " +setCard +"&& ifup " +setCard)
	    print "ok... done"
	    raise SystemExit
	elif yesno == 'N' or 'n':
	    print "ok... hasta luego"
	    raise SystemExit
	else:
	    yesno = raw_input("do you want me to look for an IP? (Yy/Nn)\n")
  
else:
    print "no access points found"
    raise SystemExit
