My own flavour of network config automatic choosing

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 WorkNET

iface 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.1

iface 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.

5 thoughts on “My own flavour of network config automatic choosing”

  1. Hi Enrique,

    have you ever tried the NetworkManager? It’s completely desktop-independent, so you can use it with your favourite one ;-). It’s just a dbus service that manages all the interfaces that were not in the /etc/network/interfaces. It’s very useful if all of the networks you connect to, have their IPs managed by a DHCP server. I’m very satisfied with it, and it’s also well integrated with the GNOME desktop through an applet.

  2. Yes, Gnome Network Manager was the program that I was using just before I wrote this post. I liked it because I had’t found a KDE equivalent being as much easy and comfortable as this one, and its ability of supporting multiple network profiles was the feature I just needed.

    But the fact is that this program have some bugs storing its configuration, and last day it went crazy and just segfaulted every time I tried to start it. Moving some profiles out, made it to work, but restoring them to their original location made the program to fail again. I looked deep into the files, and there were nothing strange, so I gave up and decided to look for alternatives.

Comments are closed.