Wireless access points helper script

From SHellium Wiki
Jump to: navigation, search
Geographylogo.png In other languages: English | Afrikaans | Albanian | Arabic | Brazilian | Bulgarian | Catalan | Chinese | Croatian | Czech | Danish | Dutch | Esperanto | Estonian | Filipino | Finnish | Flemish | French | German | Greek | Hebrew | Hindi | Hungarian | Indonesian | Italian | Japanese | Latvian | Lithuanian | Macedonian | Malay | Malayalam | Norwegian (Bokmål) | Norwegian (Nynorsk) | Persian | Polish | Portuguese | Romanian | Russian | Serbian | Slovak | Slovenian | Spanish | Swedish | Turkish | Ukrainian | Urdu

Contents

Description

This script is meant as a facilitator for those of us who don't like to use networkmanager to handle their wireless connections. It parses the output of iwlist, and, along with other executables, compiles initially a list of visible access points, then, according to a choice of access point, it outputs the corresponding wpa_supplicant.conf entry, so what is needed is just to paste the "network={ ... }" output on wpa_supplicant.conf.

Dependencies

It depends on:

  • Linux, of course.
  • Python 3
  • wpa_supplicant
  • wireless_tools

License and misc

This script was written by Sumeniac and may be freely used, distributed and edited.

Please contact me on the irc channel to chat about it (bugs, suggestions, etc). I wrote this script some 2 years ago, and just re-factored, it would be nice to have some feedback.

Execution example

sumeniac@lages $ wifi_helper.py wlan0

Avaible access points listed bellow.


Acess point number 0:

	Encryption Key = on
	Quality = 21/70
	ESSID = "republica"
	Encryption Type = WPA

Connected to ESSID:"republica".


Which acess point would you like to connect to (or 'e' to exit)? (0-0) 0
What's the passphrase? (de 8 à 63 char) test1234

network={                     
	ssid="republica"
	proto=WPA                     
	key_mgmt=WPA-PSK                     
	pairwise=CCMP TKIP                     
	group=CCMP TKIP WEP104 WEP40                     
	psk=30290c2c3e2cf14c198d032da885c9267a31a711210babc2e0d0527dbd9cd6b7
	priority=3                  
}

Paste 'network' entry on wpa_supplicant.conf.

sumeniac@lages $

Script (wifi_helper.py)

#!/usr/bin/python3

#################################################
#author: Sumeniac                               #
#contact:                                       #
#   *irc: chat.freenode.net #shellium           #
#date: refactored on march 2011                 #
#                                               #
#notes: This script is inteded as slight        #
#break on looking up wireless access points     #
#and writing the corresponding wpa_supplicant   #
#entries.                                       #
#                                               #
#dependencies:                                  #
#       *Python 3                               #
#       *wireless-tools                         #
#       *wpa_supplicant                         #
#################################################

import re
import subprocess
import time
import tempfile
import sys
import os

#Variables containing paths of the necessary executables.
iwlist_path="/sbin/iwlist"                      #wireless-tools package
iwconfig_path="/sbin/iwconfig"                  #wireless-tools package
wpa_passphrase_path="/usr/bin/wpa_passphrase"   #wpa_supplicant package

#Tests for interface argument, defaults to 'wlan0'.
if (len(sys.argv) > 1):
    interface = sys.argv[1]
else:
    interface = "wlan0"

def check_for_files():
#Checks if the necessary executables are present.

    if not os.path.isfile(iwlist_path):
        print("iwlist not found on /sbin/, please specify correct path on script.")
        sys.exit()

    if not os.path.isfile(iwconfig_path):
        print("iwconfig not found on /sbin/, please specify path on script.")
        sys.exit()

    if not os.path.isfile(wpa_passphrase_path):
        print("wpa_passphrase not found on /usr/bin/, please specify path on script.")
        sys.exit()

    return True

