#!/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
This is the place where I leave my scripts, my useful files, glad to have your suggestions. If you want to make discuss, please contact me at YM id bornbygoogle or my mail bornbygoogle@yahoo.com or simply let ur mail. I'll try to contact as soon as possible :) Nice to see you :D
Wednesday, March 19, 2008
Bash script to connect to strongest unencrypted wifi AP
Subscribe to:
Post Comments (Atom)
Convert Megaupload links to Rapidshare links !
If someone want to convert Megaupload to Rapidshare links, please let me know, I'll do it for you ! ( for free, all ). If you want, please send those links to me, with the mail title : Convert to Rapidshare !. My mail is bornbygoogle@yahoo.com. Or YM ID : bornbygoogle.
Nice to help you ! :)
Nice to help you ! :)
No comments:
Post a Comment