Wifiweasel

From Openmoko

(Difference between revisions)
Jump to: navigation, search
(comments)
Line 7: Line 7:
 
whole process repeats itself; this requires the timeout script below (needs to be tested with multiple AP's - might have an iwconfig essid glitch).
 
whole process repeats itself; this requires the timeout script below (needs to be tested with multiple AP's - might have an iwconfig essid glitch).
 
<pre>  
 
<pre>  
 +
 
#!/bin/sh
 
#!/bin/sh
   #####################
+
   ##### WifiWeasel ######   Ver. 2
###########################
+
########################### 08-07-22
 
#/var/wifi/hacker/kd8ikt###
 
#/var/wifi/hacker/kd8ikt###
 
# OMG i'm programming now!#
 
# OMG i'm programming now!#
Line 15: Line 16:
 
###we.trekbeyond.org#######
 
###we.trekbeyond.org#######
 
   #####################
 
   #####################
        #KISS#
+
#TuperWare software inc #
 
+
#
# CONFIG
+
#Variables
#######interface################
+
IFACE=eth0
IFACE=eth0  
+
DHCP_TIMEOUT=3      # requires /usr/bin/timeout
#Dhcp_timeout
+
CHECK_NET_TIMER=15  #secs
DHCP=4
+
TEMPDIR=/mnt/wifiweasel/
#check connectivity every X sec
+
#
X=15
+
#Functions
 +
#
 +
#(dont touch unless you
 +
#know wtf your doing
 
#
 
#
# /CONFIG
+
#### Scan Funk
################################
+
#### Scan Funk ####
+
 
Func_iwlist_open () {
 
Func_iwlist_open () {
echo scanning wifi ;
 
 
iwlist $IFACE scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' \
 
iwlist $IFACE scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' \
 
     | sort -k4 -k3nr |grep -m1 off \
 
     | sort -k4 -k3nr |grep -m1 off \
|awk '{print $1}' > /tmp/weasel
+
|awk '{print $1}' > /tmp/wifiweasel/scan
cat /tmp/weasel |grep '"' -m1 > /tmp/weasel_
+
cat $TEMPDIR/scan |grep '"' -m1 > $TEMPDIR/scan1
sed /\"/s/\"//g /tmp/weasel_ > /tmp/ESSID
+
sed /\"/s/\"//g $TEMPDIR/scan1 > $TEMPDIR/ESSID
 +
echo
 +
echo Using ESSID
 +
cat $TEMPDIR/ESSID
 +
echo
 
}
 
}
#### iface up Funk ########
+
#### interface down Funk
Func_ifconfig_up () {
+
echo ; bring up interface
+
iwconfig $IFACE essid `cat /tmp/ESSID`
+
}
+
#### iface down Funk #####
+
 
Func_ifconfig_down () {
 
Func_ifconfig_down () {
echo killing iface and udhcpc
 
 
killall udhcpc  
 
killall udhcpc  
ifconfig $IFACE down
+
ifconfig $IFACE down
 
}
 
}
#### Conn Funk #####
+
#### iface up Funk
 +
Func_ifconfig_up () {
 +
iwconfig $IFACE essid `cat $TEMPDIR/ESSID`
 +
}
 +
 
 +
#### Connection check Funk
 
Func_chk_net () {
 
Func_chk_net () {
 
echo checking...   
 
echo checking...   
 
ping -w 2 -c1  $dns  
 
ping -w 2 -c1  $dns  
if [ $=0 ];then ERROR=0; echo; echo Online!  go download some pr0n ; echo; sleep $X; Func_chk_net ;  
+
if [ $=0 ];then ERROR=0; echo; echo Online!  W00T ; echo ; sleep $CHECK_NET_TIMER ; Func_chk_net ;  
fi  #W00T
+
fi  #W00T
 
if [ $=1 ] ; then ERROR=1 ;echo ;echo FAIL!\  
 
if [ $=1 ] ; then ERROR=1 ;echo ;echo FAIL!\  
... Searching ; Func_Main ;
+
... Searching ; echo ; Func_Main ;
 
fi  
 
fi  
 
  }
 
  }
#### Main Funk #########
+
 
 +
#### Main loop Funk
 
Func_MAIN () { while [ ERROR=1 ] ; do
 
Func_MAIN () { while [ ERROR=1 ] ; do
echo prime directive
 
 
Func_ifconfig_down ;
 
Func_ifconfig_down ;
 
Func_iwlist_open Func_ifconfig_up ;
 
Func_iwlist_open Func_ifconfig_up ;
Line 67: Line 71:
 
Func_MAIN ;
 
Func_MAIN ;
 
done;  
 
done;  
}  
+
}
#### MAIN ####
+
#Prep
echo 1st run
+
mkdir $TEMPDIR
Func_ifconfig_down ;
+
touch $TEMPDIR/scan
Func_iwlist_open Func_ifconfig_up ;
+
touch $TEMPDIR/scan1
udhcpc $IFACE ;
+
touch $TEMPDIR/ESSID
 +
# RunProgram
 +
Func_ifconfig_down  
 +
Func_iwlist_open Func_ifconfig_up  
 +
timeout $DHCP udhcpc $IFACE  
 
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
 
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
Func_chk_net ;
+
Func_chk_net
Func_MAIN ;
+
Func_MAIN  
 
echo This is the end, my only friend the end
 
echo This is the end, my only friend the end
 
Done; #actually i dont think it ever ends but i'm done writing it
 
Done; #actually i dont think it ever ends but i'm done writing it
#####################EOF#########################################
+
#####################EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#########
 +
 
 +
 
 +
 
 
</pre>
 
</pre>
  

Revision as of 04:30, 23 July 2008

script to Auto connect to Open 802.11 wireless AP's

Contents

wifiweasel

wifiweasel v1.5 - once run this script takes control of the wireless interface, then detects and connects to strongest open wifi AP in the vicinity. Subsequently it checks the connectivity every X seconds. If the connection fails, the whole process repeats itself; this requires the timeout script below (needs to be tested with multiple AP's - might have an iwconfig essid glitch).

 

#!/bin/sh
   ##### WifiWeasel ######   Ver. 2
########################### 08-07-22
#/var/wifi/hacker/kd8ikt###
# OMG i'm programming now!#
###########################
###we.trekbeyond.org#######
  #####################
#TuperWare software inc #
#
#Variables
IFACE=eth0
DHCP_TIMEOUT=3       # requires /usr/bin/timeout
CHECK_NET_TIMER=15   #secs
TEMPDIR=/mnt/wifiweasel/
#
#Functions
#
#(dont touch unless you 
#know wtf your doing
#
#### Scan Funk
Func_iwlist_open () {
iwlist $IFACE scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' \
    | sort -k4 -k3nr |grep -m1 off \
|awk '{print $1}' > /tmp/wifiweasel/scan
cat $TEMPDIR/scan |grep '"' -m1 > $TEMPDIR/scan1
sed /\"/s/\"//g $TEMPDIR/scan1 > $TEMPDIR/ESSID
echo 
echo Using ESSID
cat $TEMPDIR/ESSID
echo
}
#### interface down Funk
Func_ifconfig_down () {
killall udhcpc 
ifconfig $IFACE down
}
#### iface up Funk
Func_ifconfig_up () {
iwconfig $IFACE essid `cat $TEMPDIR/ESSID` 
}

#### Connection check Funk
Func_chk_net ()	{
echo checking...   
ping -w 2 -c1  $dns 
if [ $=0 ];then ERROR=0; echo; echo Online!  W00T ; echo ; sleep $CHECK_NET_TIMER ; Func_chk_net ; 
fi  #W00T
if [ $=1 ] ; then ERROR=1 ;echo ;echo FAIL!\ 
... Searching ; echo ; Func_Main ;
fi 
 }

#### Main loop Funk
Func_MAIN () { while [ ERROR=1 ] ; do
Func_ifconfig_down ;
Func_iwlist_open Func_ifconfig_up ;
timeout $DHCP udhcpc $IFACE ;
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
Func_chk_net ;
Func_MAIN ;
done; 
}
#Prep 
mkdir $TEMPDIR
touch $TEMPDIR/scan
touch $TEMPDIR/scan1
touch $TEMPDIR/ESSID
# RunProgram
Func_ifconfig_down 
Func_iwlist_open Func_ifconfig_up 
timeout $DHCP udhcpc $IFACE 
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
Func_chk_net  
Func_MAIN 
echo This is the end, my only friend the end
Done; #actually i dont think it ever ends but i'm done writing it
#####################EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#########



timeout

Added a timeout script to keep udhcpc from hanging up everything. As we all know, dhcp should not take 60sec; if it's not obtained a reply or a lease within a few seconds, you are likely not going to obtain an IP (perhaps due to range issues, mac filtering, etc).

INSTALL /usr/bin/timeout


#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
#From: gwc@root.co.uk (Geoff Clare)
#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?)
#Message-ID: <EoBxrs.223@root.co.uk>
#Date: Fri, 13 Feb 1998 18:23:52 GMT

#
# Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu
# UNTESTED
#

prog=${0##*/}
usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"

SIG=-TERM       # default signal sent to the process when the timer expires
timeout=60      # default timeout
interval=15     # default interval between checks if the process is still alive
delay=2         # default delay between posting the given signal and
                # destroying the process (kill -KILL)

while :
do
        case $1 in
        --)     shift; break ;;
        -*)     SIG=$1 ;;
        [0-9]*) timeout=$1 ;;
        :*)     EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
        +*)     EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
        *)      break ;;
        esac
        shift
