Automatic Usage of Free Wi-Fi

by Rolf

Using free Wi-Fi is good for going online for free, reading emails and news, and doing other things when you are far away from home or your computer at school or work.  It's also good as a backup connection, when your own Internet connection is down.

But it's not easy: You have to go to a shop like Starbucks or McDonald's (and buy something) or you have to scan for open (unencrypted) Wi-Fi, try to connect, and test if you are online.  And often you can't connect because there is a MAC filter or you are out of range, and many open Wi-Fi networks are offline or require a payment for the Internet access.  And because only one of about 30 Wi-Fi networks is free, it's often time-consuming.

Microsoft Windows and the MacOS had as a default setting the auto-connect to open Wi-Fi networks.

You can still activate this property, but it does not test if the Wi-Fi is free (unencrypted, online, and without barriers like a MAC filter).  So the auto-connect from the OS often does not get you online, because most open Wi-Fi networks are not free.  Another disadvantage of the auto-connect from the OS is that it uses the hardware MAC, but for privacy it's better to use a random MAC.

So I made a free Bash script, licensed under the GPL, which does not have this disadvantage and works faster than a man could.

This is the short description: First, the Wi-Fi device name is the one and only command line parameter.  Than the MAC gets randomized by:

ran=$(cat /proc/interrupts | md5sum)
MAC=00:0$[$RANDOM%6]:${ran:0:2}:${ran:3:2}:${ran:5:2}:${ran:7:2}
ifconfig "$DEVICE" promisc
ifconfig "$DEVICE" hw ether $MAC

This does not work with every adapter, so you should check it.  For maximum range and noise immunity, the rate is set to 1 Mbit/s by:

iwconfig "$DEVICE" rate 1M

The next step is scanning for Wi-Fi networks by:

iwlist "$DEVICE" scanning

And parsing the output.  The list of open Wi-Fi networks is then sorted by quality (signal strength) to get the best possible connection.

Then the script tries to connect with the association:

iwconfig "$DEVICE" mode managed ap "${APMAC[$loop_counter]}" channel "${CHANNEL[$loop_counter]}" essid "${ESSID[$loop_counter]}"

And DHCP configuration:

type -P dhcpcd
if [ $? -eq 0 ]
  then # dhcpcd with 20 s timeout (default 60)
  dhcpcd -t 20 "$DEVICE"
else # dhclient which makes only one try to get a lease
  dhclient -1 "$DEVICE"
fi

If ifconfig then shows that we got an IP, the next step is checking the DNS server with two DNS requests.  If at least one DNS lookup was successful, the next step is downloading two simple files, e. g., a small Google logo.

If at least one file could be downloaded, we should be online.

This connection is being tested in a loop every ten seconds.  If the connection gets lost, go the next open Wi-Fi and test it.  If there is no next, continue with the previous MAC randomization in this endless loop.

The MAC randomization is also good for free Wi-Fi networks with a time limit, because the time limit usually is based on the MAC.

The script kills the network manager to avoid double usage of a resource which can't do that.  For the same reason it has a lockfile function to assure that the script terminates if a process with the same name set a lockfile before and is still running.

I tested the script in several shopping centers, public places, and railway stations and it works.

The script and a description are at: sslsites.de/www.true-random.com/homepage/projects/wifi/index.html

For users who can't use Bash scripts, I made USB keys with Knoppix Linux, where the auto-connect script gets started by a boot script: sslsites.de/www.true-random.com/homepage/projects/wifi/stick_e.html

The auto-connect script here has an additional endless loop over all Wi-Fi devices, so that it works with hot plugging; you can add or remove Wi-Fi devices without problems.

The script and the Knoppix does not store any files, so surfing with this key leaves no traces.

A gallery with this USB key in action is here: sslsites.de/www.true-random.com/homepage/projects/wifi/galleriee.html

One application there is downloading with a notebook in a closed briefcase, so that no one can see that Wi-Fi is used.

It's easy to hide the fact that you are using a free Wi-Fi even when someone sees that you must be online: You can simply plug a wireless USB modem and say that you are online with HSDPA, UMTS, GSM, GPRS, or EDGE but not Wi-Fi.  The gallery also shows such "deniable Wi-Fi."  With one finger close to the power button or magic system key request, and with the randomized MAC, this is really safe.

Important Update

Last October, the German journal Linux Magazin published an article with Perl scripts which opens Wi-Fi connections that have a splash page with advertising and terms of use.

The article and code can be found at: www.linux-magazin.de/Heft-Abo/Ausgaben/2010/11/Schluesseldienst and can be translated via Google.

A combination of my Bash script and these Perl scripts would automatically connect to free Wi-Fi and establish the Internet access without a splash page, advertising, or terms of use.

Code: open-wifi-auto-connect.sh  Version in the article.

Code: open-wifi-auto-connect_ping.sh

Code: freewifi_autoconnect.sh

Code: freewifi_autoconnect_old.sh

Return to $2600 Index