def acquire_spots(interface) :
#Parses iwlist output and extracts visible access points. Returns list of
#dictionaries containing data for each points found.

    buff_file = tempfile.TemporaryFile(mode='w+')

    command=iwlist_path + " " + interface + "  scanning"
    p = subprocess.Popen(command, stdout=buff_file, shell=True)
    p.wait()
    
    while True :

        buff_file.seek(0)
        first_line = buff_file.readline()

        ok = "Scan completed"
        busy = "Interface doesn't support scanning : Device or resource busy"
        network_down = "Failed to read scan data : Network is down"
   
        if (busy in first_line) :
            time.sleep(1)
            buff_file.close()
            buff_file = tempfile.TemporaryFile()
            p = subprocess.Popen("/sbin/iwlist wlan0 scanning",stdout=buff_file, shell=True)
            p.wait()
        elif (network_down in first_line):
            print("Wireless network down.")
            buff_file.close()
            sys.exit()
        elif (ok in first_line):
            break
        else : 
            print("Something unexpected happened. Please send output to Sumeniac.")
            print(first_line)
            for line in buff_file:
                print(line)
            sys.exit()
    
    spots = list()
    x=-1 
    matches = { \
            'ESSID' : re.compile('ESSID:(".+")'), \
            'Quality' : re.compile('Quality=(\d+/\d+)'), \
            'Encryption Key' :  re.compile('Encryption key:(.+)'), \
            'Encryption Type' : re.compile('(WPA)') \
            }
    
    for line in buff_file :
        if ("Cell" in line):
            x = x+1
            spots.append(dict())
            continue
    
        if x>=0 :
            temp = spots[x]
            for field, regex in matches.items():
                match = regex.search(line)
                if match:
                    temp[field] = match.group(1)

    buff_file.close()                
    
    return spots
    
def print_spots(spots) :
#Prints the access points found.

    if len(spots)>0:
        print ("\nAvaible access points listed bellow.\n")
        
        for i, spot in enumerate(spots):
            print ("\nAcess point number {0}:\n".format(i))      
            
            for key in iter(spot.keys()):
                print("\t" + key + " = " + spot[key])
    else:
        print("No avaible access points.")
        sys.exit()

def compile_conf_entry(spots, interface): 
#Compiles (outputs) an wpa_supplicant.conf entry for the choosen access point.

    buff_file = tempfile.TemporaryFile(mode='w+')

    command=iwlist_path + " " + interface
    p = subprocess.Popen("/sbin/iwconfig wlan0",stdout=buff_file, shell=True)
    p.wait()
    buff_file.seek(0)

    essid_regex = re.compile('ESSID:".*"')

    for line in buff_file:
        essid_match = essid_regex.search(line)
        if essid_match:
            if ( essid_match.group() != 'ESSID:""' ):
                print ("\nConnected to {0}.\n".format(essid_match.group()))
            else: 
                print ("\nNot connected to any acess point.\n")
            break

    buff_file.close()

    option = input("\nWhich acess point would you like to connect to (or 'e' to exit)? (0-{0}) ".format(len(spots)-1))
    if (option == 'e'):
        sys.exit()
    spot = spots[int(option)]

    essid=spot['ESSID']

    if ('Encryption Type' in spot):
        buff_file = tempfile.TemporaryFile(mode='w+')
        command=wpa_passphrase_path + " " + essid + " " + \
                input("What's the passphrase? (de 8 à 63 char) ")

        p = subprocess.Popen(command,stdout=buff_file, shell=True)
        p.wait()
        buff_file.seek(0)

        for line in buff_file:
            p = re.match("^\s+(psk=.+)", line)
            if p:
                pskKey = p.group(1)
                break

        buff_file.close()
        string = "network={ \
                    \n\tssid=" + essid + \
                    "\n\tproto=WPA \
                    \n\tkey_mgmt=WPA-PSK \
                    \n\tpairwise=CCMP TKIP \
                    \n\tgroup=CCMP TKIP WEP104 WEP40 \
                    \n\t"+ pskKey + \
                    "\n\tpriority=3 \
                 \n}"

    elif ('Encryption Type' not in spot and "on" in spot['Encryption key']):
        wepKey=input("What's the WEP key? ")
        string = "network={ \
                        \n\tssid=" + essid + \
                        "\n\tkey_mgmt=NONE \
                        \n\twep_key0=" + wepKey + \
                        "\n\tpriority=3 \
                    \n}"
    elif ("off" in spot['Encryption key']):
        string = "network={ \
                        \n\tssid="+ essid + \
                        "\n\tkey_mgmt=NONE \
                        \n\tpriority=3 \
                    \n}"
    else:
        string = "Error. Debug."

    print ('\n'+ string + "\n\nPaste 'network' entry on wpa_supplicant.conf.\n")

#Main:
if check_for_files():
    spots = acquire_spots(interface)
    print_spots(spots)
    compile_conf_entry(spots, interface)
    sys.exit()
Personal tools
Namespaces

Variants
Actions
Navigation
Indexes
SHellium Sites
Toolbox