done

case $# in
0)      echo "$prog: $usage" >&2 ; exit 2 ;;
esac

(
        for t in $timeout $delay
        do
                while (( $t > $interval ))
                do
                        sleep $interval
                        kill -0 $$ || exit
                        t=$(( $t - $interval ))
                done
                sleep $t
                kill $SIG $$ && kill -0 $$ || exit
                SIG=-KILL
        done
) 2> /dev/null &

exec "$@"

comments

not sure but i think one has to make the /tmp/ files manually

sandbox

iwscan perl

#!/usr/bin/perl
# Tim Osburn - tim@osburn.com
#
# install perl

$iwlist = "/sbin/iwlist eth0 scanning";

open scan, "$iwlist |";
while (<scan>) {
  if (/^\s+Cell (\S+) - Address: (\S+)/) {
    $CELL=$1;
    $ADDRESS=$2;
    # print "$1 $2";
    $INLOOP=1;
    }
  if (/^\s+ESSID:(\S+)/) {
    $ESSID=$1;
    print " $1";
    }
  if (/^\s+Frequency:\S+ \S+ \(Channel (\S+)\)/) {
    $CHAN=$1;
    print " $1";
    }
  if (/^\s+Quality=\S+\s+Signal level=(\S+).*/) {
    $SIGNAL=$1;
    print " $1";
    }
  if (/^\s+Encryption key:(\S+)/) {
    $ENCRYPTION=$1;
    print " $1\n";
    }
  }
