Here's an idea using Perl to execute the script for you. You could translate this to your language of choice, or probably even bash. It's really the commands to connect to the wireless that count. Perl is just a means for me to do this quickly, your idea of quick may be different. A note if you're not used to reading Perl everything backticks is a shell command.
Gnome, for example, has built in handling of remembering your networks, and will do this for a logged in user, but... I'm assuming a headless setup. Which is kind of fun, a roving headless setup that will automatically connect to wifi. Cool.
This assumes a wpa network, and iwconfig installed (which I'll bet comes with your wireless packages), and wpa_supplicant.
This accounts for only one network defined in a csv. And assumes that it will work on the first try. You'll need to flesh it out with the actual parameters for the desired networks, or add other handling. But, this general structure will do it for you quickly.
File @ /mnt/usb/network.csv
in the format "target SSID,passphrase":
myWifiSSID,mypassword
File @ /home/me/connect.pl
(with chmod 755 for example):
#!/usr/bin/perl
# Set to your device name.
$iface = "wlan0";
# Set to your USB mount point & network definition CSV file.
$networkdefinition = "/mnt/usb/network.csv";
# Get the contents of your file on USB stick.
$line = `cat $networkdefinition`;
# Read the CSV into parts.
@parts = split(/,/,$line);
# Get a list of wireless networks in range.
# And grep that for the network we're looking for.
$foundnetworks = `iwlist $iface scan | grep 'ESSID:"$parts[0]"'`;
# See if this turns up the network we're looking for
if (length($foundnetworks)) {
# It did. Let's bring up that interface.
`wpa_supplicant -B -i $iface -c <(wpa_passphrase $parts[0] $parts[1])`;
} else {
# Network not in range, perform error handling etc.
}
In your crontab:
@reboot /home/me/connect.pl
Caveats:
- Needs better error handling / a way of running more regularly
- Needs handling of other networks other than WPA
- Cron solution assumes mount will be available by then.
That's the fun stuff to extend as you go along, but, could be a quick way to boot up using the USB stick CSV file to specify your wifi network.
The real meat of this are these two commands:
The rest is just really an idea on how to implement your proposed solution to automatic connection to a wifi ap.