#!/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'` 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' | sed 's/ //g'` echo Channel=$CHANNEL # Connect echo 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