close scan;

produces output in the form of "essid" channel signal_strength encryption_status


"ESSID_NAME1" 1 -82 on
"ESSID_NAME2" 1 -49 off

Personal tools

script to Auto connect to Open 802.11 wireless AP's

wifiweasel

wifiweasel v1.5 - once run this script takes control of the wireless interface, then detects and connects to strongest open wifi AP in the vicinity. Subsequently it checks the connectivity every X seconds. If the connection fails, the whole process repeats itself; this requires the timeout script below (needs to be tested with multiple AP's - might have an iwconfig essid glitch).

 
#!/bin/sh
   #####################
###########################
#/var/wifi/hacker/kd8ikt###
# OMG i'm programming now!#
###########################
###we.trekbeyond.org#######
  #####################
        #KISS#

# CONFIG
#######interface################
IFACE=eth0 
#Dhcp_timeout
DHCP=4
#check connectivity every X sec
X=15
#
# /CONFIG
################################
#### Scan Funk ####
Func_iwlist_open () {
echo scanning wifi ;
iwlist $IFACE scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' \
    | sort -k4 -k3nr |grep -m1 off \
|awk '{print $1}' > /tmp/weasel
cat /tmp/weasel |grep '"' -m1 > /tmp/weasel_
sed /\"/s/\"//g /tmp/weasel_ > /tmp/ESSID
}
#### iface up Funk ######## 
Func_ifconfig_up () {
echo ; bring up interface
iwconfig $IFACE essid `cat /tmp/ESSID` 
}
#### iface down Funk #####
Func_ifconfig_down () {
echo killing iface and udhcpc
killall udhcpc 
ifconfig $IFACE down	
}
#### Conn Funk #####
Func_chk_net ()	{
echo checking...   
ping -w 2 -c1  $dns 
if [ $=0 ];then ERROR=0; echo; echo Online!  go download some pr0n ; echo; sleep $X; Func_chk_net ; 
fi  #W00T  
if [ $=1 ] ; then ERROR=1 ;echo ;echo FAIL!\ 
... Searching ; Func_Main ;
fi 
 }
#### Main Funk #########
Func_MAIN () { while [ ERROR=1 ] ; do
echo prime directive
Func_ifconfig_down ;
Func_iwlist_open Func_ifconfig_up ;
timeout $DHCP udhcpc $IFACE ;
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
Func_chk_net ;
Func_MAIN ;
done; 
} 
#### MAIN #### 
echo 1st run
Func_ifconfig_down ;
Func_iwlist_open Func_ifconfig_up  ; 
udhcpc $IFACE ; 
export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'`
Func_chk_net ; 
Func_MAIN ;
echo This is the end, my only friend the end
Done; #actually i dont think it ever ends but i'm done writing it
#####################EOF#########################################

timeout

Added a timeout script to keep udhcpc from hanging up everything. As we all know, dhcp should not take 60sec; if it's not obtained a reply or a lease within a few seconds, you are likely not going to obtain an IP (perhaps due to range issues, mac filtering, etc).

INSTALL /usr/bin/timeout


#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell
#From: gwc@root.co.uk (Geoff Clare)
#Subject: Re: timeout -t <sec> <unix command> (Re: How to give rsh a shorter timeout?)
#Message-ID: <EoBxrs.223@root.co.uk>
#Date: Fri, 13 Feb 1998 18:23:52 GMT

#
# Conversion to bash v2 syntax done by Chet Ramey <chet@po.cwru.edu
# UNTESTED
#

prog=${0##*/}
usage="usage: $prog [-signal] [timeout] [:interval] [+delay] [--] <command>"

SIG=-TERM       # default signal sent to the process when the timer expires
timeout=60      # default timeout
interval=15     # default interval between checks if the process is still alive
delay=2         # default delay between posting the given signal and
                # destroying the process (kill -KILL)

while :
do
        case $1 in
        --)     shift; break ;;
        -*)     SIG=$1 ;;
        [0-9]*) timeout=$1 ;;
        :*)     EXPR='..\(.*\)' ; interval=`expr x"$1" : "$EXPR"` ;;
        +*)     EXPR='..\(.*\)' ; delay=`expr x"$1" : "$EXPR"` ;;
        *)      break ;;
        esac
        shift
