# Another WLAN Association script

## amiatrome

I have written a simple bash script to cycle through available WLAN APs, starting with user-preferred APs then to open/insecure APs, stopping only when it establishes(DHCP) and verifies a valid WWW/network connection by pinging a server.

It's on sourceforge and here's part of the README. A present screenshot and an older screenshot(secure APs weren't skipped back then).  I wrote it for fun and practical purposes. My IT security company doesn't give interns internet access.   :Confused: 

It works great for me at home or work. Maybe some of you can find use for it too.   :Wink: 

```
FEATURE LIST

============

Multiple wireless interfaces supported (eth1, wlan0, wlan1 etc)

Customisable timeouts

Supports user-preferred APs (add your home AP or favourite open AP here)

Supports blacklisted APs (add your neighbours' or suspicious APs here)

Supports failed APs (save time by not trying a failed AP again in the same session)

Supports dead APs (ignore APs that have failed consistently)

WEP encryption supported

Supports specific AP connection even if SSID isn't unique (by forcing the AP MAC)

Connect to APs based on signal quality(behaviour can be changed)

Checks for WWW or network connectivity using host of choice

Informative user messages to monitor script activity
```

----------

## Bigun

Looks slick, where's a link to try it?

----------

## UberLord

https://sourceforge.net/project/showfiles.php?group_id=130772

Zero downloads :/

----------

## Bigun

Well, guess I was the first, I'll let you know how it goes

----------

## emes

Cool, but you are aware that baselayout 1.11.* can do this already, right?

----------

## amiatrome

 *emes wrote:*   

> Cool, but you are aware that baselayout 1.11.* can do this already, right?

 

Oh yes yes. But I wasn't keen on emerging baselayout because I had just installed gentoo on my laptop previously and was suffering from emerge fatigue.  :Smile: 

It has been serving me well at work and home so far and I would appreciate any feedback about it in other enviroments.

----------

## emes

Ok... a couple things:

1. I dont care about detected or failed aps, put a . before the filename or just dont make them at all, but i dont want to see them  :Smile: 

2. you should set the location of dhcpcd, iwconfig, etc with $(which program) instead of a path so that it works on other distros

3. a . before user_ap_list would help too

4. maybe find some way to make this a daemon that automaticly runs if the wireless connection is lost?

----------

## Bigun

For some reason the script will not "associate" with my wireless network.  I turned up the timeout to 30 seconds, and it still fails.  

Also, after successful connection, anyway to retrieve an address via DHCP or my manual config via config file?  It be nice to have the config file seperate from the script.

----------

## amiatrome

 *emes wrote:*   

> Ok... a couple things:
> 
> 1. I dont care about detected or failed aps, put a . before the filename or just dont make them at all, but i dont want to see them 
> 
> 2. you should set the location of dhcpcd, iwconfig, etc with $(which program) instead of a path so that it works on other distros
> ...

 

Hi! With regard to 1, 2 and 3, you could change the following in twas.sh.

edit: I will reflect them in the next release

```
IWLIST="/usr/sbin/iwlist"

IWCONFIG="/usr/sbin/iwconfig"

DHCPCD="/sbin/dhcpcd"

DETECTED_AP_LIST=~/detected_ap_list

USER_AP_LIST=~/user_ap_list

FAILED_AP_LIST=~/failed_ap_list

DEAD_AP_LIST=~/dead_ap_list
```

As to 4, here's something off the top of my head. I am supposed to be working now so I havent tested the code yet so please excuse any syntax errors or even logic errors.  :Embarassed:  If you do try it, any feedback would be appreciated. :Smile: 

```

Place the code blocks under their respective sections indicated

#SCRIPT CONFIGURATION

AUTO_RECONN=1         # enable = 1

CONN_CHECK_INTERVAL=300      # check every 5 mins

AUTO_RECONN_LIMIT=5      # number of times to try reconnect and fail before giving up

#AP CONNECTION GENERAL CODE

Auto_Reconn()

{

for (( COUNT = 0 ; COUNT < "$AUTO_RECONN_LIMIT" ; COUNT++ )); do

   

   if ping -q -c "$NUM_PINGS" -W "$PING_TIMEOUT" "$PING_SERVER" &> /dev/null; then

      COUNT=0

      sleep $CONN_CHECK_INTERVAL

   else

      Multiple_Wifaces

   fi

   

done

}

# arrange functions here

Multiple_Wifaces

if [ "$AUTO_RECONN" == "1" ]; then

   Auto_Reconn

fi

```

----------

## amiatrome

 *bigun89 wrote:*   

> For some reason the script will not "associate" with my wireless network.  I turned up the timeout to 30 seconds, and it still fails.  
> 
> Also, after successful connection, anyway to retrieve an address via DHCP or my manual config via config file?  It be nice to have the config file seperate from the script.

 

Hi! Did you happen to specify your MAC in user_ap_list as well? If yes, leave it out and see if it works this time. It's a bug and I will get it sorted out asap.

If no, below is the code used in the script to associate. Perhaps you could try iwconfig <wiface> essid <ssid> key <key> manually to see if it works for you. Lastly, my check for a successful association is checking if iwconfig <wiface> shows a signal level. ie. "level=-" It could be that this string isnt found in your iwconfig <wiface> output.

```

$IWCONFIG "$WIFACE" essid "$ESSID" key "$WEP_KEY"      # associate using ssid and key

sleep $ASSOCIATION_TIME                     # wait a while for association to complete

if $IWCONFIG "$WIFACE" | grep "level=-" > /dev/null; then      # proceed if association is successful
```

Do you mean that you want to specify an ip to use instead of dhcp? That and a separate config file to support multiple users are probably highly desired. I will try my best to add them soon.  :Smile: 

----------

## Bigun

 *amiatrome wrote:*   

> Perhaps you could try iwconfig <wiface> essid <ssid> key <key> manually to see if it works for you. Lastly, my check for a successful association is checking if iwconfig <wiface> shows a signal level. ie. "level=-" It could be that this string isnt found in your iwconfig <wiface> output.
> 
> 

 

Ya, I used iwconfig <wiface> essid <ssid> key <key> before I tried this script.  Worked fine.

----------

## amiatrome

 *bigun89 wrote:*   

> Ya, I used iwconfig <wiface> essid <ssid> key <key> before I tried this script.  Worked fine.

 

Ok. We have eliminated 1 possibility. Have you tried not specifying yr MAC? Is the check for successful association applicable to your iwconfig output?

Taking into account the 2 suggestions above, what the script does is very simple(nice pseudo-code below) and if it still doesn't work then I am stumped. A mistake in the WEP key perhaps?  :Sad: 

```

if WEP_KEY not blank

   Attempt to associate with ESSID with WEP key on

   iwconfig WIFACE essid ESSID key WEP_KEY

else

   Attempt to associate with ESSID with WEP key off

   iwconfig WIFACE essid ESSID

Wait 5 seconds for association to complete.

sleep 5

if "iwconfig WIFACE" output contains a string 'level=-' then

   Associated with ESSID through WIFACE

      proceed

else

   Couldn't associate with ESSID

      return

```

I just downloaded a copy of the script from sourceforge and I got connected in less than 5 seconds with the following user_ap_list.  :Very Happy: 

```
tejpssid|6e2332643d912d2d3b5981262c
```

```

root@amiatrome twas # ./twas.sh 

lo        no wireless extensions.

eth0      no wireless extensions.

eth1      IEEE 802.11b  ESSID:"NETGEAR"  Nickname:"ipw2100"

GOOD NEWS Everything seems to be OK. Starting...

INFORMATION Detecting APs around us through eth1

INFORMATION Attempting to go online using preferred APs listed in /root/user_ap_list through eth1

INFORMATION Attempting to associate with tejpssid with WEP key on

INFORMATION Let's wait 5 seconds for association to complete...

GOOD NEWS Associated with tejpssid[] through eth1

GOOD NEWS Acquired an IP address, we are in the network through eth1

GOOD NEWS Ping successful, we are online through eth1

INFORMATION Exiting...

```

----------

## Bigun

Yeah, I tried with and without.

----------

## amiatrome

 *bigun89 wrote:*   

> Yeah, I tried with and without.

 

It really should work for you. Could you run the script again and then terminate the script by pressing Ctrl-c when it is waiting for association to complete? Then post the output of iwconfig <wiface>. Thanks.

----------

## amiatrome

 *UberLord wrote:*   

> https://sourceforge.net/project/showfiles.php?group_id=130772
> 
> Zero downloads :/

 

Sigh. Is there something wrong with sourceforge's dl stats? The script has better stats (~22 dls) at freshmeat even though I didnt add its link here previously and we all know links here get loads of hits.  :Confused: 

----------

