OpenWrt Forum Archive

Topic: Announcing AAP (aka AutoAP) for OpenWrt Kamikaze 8.09+

The content of this topic has been archived on 3 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

UPDATE 100814 - For most recent Piranha + AAP development, please follow https://forum.openwrt.org/viewtopic.php?id=26116

Community,

I'm glad to announce the immediate availability of AAP for OpenWrt Kamikaze 8.09+. AAP is ready-to-run on devices with Atheros wireless chipsets (while easily adoptable to other chipsets) and has been written for the Fonera 2100/2200. Instead of explaining AAP in detail again here, I paste the rather comprehensive README that ships with the initial release of AAP. AAP is available as of today at http://piranha.klashed.net/pub/aap

#
# Copyright (C) 2009 orange <or4n9e@gmx.de>
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

### Definition ###
AAP is a script that enables users of OpenWrt Kamikaze 8.09+ to automatically log into different 
wireless accesspoints that provide internet access. The accesspoints can be automatically
searched and/or set in a whitelist (aap_wlst). Beyond that AAP is capable in defining a blacklist
of undesired accesspoints while connecting (aap_blst). AAP supports the client connection of
your OpenWrt Kamikaze 8.09+ device to open, wep, wpa-psk and wpa2-psk encrypted accesspoints. 
AAP connects to surrounding accesspoints utilizing their MAC addresses. This ensures a high level of
differentiation between accesspoints in contrast to SSID-based connecting.

### Requirements ###
AAP requires an OpenWrt Kamikaze 8.09+ device properly configured in Client Mode (sta). AAP supports
devices with an Atheros wireless chipset (while easily adoptable to other chipsets) and has been written
for the Fonera 2100/2200. It assumes a single madwifi-VAP ath0 in sta mode.

### Whitelist/Blacklist ###
aap_wlst is a newline-separated whitelist of preferred accesspoints:
00:11:22:33:44:55* for accesspoints with no encryption
00:11:22:33:44:55*key for accesspoints with wep, wpa-psk or wpa2-psk encryption

aap_blst is a newline-separated blacklist of undesired accesspoints:
00:11:22:33:44:55 for open, wep, wpa-psk and wpa2-psk encrypted accesspoints

### Configuration Variables ###
aap.sh provides configuration variables (with default values) at the top of the script:
# Internet connection check frequency in seconds
aap_checkfreq=60

# Length of time in seconds to wait for a DHCP request to succeed
aap_dhcptimeout=30

# Internet URL or IP address used to ping to ensure internet access is working
aap_ineturl=www.google.com

# Defines the search mode. Try to find all accesspoints that provide internet access. "0"=NO, "1"=YES
aap_prefonly=0

# Defines the accesspoint refresh scan delay in seconds (divisible by 5)
aap_rescandelay=43200

### Installation ###
1. Log into your device via SSH
2. # mkdir /aap
3. scp aap.sh, aap_blst, aap_wlst and scan.awk to /aap
4. scp aap to /etc/init.d
5. # chmod 755 /aap/aap.sh, /aap/scan.awk and /etc/init.d/aap

### Usage ###
To enable autostart of AAP
# /etc/init.d/aap enable
To disable autostart of AAP
# /etc/init.d/aap disable
To start AAP manually
# /etc/init.d/aap start
To stop AAP manually
# /etc/init.d/aap stop

### Known Issues ###
Both aap_wlst and aap_blst need to be non-empty files due to the behaviour of the busybox builtin grep. 
Thus, please leave aap_wlst and aap_blst as provided and just append your custom newline-separated entries 
if actually in use.

### Warning ###
Both aap.sh and scan.awk make use of "*" as a field separator. Thus, both SSIDs and encryption keys
that includes "*" aren't supported by AAP. Unfortunately this constraint isn't solvable currently
as the busybox ash shell doesn't support arrays and the SSID specification allows the complete ASCII
character set. Be sure to utilize aap_blst to exclude those particular accesspoints. AAP may harm your 
OpenWrt Kamikaze 8.09+ /etc/config/wireless configuration permanently if you neglect this warning. Use
AAP at your own risk!

### Changelog ###
Initial release aap_090903

Testing, reporting bugs, voicing comments/suggestions and asking questions about AAP is certainly much appreciated. I hope you feel this initial release of AAP as a valuable share to the OpenWrt community.

