Feb 8

We got many keys for some wifi APs around, so we put them in a delimited text file like:


ap8599:5537801570
sd6980:5202140314
wq0858:5953230520


now, when we are around, we want a script that we can tell: "connect to ap6980 if you see it around, if you need a key, take it from the text file". Called without arguments, it scans all available networks, and tries to connect to all of the ones that we have a key or that are open.


This is our connection script:


#!/bin/bash

ap=$@
i=wlan0
en=0

#------------------------------------------------------------------------------------------------------------------
#  Scan for wireless networks, and pretty print the quality, the essid, and if we require encryption
#-----------------------------------------------------------------------------------------------------------------
scanw () {
i=$1
(       ifconfig $i up
        iwlist $i scan | egrep -i 'essid|freq|qual|encr' |nawk 'ORS=NR%4?" ":"\n"'| tr -s ' '| while read l;do
                AP=`echo "$l "|awk '{s=substr($0,index($0,"ESSID:")+7);print substr(s,1,index(s,"\"")-1)}'`
                EN=`echo "$l "|awk '{s=substr($0,index($0,"Encryption key:")+15);print substr(s,1,index(s," ")-1)}'`
                QU=`echo "$l "|awk '{s=substr($0,index($0,"Quality=")+8);print substr(s,1,index(s,"/")-1)}'`
                K=`grep "^$AP" keys.txt| awk -F':' '{ print $2 }'`
                echo "$QU:$EN:$AP:$i:$K"
        done
) | sort -n
}

#-----------------------------------------------------------------------------------------------------------------
# Connect to the accespoint determined by the "$ap" variable
#-----------------------------------------------------------------------------------------------------------------
conn(){
L=`grep -i "$ap" ~/keys.txt| head -1`
C=`echo -n $L | wc -c`
indb=1
if [ $C -lt 1 ];then
#       echo "------------- WARNING: AP $ap not found in database"
        indb=0
fi
S=`grep "$ap" /tmp/scan.$$|wc -c`
if [ $S -lt 1 ];then
        echo "------------- ERROR: AP $ap is not in range, cannot connect"
        exit 1
fi
if [ $indb -eq 1 ];then
        #---if we found an accesspoint in our database work with it
        AP=`echo $L | awk -F':' '{ print $1 }'`
        K=`echo $L | awk -F':' '{ print $2 }'`
        echo "------------- Connecting to $AP with key $K"
        iwconfig $i mode managed key $K essid "$AP" rate auto
else
        AP=`grep "$ap" /tmp/scan.$$|awk -F':' '{ print $3 }'`
        en=`grep "$AP" /tmp/scan.$$|awk -F':' '{ print $2 }'`
        if [ $en == "off" ];then
                echo "------------- Connecting to $AP without key"
                iwconfig $i mode managed key off essid "$AP" rate auto
        else
                echo "------------ ERROR: Encryption needed for $AP but i don't have the key"
                return 1
        fi
fi
x=0
echo -n "------------- Associating with $AP"
while [ $x -lt 10 ];do
        A=`iwconfig $i | grep -i 'Not-Associated'|wc -c`
        if [ ${A} -eq 0 ];then
                echo "";echo "------------- Associated to $AP !!!"
                dhclient -q -r $i
                dhclient $i
                exit 0
        else
                echo -n "."
        fi
        sleep 1
        x=`echo "$x + 1" | bc`

done
echo "";echo "------------- ERROR: Could not associate"
}

#-------------------------------------------------------------------------------------------------------------------
# Connect to any accesspoint we can sniff
#-------------------------------------------------------------------------------------------------------------------
auto () {
        cat /tmp/scan.$$ | awk -F':' '{ print $3 }' |while read ap;do
                echo "---------- trying to connect to $ap"
                conn $ap
        done
}


#-------------------------------------------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------------------------------------------
ifconfig -a | egrep -i 'mon|wlan' | awk '{ print $1 }' |while read i;do airmon-ng stop $i; done > /dev/null 2>&1
ifconfig $i down
ifconfig $i up
scanw $i > /tmp/scan.$$
cat /tmp/scan.$$
if [ $# -lt 1 ];then
        echo "-------- WARNING: no Accesspoint specified, will try to connect to the ones i see alive"
        auto
else
        conn
fi
rm /tmp/scan.$$


Posted by rdircio

| Top Exits (0)
Vote for articles fresher than 7 days!
Current karma: none, 0 vote(s) 225 hits

View as PDF: This entry | This month | Full blog

0 Trackbacks

  1. No Trackbacks

0 Comments

Display comments as(Linear | Threaded)
  1. No comments

Add Comment


Enclosing asterisks marks text as bold (*word*), underscore are made via _word_.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA