Wifiweasel
From Openmoko
(→weaselwifi) |
(-cat) |
||
(50 intermediate revisions by 8 users not shown) | |||
Line 1: | Line 1: | ||
− | script to Auto connect to Open | + | {{Todo|Dear maintainer: Please add an introduction to this article.}} |
− | ==== | + | |
− | + | [[image:Weaselwifi.jpg|right|220px]] | |
− | once run this script takes control of the wireless interface | + | |
− | detects and connects to strongest open wifi AP in the vicinity | + | see also [[Lint-wifi]] |
− | + | ||
− | whole | + | == wpa_config auto connect any == |
− | + | <PRE> | |
+ | root@om-gta02:~# cat ./wifiupp | ||
+ | killall udhcpc | ||
+ | wpa_supplicant -ieth0 -c/etc/wpa_supplicant/wpa_supplicant.conf -B | ||
+ | udhcpc | ||
+ | |||
+ | |||
+ | |||
+ | root@om-gta02:~# cat /etc/wpa_supplicant/wpa_supplicant.conf | ||
+ | |||
+ | ctrl_interface=/var/run/wpa_supplicant | ||
+ | ctrl_interface_group=0 | ||
+ | eapol_version=1 | ||
+ | ap_scan=1 | ||
+ | fast_reauth=1 | ||
+ | |||
+ | network={ | ||
+ | key_mgmt=NONE | ||
+ | priority=-9999999 | ||
+ | } | ||
+ | |||
+ | |||
+ | root@om-gta02:~# | ||
+ | |||
+ | root@om-gta02:~# cat /usr/share/applications/wifiupp.desktop | ||
+ | [Desktop Entry] | ||
+ | Name=wifiup | ||
+ | Comment=wpa_supplicant start | ||
+ | Encoding=UTF-8 | ||
+ | Version=1.0 | ||
+ | Type=Application | ||
+ | Exec=/home/root/wifiupp & | ||
+ | Icon=fixmeh | ||
+ | Terminal=true | ||
+ | SingleInstance=true | ||
+ | StartupNotify=true | ||
+ | root@om-gta02:~# | ||
+ | |||
+ | |||
+ | |||
+ | </PRE> | ||
+ | |||
+ | =FAIL= | ||
+ | |||
+ | script to Auto connect to Open 802.11 wireless AP's ***DOESNT WORK*** i think something is wrong with the driver and it doesnt reset properly with this procedure, anyone else test re-associating with separate aps manually? | ||
+ | |||
+ | ====wifiweasel==== | ||
+ | wifiweasel v2 - | ||
+ | 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 | ||
+ | |||
+ | [ [ comment: Func_MAIN calls Func_chk_net calls Func_Main(!!)... calls... (BUG?) ] ] | ||
<pre> | <pre> | ||
#!/bin/sh | #!/bin/sh | ||
− | + | ####### WifiWeasel ###### Ver. 2 | |
− | ########################### | + | ######################### 08-07-22 |
− | + | ## kd8ikt ## | |
− | + | ## we.trekbeyond.org ## | |
− | + | ######################### | |
− | + | ######################### | |
− | + | ||
− | + | ||
− | + | ||
− | # | + | |
− | ####### | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
# | # | ||
− | # / | + | #install /usr/bin/wifiweasel |
− | ######### | + | # |
− | #### Scan Funk | + | #Variables |
+ | IFACE=eth0 | ||
+ | DHCP_TIMEOUT=5 # requires /usr/bin/timeout | ||
+ | CHECK_NET_TIMER=15 #secs | ||
+ | TEMPDIR=/tmp/wifiweasel/ | ||
+ | # | ||
+ | #Functions | ||
+ | # | ||
+ | #(dont touch unless you | ||
+ | #know wtf your doing | ||
+ | # | ||
+ | #### Scan Funk | ||
Func_iwlist_open () { | Func_iwlist_open () { | ||
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 off \ | + | | sort -k4 -k3nr |grep -m1 off \ |
− | |awk '{print $1}' > /tmp/ | + | |awk '{print $1}' > /tmp/wifiweasel/scan |
− | sed /\"/s/\"//g / | + | 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 () { | Func_ifconfig_down () { | ||
killall udhcpc | killall udhcpc | ||
− | ifconfig $IFACE down | + | ifconfig $IFACE down |
} | } | ||
− | #### | + | #### iface up Funk |
+ | Func_ifconfig_up () { | ||
+ | ifconfig $IFACE up | ||
+ | iwconfig $IFACE essid `cat $TEMPDIR/ESSID` | ||
+ | } | ||
+ | #### Connection check Funk | ||
Func_chk_net () { | Func_chk_net () { | ||
− | export | + | echo checking... |
− | + | export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'` | |
− | if [ $=0 ];then ERROR=0; echo Online! ;sleep $ | + | timeout 1 nslookup $dns |
− | fi #W00T | + | if [ $? = 0 ];then ERROR=0; echo; echo Online! W00T ; echo ;echo using essid ; cat $TEMPDIR/ESSID ; sleep $CHECK_NET_TIMER ; Func_chk_net ; |
− | if [ $ | + | fi #W00T |
+ | if [ $? -ge 1] ; then ERROR=1 ;echo ;echo FAIL!\ | ||
+ | ... Searching ; echo ; Func_Main ; | ||
fi | fi | ||
− | + | } | |
− | #### Main Funk | + | #### Main loop Funk |
Func_MAIN () { while [ ERROR=1 ] ; do | Func_MAIN () { while [ ERROR=1 ] ; do | ||
Func_ifconfig_down ; | Func_ifconfig_down ; | ||
− | + | Func_iwlist_open Func_ifconfig_up ; | |
− | timeout $ | + | timeout $DHCP_TIMEOUT udhcpc $IFACE ; |
− | Func_chk_net | + | Func_chk_net ; |
+ | Func_MAIN ; | ||
done; | done; | ||
− | } | + | } |
− | ## | + | #Prep |
− | Func_ifconfig_down | + | mkdir $TEMPDIR |
− | Func_iwlist_open Func_ifconfig_up | + | touch $TEMPDIR/scan |
− | udhcpc $IFACE | + | touch $TEMPDIR/scan1 |
− | Func_chk_net | + | touch $TEMPDIR/ESSID |
− | echo This is the end my only friend the end | + | # RunProgram |
− | Done; | + | Func_ifconfig_down |
− | #####################EOF############### | + | Func_iwlist_open Func_ifconfig_up |
+ | timeout $DHCP_TIMEOUT udhcpc $IFACE | ||
+ | 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######### | ||
</pre> | </pre> | ||
====timeout==== | ====timeout==== | ||
− | + | Added a timeout script to keep udhcpc from hanging everything. | |
− | + | As we all know, dhcp should not take 60sec <br> | |
− | + | ||
− | + | ||
INSTALL /usr/bin/timeout | INSTALL /usr/bin/timeout | ||
− | |||
<pre> | <pre> | ||
− | |||
#Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell | #Newsgroups: comp.unix.admin,comp.unix.solaris,comp.unix.shell | ||
#From: gwc@root.co.uk (Geoff Clare) | #From: gwc@root.co.uk (Geoff Clare) | ||
Line 133: | Line 198: | ||
exec "$@" | exec "$@" | ||
+ | |||
+ | </pre> | ||
+ | |||
+ | ====comments==== | ||
+ | not sure but i think one has to make the /tmp/ files manually | ||
+ | *fixed as of v2 | ||
+ | |||
+ | = Am Improved Version Of wifiweasel = | ||
+ | |||
+ | Looking carefully through the code there seemed to be logical errors in the control structure of the program. This version is supposed to be logically similar without the recursive function calls. I have made very limited use of it but am submitting it here in case its useful for others. I call it wifiweasel3.sh It also uses environment variables instead of temporary files, which may or may not be a good thing. | ||
+ | |||
+ | 1) Copy it to your phone, | ||
+ | 2) chmod +x wifiweasel3.sh | ||
+ | 3) ensure that you have the timeout program above copied and executable. | ||
+ | 4) Run it - At first you can try | ||
+ | wifiweaselv3.sh -v | ||
+ | which gives you a verbose account of what is happening. | ||
+ | you can also do | ||
+ | wifiweasel start | ||
+ | and wifiweasel stop | ||
+ | |||
+ | which keeps it silently backgrounded. | ||
+ | |||
+ | Here is the code: | ||
+ | |||
+ | <pre>#!/bin/sh | ||
+ | ####### WifiWeasel ###### | ||
+ | # Ver. 2 ## | ||
+ | ######################### | ||
+ | # 08-07-22 ## | ||
+ | ## kd8ikt ## | ||
+ | ## we.trekbeyond.org ## | ||
+ | ######################### | ||
+ | # Ver 3 Mark Winder 27/7/8 | ||
+ | ######################### | ||
+ | # | ||
+ | #install /usr/bin/wifiweasel | ||
+ | # | ||
+ | #Variables | ||
+ | IFACE=eth0 | ||
+ | DHCP_TIMEOUT=5 # requires /usr/bin/timeout | ||
+ | CHECK_NET_TIMER=15 #secs | ||
+ | # call -v for debug to std out | ||
+ | PIDFILE=/tmp/wifiweasel.pid | ||
+ | [[ "$1" == '-v' ]] && debug=1 && shift | ||
+ | if [[ "$1" == 'start' ]] | ||
+ | then | ||
+ | shift | ||
+ | wifiweasel3.sh "$@" & | ||
+ | echo $! > $PIDFILE | ||
+ | exit | ||
+ | fi | ||
+ | if [[ "$1" == 'stop' ]] | ||
+ | then | ||
+ | if [[ -e $PIDFILE ]] | ||
+ | then | ||
+ | kill $(cat $PIDFILE) | ||
+ | rm $PIDFILE | ||
+ | echo "WiFiWeasel stopped"; | ||
+ | fi | ||
+ | exit | ||
+ | fi | ||
+ | |||
+ | # | ||
+ | #Functions | ||
+ | # | ||
+ | #### Scan Funk | ||
+ | Func_iwlist_open () | ||
+ | { | ||
+ | ESSID=$(iwlist $IFACE scanning | | ||
+ | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | | ||
+ | sort -k4 -k3nr |grep off | | ||
+ | head -n1| sed 's/^"//;s/".*$//' | ||
+ | ) | ||
+ | [[ $debug ]] && echo Using ESSID $ESSID | ||
+ | } | ||
+ | #### interface down Funk | ||
+ | Func_ifconfig_down () | ||
+ | { | ||
+ | killall udhcpc | ||
+ | ifconfig $IFACE down | ||
+ | } | ||
+ | #### iface up Funk | ||
+ | Func_ifconfig_up () | ||
+ | { | ||
+ | ifconfig $IFACE up | ||
+ | iwconfig $IFACE essid $ESSID | ||
+ | } | ||
+ | #### Connection check Funk | ||
+ | # | ||
+ | Func_net_ok () | ||
+ | { | ||
+ | [[ $debug ]] && echo checking... | ||
+ | export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'` | ||
+ | if timeout 1 nslookup $dns > /dev/null 2>&1 | ||
+ | then | ||
+ | [[ $debug ]] && echo Online! $ESSID | ||
+ | return 0 # net is ok | ||
+ | else | ||
+ | [[ $debug ]] && echo "FAIL! $? ... Searching " | ||
+ | return 1 # net not ok | ||
+ | fi | ||
+ | } | ||
+ | # RunProgram | ||
+ | while [[ 1 ]] | ||
+ | do | ||
+ | if [[ -z $ESSID ]] # no essid ? | ||
+ | then | ||
+ | Func_ifconfig_down | ||
+ | Func_iwlist_open | ||
+ | Func_ifconfig_up | ||
+ | timeout $DHCP_TIMEOUT udhcpc $IFACE > /dev/null 2>&1 | ||
+ | fi | ||
+ | sleep $CHECK_NET_TIMER | ||
+ | Func_net_ok || ESSID="" | ||
+ | done | ||
+ | #####################EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF######### | ||
+ | |||
</pre> | </pre> | ||
Line 179: | Line 362: | ||
<br> "ESSID_NAME2" 1 -49 off | <br> "ESSID_NAME2" 1 -49 off | ||
− | [[ | + | [[Category:Wlan software]] |
− | [[ | + | [[Category:Neo FreeRunner Hardware]] |
Latest revision as of 16:36, 10 July 2009
TODO: Dear maintainer: Please add an introduction to this article. (See: To-Do List) |
see also Lint-wifi
Contents |
[edit] wpa_config auto connect any
root@om-gta02:~# cat ./wifiupp killall udhcpc wpa_supplicant -ieth0 -c/etc/wpa_supplicant/wpa_supplicant.conf -B udhcpc root@om-gta02:~# cat /etc/wpa_supplicant/wpa_supplicant.conf ctrl_interface=/var/run/wpa_supplicant ctrl_interface_group=0 eapol_version=1 ap_scan=1 fast_reauth=1 network={ key_mgmt=NONE priority=-9999999 } root@om-gta02:~# root@om-gta02:~# cat /usr/share/applications/wifiupp.desktop [Desktop Entry] Name=wifiup Comment=wpa_supplicant start Encoding=UTF-8 Version=1.0 Type=Application Exec=/home/root/wifiupp & Icon=fixmeh Terminal=true SingleInstance=true StartupNotify=true root@om-gta02:~#
[edit] FAIL
script to Auto connect to Open 802.11 wireless AP's ***DOESNT WORK*** i think something is wrong with the driver and it doesnt reset properly with this procedure, anyone else test re-associating with separate aps manually?
[edit] wifiweasel
wifiweasel v2 - 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
[ [ comment: Func_MAIN calls Func_chk_net calls Func_Main(!!)... calls... (BUG?) ] ]
#!/bin/sh ####### WifiWeasel ###### Ver. 2 ######################### 08-07-22 ## kd8ikt ## ## we.trekbeyond.org ## ######################### ######################### # #install /usr/bin/wifiweasel # #Variables IFACE=eth0 DHCP_TIMEOUT=5 # requires /usr/bin/timeout CHECK_NET_TIMER=15 #secs TEMPDIR=/tmp/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 () { ifconfig $IFACE up iwconfig $IFACE essid `cat $TEMPDIR/ESSID` } #### Connection check Funk Func_chk_net () { echo checking... export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'` timeout 1 nslookup $dns if [ $? = 0 ];then ERROR=0; echo; echo Online! W00T ; echo ;echo using essid ; cat $TEMPDIR/ESSID ; sleep $CHECK_NET_TIMER ; Func_chk_net ; fi #W00T if [ $? -ge 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_TIMEOUT udhcpc $IFACE ; 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_TIMEOUT udhcpc $IFACE 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#########
[edit] timeout
Added a timeout script to keep udhcpc from hanging everything.
As we all know, dhcp should not take 60sec
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 "$@"
[edit] comments
not sure but i think one has to make the /tmp/ files manually
- fixed as of v2
[edit] Am Improved Version Of wifiweasel
Looking carefully through the code there seemed to be logical errors in the control structure of the program. This version is supposed to be logically similar without the recursive function calls. I have made very limited use of it but am submitting it here in case its useful for others. I call it wifiweasel3.sh It also uses environment variables instead of temporary files, which may or may not be a good thing.
1) Copy it to your phone, 2) chmod +x wifiweasel3.sh 3) ensure that you have the timeout program above copied and executable. 4) Run it - At first you can try wifiweaselv3.sh -v which gives you a verbose account of what is happening. you can also do wifiweasel start and wifiweasel stop which keeps it silently backgrounded.
Here is the code:
#!/bin/sh ####### WifiWeasel ###### # Ver. 2 ## ######################### # 08-07-22 ## ## kd8ikt ## ## we.trekbeyond.org ## ######################### # Ver 3 Mark Winder 27/7/8 ######################### # #install /usr/bin/wifiweasel # #Variables IFACE=eth0 DHCP_TIMEOUT=5 # requires /usr/bin/timeout CHECK_NET_TIMER=15 #secs # call -v for debug to std out PIDFILE=/tmp/wifiweasel.pid [[ "$1" == '-v' ]] && debug=1 && shift if [[ "$1" == 'start' ]] then shift wifiweasel3.sh "$@" & echo $! > $PIDFILE exit fi if [[ "$1" == 'stop' ]] then if [[ -e $PIDFILE ]] then kill $(cat $PIDFILE) rm $PIDFILE echo "WiFiWeasel stopped"; fi exit fi # #Functions # #### Scan Funk Func_iwlist_open () { ESSID=$(iwlist $IFACE scanning | awk -F '[ :=]+' '/(ESS|Freq|Qual)/{ printf $3" " } /Encr/{ print $4 }' | sort -k4 -k3nr |grep off | head -n1| sed 's/^"//;s/".*$//' ) [[ $debug ]] && echo Using ESSID $ESSID } #### interface down Funk Func_ifconfig_down () { killall udhcpc ifconfig $IFACE down } #### iface up Funk Func_ifconfig_up () { ifconfig $IFACE up iwconfig $IFACE essid $ESSID } #### Connection check Funk # Func_net_ok () { [[ $debug ]] && echo checking... export dns=`cat /etc/resolv.conf |grep name |awk '{print $2}'` if timeout 1 nslookup $dns > /dev/null 2>&1 then [[ $debug ]] && echo Online! $ESSID return 0 # net is ok else [[ $debug ]] && echo "FAIL! $? ... Searching " return 1 # net not ok fi } # RunProgram while [[ 1 ]] do if [[ -z $ESSID ]] # no essid ? then Func_ifconfig_down Func_iwlist_open Func_ifconfig_up timeout $DHCP_TIMEOUT udhcpc $IFACE > /dev/null 2>&1 fi sleep $CHECK_NET_TIMER Func_net_ok || ESSID="" done #####################EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#EOF#########
[edit] sandbox
[edit] 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