done

case $# in
0)      echo "$prog: $usage" >&2 ; exit 2 ;;
esac

(
        for t in $timeout $delay
        do
                while (( $t > $interval ))
                do
                        sleep $interval
                        kill -0 $$ || exit
                        t=$(( $t - $interval ))
                done
                sleep $t
                kill $SIG $$ && kill -0 $$ || exit
                SIG=-KILL
        done
) 2> /dev/null &

exec "$@"

comments

not sure but i think one has to make the /tmp/ files manually

sandbox

iwscan perl

#!/usr/bin/perl
# Tim Osburn - tim@osburn.com
#
# install perl

$iwlist = "/sbin/iwlist eth0 scanning";

open scan, "$iwlist |";
while (<scan>) {
  if (/^\s+Cell (\S+) - Address: (\S+)/) {
    $CELL=$1;
    $ADDRESS=$2;
    # print "$1 $2";
    $INLOOP=1;
    }
  if (/^\s+ESSID:(\S+)/) {
    $ESSID=$1;
    print " $1";
    }
  if (/^\s+Frequency:\S+ \S+ \(Channel (\S+)\)/) {
    $CHAN=$1;
    print " $1";
    }
  if (/^\s+Quality=\S+\s+Signal level=(\S+).*/) {
    $SIGNAL=$1;
    print " $1";
    }
  if (/^\s+Encryption key:(\S+)/) {
    $ENCRYPTION=$1;
    print " $1\n";
    }
  }
close scan;

produces output in the form of "essid" channel signal_strength encryption_status


"ESSID_NAME1" 1 -82 on
"ESSID_NAME2" 1 -49 off