best,
orange

(Last edited by orange on 14 Aug 2010, 18:09)

aap_090909 has been released. It fixes a bug with missing variable quoting. With the new release, SSIDs that contain spaces are now properly processed by UCI. I plan to do some more code clean-up in the future, but this is a quick fix for your convenience. aap_090909 is available at http://piranha.klashed.net/pub/aap

thanks,
orange

I'm trying your script.  scan.awk fails to parse the channel properly, resulting in:

(I stuck in a few echo statements)

uci set wireless.wifi0.channel=0
uci: Invalid argument
uci set wireless.@wifi-iface[0].ssid=4jwireless
uci set wireless.@wifi-iface[0].bssid=00:1A:1E:17:E3:20
uci set wireless.@wifi-iface[0].hidden=0

This is with Broadcom 2.4 kernel; sample iwlist scan output:

          Cell 05 - Address: 00:1A:1E:18:01:A0
                    ESSID:"4jwireless"
                    Mode:Managed
                    Channel:3
                    Quality:2/5  Signal level:-76 dBm  Noise level:-87 dBm
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s

root@OpenWrt:/usr/sbin# cat /tmp/aap_slst
1*4jwireless*00:1A:1E:17:E3:20*0*none
1*4jauth*00:1A:1E:17:E3:21*0*psk2
3*linksys*00:13:10:0E:E4:ED*0*wep
2*SeinerHome*00:90:4B:C9:AC:F6*0*wep
1*4jwireless*00:1A:1E:19:DD:A0*0*none
2*4jauth*00:1A:1E:19:DD:A1*0*psk2

All of the channels are '0'.

@cptdondo
AAP has been written for Atheros wireless chipsets as mentioned above. Thus, you'd need to port it by yourself to be able to use it on your Broadcom 2.4 platform. That said, AAP is designed to be rather easy portable though. I just had a quick look on the iwlist scan output you provided above and the reason it doesn't work out-of-the-box is pretty obvious. Just have a look on sample Atheros iwlist scan output and you'll see:

                    Cell 04 - Address: 00:1A:1E:18:01:A0
                    ESSID:"4jwireless"
                    Mode:Master
                    Frequency:2.462 GHz (Channel 11)
                    Quality=54/70  Signal level=-41 dBm  Noise level=-95 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 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
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : CCMP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : PSK

scan.awk parses field $4 to get the appropriate channel and it's just not the same output. If you'd decide to port it by yourself, have a precise look at line 9 of aap.sh, its UCI section (function aap_connect() {}) and proper adjusting of scan.awk to your iwlist scan output.

I may help with the porting if you provide me the following:
1. iwlist <interface> scan sample with OPN, WEP, WPA1, WPA1/2 mixed and WPA2 encryptions
2. uci show wireless sample
3. interface (ath0 equivalent)

Other than that I'd need appropriate information about possible operating modes of the Broadcom 2.4 platform as I never dealed with it before. I hope it's able to operate in station mode at least?

(Last edited by orange on 10 Sep 2009, 05:43)

I had a look through scan.awk, which is where the problem lies.  Unfortunately I've always found awk scripts very difficult to understand; I'm more of a sed/grep kind of person.

Here's a list of APs that I can see from my house - not quite the mix you asked for, but it has all of the common modes:

