Automatic Network Setup · Sep 23, 10:06 AM by Dylan Doxey

I don’t have any fancy GUI thing on my Xubuntu machine to help configure my network.
So, when I go to a friends house or return home with my laptop, I have to manually tweak my /etc/network/interfaces file accordingly.
To make this easier I save each variation with the SSID in the file name.
/etc/network/interfaces-oinker
/etc/network/interfaces-matas
/etc/network/interfaces-generic
/etc/network/interfaces-sdairport
/etc/network/interfaces-actiontec
/etc/network/interfaces-hhonors
/etc/network/interfaces-ibahn
/etc/network/interfaces-oscon
/etc/network/interfaces-metrofi
/etc/network/interfaces-flypdx
/etc/network/interfaces
This makes the procedure just a matter of:
Well, that’s just not automatic enough. I deserve more!
How about if I write a shell script which scans the available networks and finds one that matches one of my sample interfaces files?
Here’s a good place to start:
ESSID:“oinker”
ESSID:“Tierra”
ESSID:“2WIRE350”
ESSID:“2WIRE996”
ESSID:“CARLINBAYBOOZIN”
ESSID:“wifiharbor”
ESSID:“belkin54g”
Let’s narrow that down a little:
oinker
Tierra
2WIRE350
2WIRE996
CARLINBAYBOOZIN
wifiharbor
belkin54g
Now all we need to do is check to see if there exists a file: /etc/network/interfaces-[ssid]
So, for convenience let’s store that data in a variable.
dylan:$ ssids=`iwlist scanning 2>/dev/null | grep ESSID | awk -F: '{print $2}' | awk -F\" '{print $2}'`Now we can iterate through the values and check them individually:
for ssid in $ssids; do if [ -e /etc/network/interfaces-$ssid ]; then echo Bingo: $ssid; break; fi; done Bingo: oinker
That works pretty good. It identifies the first available SSID which matches one of my existing sample interfaces files.
So, let’s bring it all together in a fancy shell script.
dylan:$ sudo vim /usr/local/bin/set_interfaces
#!/bin/bash
target_ssid=
ssids=`iwlist scanning 2>/dev/null | grep ESSID | awk -F: '{print $2}' | awk -F\" '{print $2}'`
for ssid in $ssids
do
if [ -e /etc/network/interfaces-$ssid ]
then
target_ssid=$ssid
echo Target SSID: $target_ssid
sudo cp /etc/network/interfaces-$target_ssid /etc/network/interfaces
break
fi
done
if [[ $target_ssid == "" ]]
then
echo No candidate networks found.
fi
Okay, if you’re like me, you’re thinking about the obvious limitation: what if there are multiple network SSIDs matching among my sample interfaces files, and I wish to connect to one other than the first one matched? Although this is not an issue for me, I could see easily modifying this script to take a single argument which would be the SSID, or an ordered list of SSIDs, for your preferred network(s).
But I’m not going to get into that.
Happy computing.

Commenting is closed for this article.
Getting fancy with xfce4-terminal Hard Drive Formatting: Linux Style
