You are not logged in.

#1 2006-03-15 07:20:24

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Bash script to connect to strongest unencrypted wifi AP

Here's a little bash script that looks at the available wifi APs that your card can see, and automatically connects you to the AP with the highest quality and that is also unencrypted. I'm just posting it in case anyone wants to improve upon it or use it. And yes, my bash knowledge kinda sucks.

Just call it like "wifi.sh wlan0" as root.

#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
interface=$1
iwlist $interface scan > blah
NumAPs=`cat blah | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
    # Check if AP is encrypted
    Encryption=`cat blah | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
    if [ $Encryption = "off" ]; then
        # Find AP with the highest quality
        QUALITY=`cat blah | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
        if [ $QUALITY -gt $BestQuality ]; then
            BestQuality=$QUALITY
            BestAP=$i
        fi
    fi
done
if [ $BestAP -gt 0 ]; then
    # Yay, we found an unencrypted AP:
    echo Connecting to...
    ESSID=`cat blah | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
    echo ESSID=$ESSID
    MODE=`cat blah | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
    echo Mode=$MODE
    CHANNEL=`cat blah | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
    echo Channel=$CHANNEL
    # Connect
    iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
    if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
        rm /etc/dhcpc/dhcpcd-${interface}.pid
    fi
    dhcpcd $interface        
    # Cleanup
    rm blah
fi

I am a gated community.

Offline

#2 2006-03-15 14:44:21

_Gandalf_
Member
Registered: 2006-01-12
Posts: 735

Re: Bash script to connect to strongest unencrypted wifi AP

Thx for the script, i add some little *fancy* stuff to it

#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
TEMP=/tmp/bestap.tmp
LOCK=/var/lock/bestap.lock
if [ `whoami` != "root" ];then
        echo "Sorry, you need to be root to run this program"
        exit 1
fi

if [[ -z $1 ]];then
        echo "USAGE: $0 device"
        exit 1
else
        interface=$1
fi

# Checking for lock
if [[ -e $LOCK ]];then
        exit 1; # Too simply nothing to do here :)
else
        touch $TEMP $LOCK
fi


# Proggy
iwlist $interface scan > $TEMP
NumAPs=`cat $TEMP | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
   # Check if AP is encrypted
   Encryption=`cat $TEMP | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
   if [ $Encryption = "off" ]; then
      # Find AP with the highest quality
      QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
      if [ "$QUALITY" -gt "$BestQuality" ]; then
         BestQuality=$QUALITY
         BestAP=$i
      fi
   fi