wl0       Scan completed :
          Cell 01 - Address: 00:1A:1E:17:E3:20
                    ESSID:"4jwireless"
                    Mode:Managed
                    Channel:3
                    Quality:2/5  Signal level:-77 dBm  Noise level:-90 dBm
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
          Cell 02 - Address: 00:1A:1E:17:E3:21
                    ESSID:"4jauth"
                    Mode:Managed
                    Channel:3
                    Quality:2/5  Signal level:-77 dBm  Noise level:-90 dBm
                    IE: IEEE 802.11i/WPA2 Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : CCMP
                        Authentication Suites (1) : 802.1x
                    IE: WPA Version 1
                        Group Cipher : TKIP
                        Pairwise Ciphers (1) : TKIP
                        Authentication Suites (1) : 802.1x
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s
          Cell 03 - Address: 00:13:10:0E:E4:ED
                    ESSID:"linksys"
                    Mode:Managed
                    Channel:11
                    Quality:3/5  Signal level:-69 dBm  Noise level:-87 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s; 18 Mb/s
                              24 Mb/s; 36 Mb/s; 54 Mb/s; 6 Mb/s; 9 Mb/s
                              12 Mb/s; 48 Mb/s
          Cell 04 - Address: 00:90:xx:xx:xx:xx
                    ESSID:"XXXXXX"
                    Mode:Managed
                    Channel:13
                    Quality:1/5  Signal level:-82 dBm  Noise level:-90 dBm
                    Encryption key:on
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 11 Mb/s
          Cell 05 - Address: 00:1A:1E:18:01:A0
                    ESSID:"4jwireless"
                    Mode:Managed
                    Channel:3
                    Quality:2/5  Signal level:-76 dBm  Noise level:-87 dBm
                    Encryption key:off
                    Bit Rates:1 Mb/s; 2 Mb/s; 5.5 Mb/s; 6 Mb/s; 9 Mb/s
                              11 Mb/s; 12 Mb/s; 18 Mb/s; 24 Mb/s; 36 Mb/s
                              48 Mb/s; 54 Mb/s

root@OpenWrt:/usr/sbin# uci show | grep wireless
wireless.wl0=wifi-device
wireless.wl0.type=broadcom
wireless.wl0.channel=5
wireless.@wifi-iface[0]=wifi-iface
wireless.@wifi-iface[0].device=wl0
wireless.@wifi-iface[0].network=lan
wireless.@wifi-iface[0].mode=sta
wireless.@wifi-iface[0].encryption=none
wireless.@wifi-iface[0].ssid=4jwireless
wireless.@wifi-iface[0].hidden=0
wireless.@wifi-iface[0].bssid=00:1A:1E:19:DD:A0

As I'm not really sure what scan.awk does (too early, not enough coffee) I can't quite rewrite in with sed but here's a line that should reliably parse the channel number:

iwlist scan | awk /hannel/ | sed 's/^.*hannel[^0-9]*//;s/[^0-9]*$//'

I'll see later on if I can get it to mind-meld with the rest of your script.  awk doesn't support backreferences, so I can't really do this within awk.

--Yan

(Last edited by cptdondo on 10 Sep 2009, 13:30)

OK, I did some minor adjustments to both scan.awk and aap.sh and uploaded a tarball available at http://piranha.klashed.net/pub/testing/ … ing.tar.gz

Please let me know if it works. I won't guarantee for proper functionality though as I have no hardware to actually test it. Thus use it at your own risk!

best,
orange

One observation:

If I understand this correctly, the script writes to flash every time it tries a new AP.  I don't know how many write cycles the flash is good for, but there really is no real reason to write to flash at all.  Everything could be done with iwconfig.

This particular box will sit in a van and be on all the time, 24/7, for the forseeable future - several years at least.  I don't want it writing to flash every few seconds.

Am I wrong on this?

cptdondo wrote:

One observation:

If I understand this correctly, the script writes to flash every time it tries a new AP.  I don't know how many write cycles the flash is good for, but there really is no real reason to write to flash at all.  Everything could be done with iwconfig.

This particular box will sit in a van and be on all the time, 24/7, for the forseeable future - several years at least.  I don't want it writing to flash every few seconds.

Am I wrong on this?

Actually you're right on this. AAP utilizes Kamikaze UCI configuration and thus it writes to flash when attempting to connect to a new AP (i.e. it writes to /etc/config/wireless). All temporary output of AAP ends up in /tmp though. That said, your assumption is a bit too pessimistic from my perspective (every few seconds): I mean, as long as AAP stays connected to a particular AP it won't access the flash, just upon connecting to a new AP (when aap_check() {} fails OR aap_rescandelay is reached). AutoAP for dd-wrt does, btw, the very same thing with NVRAM configuration. That said, feel free to re-write aap_connect () {} to utilize iwconfig commands if you feel better then - I haven't looked into that as I try to support the standard Kamikaze-style approach to configure things (=UCI)

From my very personal experience, I can tell you that neither AutoAP for dd-wrt nor AAP harmed my flash so far although I have my device (Fonera 2100) running 24/7 on the rooftop for almost a year now.

What you haven't mentioned and what I'm certainly most interested in: Does the brcm-2.4 port I've written for you actually works on your device?

