Until a couple of days ago, I used to use Gnome network-admin’s ability of having multiple network profiles to configure my wifi card at work and at home. But yesterday, the program went crazy and started to crash without any logical reason and I decided to shift to another solution: logical network interface mapping.
Interface mapping is a feature of ifupdown (/etc/network/interfaces). You can define some logical interfaces and call a script in order to choose which one will be mapped to a physical interface. The /etc/network/interfaces could be like this one (being HomeNET and WorkNET two existing ESSID network identifiers):
auto eth1
mapping eth1
script /root/WIFI/wldetect.sh
map HomeNET HomeNET
map WorkNET WorkNETiface HomeNET inet static
address 192.168.1.2
netmask 255.255.255.0
gateway 192.168.1.1
wireless-essid HomeNET
wireless-key s:mysecretpass1 open
dns-nameservers 127.0.0.1 192.168.1.1iface WorkNET inet dhcp
wireless-essid WorkNET
wireless-key s:mysecretpass2 open
dns-nameservers 127.0.0.1 192.168.100.1
I’m using resolvconf to activate the dns-nameservers directive in the interfaces file, and dnsmasq to take care of some dns issues with a couple of VPNs I use. That explains the first 127.0.0.1 entry.
The script /root/WIFI/wldetect.sh lists the available networks and chooses one of them being in a whitelist (HomeNET|WorkNET):
#!/bin/sh
# Config
WL_IFACE=`iwconfig 2>/dev/null | { read A _; echo $A; };`# Reset the interface
ifconfig $WL_IFACE down
ifconfig $WL_IFACE 0.0.0.0
ifconfig $WL_IFACE up# Search networks
NETWORKS=`iwlist $WL_IFACE scanning | grep ESSID | sed -e ‘s/.*”(.*)”/1/’`# Bring down the interface
ifconfig $WL_IFACE down# Select preferred networks
for NET in $NETWORKS
do
case $NET in
HomeNEt|WorkNET)
echo $NET;
exit 0;
;;
esac
done
exit 1;
And that’s all! I hope that this configuration could be helpful for someone.