done
if [ $BestAP -gt 0 ]; then
   # Yay, we found an unencrypted AP:
   echo Connecting to...
   ESSID=`cat $TEMP | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
   echo ESSID=$ESSID
   MODE=`cat $TEMP | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
   echo Mode=$MODE
   CHANNEL=`cat $TEMP | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
   echo Channel=$CHANNEL
   # Connect
   iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
   if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
      rm /etc/dhcpc/dhcpcd-${interface}.pid
   fi
   dhcpcd $interface
   # Cleanup
fi
rm -f $TEMP $LOCK

Offline

#3 2006-03-16 02:46:35

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Re: Bash script to connect to strongest unencrypted wifi AP

Thanks _Gandalf_! I was planning on eventually looking into how to verify if the user is root... smile

I also have no idea if it is actually finding the "strongest" AP or not. For whatever reason, iwlist always shows 0/100 for the Quality of every AP. Ah well.


I am a gated community.

Offline

#4 2006-03-16 06:21:08

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Bash script to connect to strongest unencrypted wifi AP

You might find it worth mentioning what chipset your card uses.

Some chipsets report it entirely differently, others dont report it at all. ndiswrapper doesnt.

Offline

#5 2006-03-16 15:41:14

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Re: Bash script to connect to strongest unencrypted wifi AP

Ah yes, it's ndiswrapper. It never dawned on me that that would have something to do with it, thanks for the info iphitus.


I am a gated community.

Offline

#6 2006-03-19 20:34:40

syntax
Member
From: Saint-Truiden
Registered: 2006-03-04
Posts: 19
Website

Re: Bash script to connect to strongest unencrypted wifi AP

this script will sure come in handy on the road, I was looking for something similar before and I'm too lazy to write on myself ^^


-kupo-

Offline

#7 2006-03-23 12:53:28

Sigi
Member
From: Thurgau, Switzerland
Registered: 2005-09-22
Posts: 1,131

Re: Bash script to connect to strongest unencrypted wifi AP

Any volunteers for making a "Bash script to connect to strongest encrypted wifi AP"?   wink

Cheers Sigi


Haven't been here in a while. Still rocking Arch. smile

Offline

#8 2006-03-29 03:56:11

fedaykin
Member
Registered: 2004-12-02
Posts: 25

Re: Bash script to connect to strongest unencrypted wifi AP

This script is awesome, thanks guys.  I always had trouble where I would connect to a network and then drop every few minutes and have to reconnect, but I've been connected now for about an hour without any problems   big_smile

Offline

#9 2006-03-30 14:49:47

codemac
Member
From: Cliche Tech Place
Registered: 2005-05-13
Posts: 794
Website

Re: Bash script to connect to strongest unencrypted wifi AP

Sweet script.  Is there a way to exclude one AP?  The problem being my school has campus wide wifi which is nice, but also clogged with ton's of kids who suck and are using it the same time I am, so I want to find the n00bs with open wifi on their new routers that they haven't secured.  It may be lower quality, but it's probably going to be faster for me.

Extending this script to read a preference file or something would be cool.  Priorities like wpa_supplicant. mmmm

[edit]
@Sigi:  It's my understanding that wpa_supplicant can do that for you.

Offline

#10 2006-08-25 23:58:17

jakob
Member
From: Berlin
Registered: 2005-10-27
Posts: 419

Re: Bash script to connect to strongest unencrypted wifi AP

Trying this at home, I encounter the following error:

jakob@scripts $ s ./wconnect ath0
./wconnect: line 39: [: Quality=10: integer expression expected

my full iwlist ath0 scan looks like so:

ath0      Scan completed :
          Cell 01 - Address: 00:04:0E:6C:4E:9C
                    ESSID:"keller"
                    Mode:Master
                    Frequency:2.412 GHz (Channel 1)
                    Quality=10/94  Signal level=-86 dBm  Noise level=-95 dBm
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 22 Mb/s
                              6 Mb/s; 9 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s
                              36 Mb/s; 48 Mb/s; 54 Mb/s
                    Extra:bcn_int=100

Can't believe, that I'm the only one with that error?

Offline

#11 2006-08-26 01:39:44

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Re: Bash script to connect to strongest unencrypted wifi AP

For whatever reason, your quality line uses an equal sign whereas every other line of your scan has a colon (and most people have a colon for every line). Too weird..

Anyway, give this a shot:

#!/bin/bash
# Finds the strongest unencrypted AP and tries to connect to it via dhcp
# Call this script like "wifi.sh wlan0"
TEMP=/tmp/bestap.tmp
LOCK=/var/lock/bestap.lock
if [ `whoami` != "root" ];then
        echo "Sorry, you need to be root to run this program"
        exit 1
fi

if [[ -z $1 ]];then
        echo "USAGE: $0 device"
        exit 1
else
        interface=$1
fi

# Checking for lock
if [[ -e $LOCK ]];then
        exit 1; # Too simply nothing to do here :)
else
        touch $TEMP $LOCK
fi

isNotInteger()
{
    x=$1
    case $x in
        *[!0-9])
            return 0 ;;
        *)
            return 1 ;;
    esac
} 

# Proggy
iwlist $interface scan > $TEMP
NumAPs=`cat $TEMP | grep ESSID | wc -l`
BestAP=0
BestQuality=-1
for i in `seq 1 $NumAPs`;
do
   # Check if AP is encrypted
   Encryption=`cat $TEMP | grep Encryption | head -n$i | tail -n1 | cut -d":" -f2`
   if [ $Encryption = "off" ]; then
      # Find AP with the highest quality
      QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d":" -f2 | cut -d"/" -f1 | sed 's/ //g'`
      if isNotInteger "$QUALITY"; then
        # If we didn't find an integer, try this instead:
        QUALITY=`cat $TEMP | grep Quality | head -n$i | tail -n1 | cut -d"=" -f2 | cut -d"/" -f1 | sed 's/ //g'`
      fi
      if [ "$QUALITY" -gt "$BestQuality" ]; then
         BestQuality=$QUALITY
         BestAP=$i
      fi
   fi
done
if [ $BestAP -gt 0 ]; then
   # Yay, we found an unencrypted AP:
   echo Connecting to...
   ESSID=`cat $TEMP | grep ESSID | head -n$BestAP | tail -n1 | cut -d""" -f2`
   echo ESSID=$ESSID
   MODE=`cat $TEMP | grep Mode | head -n$BestAP | tail -n1 | cut -d":" -f2`
   echo Mode=$MODE
   CHANNEL=`cat $TEMP | grep Channel | head -n$BestAP | tail -n1 | cut -d"(" -f2 | sed 's/Channel //g' | sed 's/)//g'`
   echo Channel=$CHANNEL
   # Connect
   iwconfig $interface essid $ESSID mode $MODE channel $CHANNEL
   if [ -e /etc/dhcpc/dhcpcd-${interface}.pid ]; then
      rm /etc/dhcpc/dhcpcd-${interface}.pid
   fi
   dhcpcd $interface
   # Cleanup
fi
rm -f $TEMP $LOCK

I am a gated community.

Offline

Board footer

Powered by FluxBB