You can just leave out the "uci commit" iirc. As long as you just do "uci set" the changes are staged in /tmp and only written to flash if you run "uci commit". However, the "wifi" command will pick up the changed values even if the uci was not committed before. As a bonus you can run "uci revert wireless" to get back to the original state. So there's no need to actually write anything to flash.

~ JoW

jow wrote:

You can just leave out the "uci commit" iirc. As long as you just do "uci set" the changes are staged in /tmp and only written to flash if you run "uci commit". However, the "wifi" command will pick up the changed values even if the uci was not committed before. As a bonus you can run "uci revert wireless" to get back to the original state. So there's no need to actually write anything to flash.

~ JoW

That's indeed pretty interesting! Thanks a lot for that valuable insight into the "inner workings" of UCI. I myself assumed that "wifi" would read /etc/config/wireless instead of the staged UCI values in /tmp so far. Knowing about it, I'll definitely consider this for the next release of AAP once I've done appropriate testing on this.

@jow
One related question that came to my mind when thinking about your proposed change to aap_connect () {}: What's the actual difference between "wifi" and "/etc/init.d/network restart"? Would "wifi" be sufficient (or maybe even better) for aap_connect () {} from your perspective and is there any difference in regard of using the staged UCI values in /tmp VS the config information from /etc/config/wireless as the used input?

(Last edited by orange on 10 Sep 2009, 17:13)

orange wrote:

What you haven't mentioned and what I'm certainly most interested in: Does the brcm-2.4 port I've written for you actually works on your device?

Yup it works, but I see maintenance headaches if you need to have a custom version for each driver.

I think I have some time in the next couple of days; I may take a shot at streamlining your script.  At least it will give me an opportunity to overcome my obtuseness when it comes to awk.

--Yan

Yup it works, but I see maintenance headaches if you need to have a custom version for each driver.

Cool! I see where you're coming from, but the maintenance overhead is worth it from my perspective. I even think about posting another "AAP porting" thread and request the needed input from users for all OpenWrt supported platforms. I mean, I've written AAP with portability in mind anyway - I just lack the hardware to test every single supported platform, but the effort for brcm-2.4 was actually changing 4 lines of code and that's not that much to do, even from a maintenance perspective.

I'm now on my way to work on jow's proposed change to completely get rid of the write access to flash and will release the next AAP very soon. The remaining question is the "/sbin/wifi" VS "/etc/init.d/network restart" thing - I'd like to address this as well with the new release.

Stay tuned and have fun with AAP! I hope you'll like it!

Proof of concept for a more generic script:

First we split the output of iwlist scan into separate files.  This gets rid of the need for arrays.

iwlist wl0 scan > /tmp/test
awk 'BEGIN{IGNORECASE=1}/^ *cell [0-9]/{close("/tmp/cell/0"f);f++}{print $0 > "/tmp/cell/0"f}' /tmp/test

Now we can loop through each of the files in /tmp/cell/NN at our leisure.  Instead of looking for hard position parameters, use pattern matching to find our arguments.  I'm not clear on what you're doing with all of the encryption stuff, so I haven't taken it further.  Maybe later.

for cell in /tmp/cell/0[0-9]* ; do
....

scan.awk:

#!/usr/bin/awk -f

BEGIN{IGNORECASE=1;b="[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]"}

/b/ {m=match($0,b);if (RSTART > 0) print substr($0,m,RLENGTH)}
/^ *ESSID/ {m=match($0,"\".*\"");if (RSTART > 0) print substr($0,m,RLENGTH)}
/Channel.+[0-9]/ {m=match($0,"Channel");if (RSTART > 0) {s=substr($0,m);n=match(s,/[0-9]+/);print substr(s,n,RLENGTH)}}
/^ *Encryption Key/ {m=match($0, /on$/); if (RSTART > 0) print substr($0,m,RLENGTH)}

That's it for now.  It should drop a lot of the complexity from your script.

If you can comment on what you're doing in the script, I can do more.

(Last edited by cptdondo on 10 Sep 2009, 20:20)

OK, here's a shiny new scan.awk that should work with any reasonable driver.  I called it scan2.awk.

#!/usr/bin/awk -f

BEGIN{
        IGNORECASE=1
        }

/[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]/ {
        m=match($0,/[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]/);
        if (RSTART > 0) {
                bssid=substr($0,m,RLENGTH);
                next;
                }
        }

/^ *ESSID/ {
        m=match($0,"\".*\"");
        if (RSTART > 0) essid=substr($0,m+1,RLENGTH-2);
        next;
        }

/Channel.+[0-9]/ {
        m=match($0,"Channel");
        if (RSTART > 0) {
                s=substr($0,m);
                n=match(s,/[0-9]+/);
                channel=substr(s,n,RLENGTH)}
        }

/^ *Quality/ {
        m=match($0,/[0-9]+\/[0-9]+/);
        if (RSTART > 0) {
                s=substr($0,m);
                split(s,t,"/");
                if (t[2] > 0) {quality=t[1]*100/t[2];}
                else quality = 50;
                next;
                }
        }

/^ *Encryption Key/ {
        m=match($0, /on$/);
        if (RSTART > 0) encr=1;
        next;
        }

/^ *IE. *IEEE/ {
        m=match($0, /wpa/);
        if (RSTART > 0) psk2=1;
        next;
        }

/^ *IE. *WPA/ {
        m=match($0, /wpa/);
        if (RSTART > 0) psk=1;
        next;
        }

END{
        if(psk2 == 1) crypt = "psk2";
        else if (psk == 1) crypt ="psk";
        else if (encr == 1) crypt = "wep";
        else crypt = "none";

        print quality"*"bssid"*"essid"*"channel"*"crypt;
        }

It needs a minor modification to aap.sh; modify the first few lines of aap_scan to read

aap_scan() {
mkdir -p /tmp/cell
rm -f /tmp/aap_rlst2 /tmp/cell/*
iwlist wl0 scan > /tmp/scan.out
awk 'BEGIN{IGNORECASE=1}/^ *cell [0-9]/{close("/tmp/cell/0"f);f++}{print $0 > "/tmp/cell/0"f}' /tmp/scan.out
for f in /tmp/cell/0[0-9]*; do cat $f | /usr/sbin/scan2.awk ; done > /tmp/aap_slst

@all
AAP 090911 has been released. Thanks to a suggestion by jow, the write-access of AAP to flash memory got completely eliminated. An “uci commit wireless” upon connection attempt to a new AP is actually not needed as “wifi” reads the staged UCI values in /tmp anyway. Thanks jow, I certainly gave you full credits for this within the README that ships with the tarball. AAP 090911 is available at http://piranha.klashed.net/pub/aap

@cptdondo
Thanks for your efforts. I'll come back to your suggestions later on as I'm under time pressure currently - I'm sorry!

orange wrote:

@jow
One related question that came to my mind when thinking about your proposed change to aap_connect () {}: What's the actual difference between "wifi" and "/etc/init.d/network restart"? Would "wifi" be sufficient (or maybe even better) for aap_connect () {} from your perspective and is there any difference in regard of using the staged UCI values in /tmp VS the config information from /etc/config/wireless as the used input?

The init script restarts the whole networking, which will bring down every interface and reconfigure it again. This can be "harmfull" for users with ppp(oe), pptp or 3g connections since the reinitializing could take quite some time. Just running "wifi" should be enough in almost every case. It will take care of the required ifdown/ifup stuff and interface reconfiguration, also it's less intrusive then a full network restart and way faster in most cases.

Regards,
JoW

It will take care of the required ifdown/ifup stuff and interface reconfiguration

I tried using a plain "wifi" instead of "/etc/init.d/network restart" but this wouldn't work out because of the ifdown/ifup stuff. Although you mentioned that "wifi" would take care of this as well I cannot confirm this. If I join a new network (uci set blabla) and run "wifi" afterwards "iwconfig" will reflect the changed wireless configuration but I won't get an IP via DHCP (and this is to my best knowlegde ifup-related, isn't it?)

So, here is my proposal and I'd like to ask you what's right/wrong from your perspective, what's redundant and so on and so forth.

ifdown wan (OR redundant?)
uci set blabla
...
/sbin/wifi (OR /sbin/wifi up?)
ifup wan

In the wiki for WhiteRussian I found "ifup wan" and "wifi" switched around but this isn't understandable to me as "ifup wan", i.e. the dhcp client would request the IP from the former network prior re-configuration with "wifi". Also, as a last question... Is it needed to kill the dhcp client prior joining a new network with "/sbin/wifi" followed by "ifup wan"? I.e. would I have a double instance if I miss to kill the client previously or will it just re-request an IP? I'm sorry for the rather long response but the documentation here is really misleading so far and due to your previous explanation I'd really like to get rid of the "/etc/init.d/network restart". I hope this all is understandable.

Thanks a lot in advance for your support!

Here's my code which handles the wifi up/down scenario.  I bring wifi down and then up.  wifi by itself apparently toggles the state of wifi, so the only way to be sure is to force it off before bringing up the new config.

aap2.sh:

#!/bin/ash

aap_connect () {
        mkdir -p /tmp/cell
        rm -f /tmp/cell/*

        iwlist wl0 scan > /tmp/scan.out

        awk 'BEGIN{IGNORECASE=1}/^ *cell[0-9]/{close("/tmp/cell/0"f);f++}{print $0 > "/tmp/cell/0"f}' /tmp/scan.out

#        ls /tmp/cell/

        for ap in /tmp/cell/0[0-9]* ; do

#                echo $ap

                uci set wireless.wl0.disabled=1
                wifi
                if [ -e /proc/`cat /var/run/wl0.pid` ] ; then kill -9 `cat /var/run/wl0.pid` ; fi
                echo 0 > /proc/diag/led/power
                if [ -f /tmp/ap ] ; then rm /tmp/ap ; fi
                if [ -f /tmp/script ] ; then rm /tmp/script ; fi
                sleep 3

                cat $ap | /usr/sbin/scan2.awk
                if [ -e /tmp/ap ] ; then cat /tmp/ap ; fi

                if [ -f /tmp/script ] ; then
                        sh /tmp/script
                        uci set wireless.wl0.disabled=0
                        wifi
                        sleep 5
##############################
#### change the URL to something that works
##############################
                wget -O /dev/null http://www.known.good.website/real.file
                        ret=$?
                        if [ $ret -eq 0 ] ; then
                                echo 1 > /proc/diag/led/power
                                conn=1
                                return
                        else
                                # try to deliver our payload via ping
                                aap_ping
                        fi
                fi
                done
        conn=0
        }

aap_maintain() {
        ret=0
        while [ $ret -eq 0 ] ; do
                sleep 15
                echo checking connection
##############################
#### change the URL to something that works
##############################
                wget -O /dev/null http://www.known.good.website/real.file
                ret=$?
                done
        }

aap_ping() {
        # placeholder for the ping payload routine
        # eventually craft a ping that can carry a small payload, like GPS coordinates
        # a bit complicated by the fact that busybox ping doesn't have the -p option
        ping -c 3 -q www.google.com
        }

aap_payload() {
        echo payload goes here
        # placeholder for whatever we want to do on connection
        # like deliver email, send GPS location coordinates, etc
        }

while [ 1 ] ; do
        aap_connect
        if [ $conn -eq 1 ] ; then
                aap_payload
                aap_maintain
                fi
        done

scan2.awk:

#!/usr/bin/awk -f

BEGIN{
        IGNORECASE=1
        }

/[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]/ {
        m=match($0,/[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]:[0-9,a-f][0-9,a-f]/);
        if (RSTART > 0) {
                bssid=substr($0,m,RLENGTH);
                next;
                }
        }

/^ *ESSID/ {
        m=match($0,"\".*\"");
        if (RSTART > 0) essid=substr($0,m+1,RLENGTH-2);
        next;
        }

/Channel.+[0-9]/ {
        m=match($0,"Channel");
        if (RSTART > 0) {
                s=substr($0,m);
                n=match(s,/[0-9]+/);
                channel=substr(s,n,RLENGTH)}
        }

/^ *Quality/ {
        m=match($0,/[0-9]+\/[0-9]+/);
        if (RSTART > 0) {
                s=substr($0,m);
                split(s,t,"/");
                if (t[2] > 0) {quality=t[1]*100/t[2];}
                else quality = 50;
                next;
                }
        }

/^ *Encryption Key/ {
        m=match($0, /on$/);
        if (RSTART > 0) encr=1;
        next;
        }

/^ *IE. *IEEE/ {
        m=match($0, /wpa/);
        if (RSTART > 0) psk2=1;
        next;
        }

/^ *IE. *WPA/ {
        m=match($0, /wpa/);
        if (RSTART > 0) psk=1;
        next;
        }

END {
        if(psk2 == 1) crypt = "psk2";
        else if (psk == 1) crypt ="psk";
        else if (encr == 1) crypt = "wep";
        else crypt = "none";

        # first we see if we're in blacklist
        while ( ( getline < "/etc/aap_blst" ) > 0) {
                if (bssid == $0) exit;
                }

        # now we get key if we're encrypted
        if ( crypt != "none" ) {
                rkey = "";
                while ( ( getline < "/etc/aap_wlst" ) > 0 ) {
                        if ($1 == bssid) rkey = $2;
                        }
                if ( rkey == "" ) exit;
                }

        print quality "\n" bssid "\n" essid "\n" channel "\n" crypt "\n" rkey > "/tmp/ap";

        print "uci set wireless.wl0.channel=" channel > "/tmp/script";
        print "uci set wireless.@wifi-iface[0].ssid=" essid >> "/tmp/script";
        print "uci set wireless.@wifi-iface[0].bssid=" bssid >> "/tmp/script";
        if (crypt == "none") {
                print "uci set wireless.@wifi-iface[0].encryption=none" >> "/tmp/script";
                }
        else {
                print "uci set wireless.@wifi-iface[0].key=" rkey >> "/tmp/script";
                print "uci set wireless.@wifi-iface[0].encryption=" crypt >> "/tmp/script";
                }
        }

(Last edited by cptdondo on 15 Sep 2009, 02:19)

I'd like to use these AAP scripts, but am getting the following error from this line:

# iwlist wl0 scan
wl0       Failed to read scan data : Invalid argument

What can I do to fix this?

If it helps, I'm running on the brcm-2.4 version of openwrt, on an asus 520gu router. Here are some rows from the syslog with more detail:

Dec 31 16:00:23 (none) user.emerg kernel: wl0: wlc_attach: chiprev 3 coreunit 0 corerev 13 cccap 0x104007ea maccap 0x30482205 band 2.4G, phy_type 5 phy_rev 0 ana_rev 6
Dec 31 16:00:23 (none) user.warn kernel: wl0: Broadcom BCM4318 802.11 Wireless Controller 4.150.10.5

And fwiw, I am able to connect to wireless networks by adjusting the settings in /etc/config/wireless, but would prefer to use the AAP scripts instead.

thanks
Dan

Nevermind. I needed to un-disable the wireless in the uci settings and run wifi.

The scripts work great! thanks!

Just a quick note: AAP 090916 is out with some new features/enhancements! Please read http://piranha.klashed.net/2009/09/16/p … -released/ for further information.

drbrown, cptdondo:
I'll response as soon as I have enough time to do so. brcm-2.4 will be maintained (WIP)

If you're interested in AAP and/or Piranha 3.0, I'd like to encourage you to follow my blog http://piranha.klashed.net/ for updates from now on - it's even possible to subscribe to my http://piranha.klashed.net/feed/ RSS feed. From my perspective it makes not too much sense to "spam" these forums every time I release something new. New releases of AAP will be (at any time) available at http://piranha.klashed.net/pub/aap/ and of Piranha 3.0 at http://piranha.klashed.net/pub/3.0/

I hope you feel this as an appropriate decision. I'll certainly check these forums on a regular basis, so feel free to ask whatever you like about AAP here and I'll come back to you asap.

Thanks,
orange

@Orange

Found this thread more informative. I have your script running on the WGT634U, just playing around with it now. Still trying to figure out how to kick it in as a backup on my other project. Almost forgot, I can try out your script with the ath9k driver and N if you think it would work. I know ath0 is more like wlan0 or wifi0, can't remember.

(Last edited by foxtroop11 on 28 Sep 2009, 03:16)

One more thing, would it be hard to use this with broadcom wireless devices? I wasn't sure if that's what was addressed earlier or if that was just in reference to the 2.4 kernel broadcom itself. Just thinking about all the devices dd-wrt autoap currently run's on and would like to switch them over to openwrt and your aap, they never did add in wpa encryption and that's something I need.

Thanks

The discussion might have continued from here.