Wednesday 21 September 2011

Powerful networking with Wicd

Wicd is a very clever network manager. It is independent of any desktop environments meaning that it is compatible with any window managers. It has a daemon running in the background and a client with a ststus icon in the systray and a cool GUI to manipulate your network connections. Wicd handles both wired and wireless connections. You can set it up to automatically connect to the wired network when you plug in your ethernet cable and in other circumstances connect to your preferred wifi access points.

Installation is easy, just follow the Wicd Arch Wiki. I put the wicd daemon in my /etc/rc.conf (after dbus) and disabled the original networking daemon:
DAEMONS=(...dbus ... !network ... wicd ...)
I edited my .xinitrc to run the client when X starts:

...
wicd-client &
...
exec ck-launch-session wmfs

(See my actual rc.conf and .xinitrc files on the right panel of this blog.)

Above the basic networking services Wicd gives you scripting possibilities to execute programs on different networking events and different networking environments. This feature is very useful for laptops that are brought from one network to another (home / office / net café, etc.) and the behavior of the machine can be altered according to the network it is connected to. Just some ideas:
- You can automatically mount nfs and/or samba shares when you connect to your office network.
- You can start or stop daemons according to your network connection (i.e. if you use network printers you can set up your cupsd service to start only at places where you use printers).
- You can switch off your wi-fi antenna after disconnecting from a wireless network to gain battery life when your laptop is running on battery power.

Mainly I use my laptop in my office and at home. In the office I have both wired and wireless networks (at my desk I usually plug in the network cable but when I am at a meeting in another room I use wi-fi). I need the same settings for both cases. At home I use wireless internet, I do not have a printer nor do I use nfs file shares. I want to access samba shares anywhere.

You can find the preconnect, postconnect, predisconnect, postdisconnect directories under /etc/wicd/scripts/. Bash scripts placed in these directories are executed each time the appropriate network event (the nemes of the directories are self-explanatories) occurs. I use two of them but their usage depends only on your creativity.

I created a share_mount.sh script that runs after wicd connects to a network and a share_umount.sh that runs before disconnecting. If the network is wired or the wireless ESSID belongs to my office's wifi router then I start the rpcbind and nfs-common daemons to use nfs, the cupsd daemon for printing and I mount my office nfs shares (defined previously in fstab with noauto option). In all networks I start samba and smbnetfs to automatically mount samba shares after network connection. Logs go to the /etc/wicd/<script_name>.log file. I used wicd's sample scripts to build up my own ones:

Postconnect script ( /etc/wicd/scripts/postconnect/share_mount.sh ):

#!/bin/bash

script="$(basename "$0")"
script_name="${script/.sh/}"

echo "Running ${script}" >"/var/log/wicd/${script_name}.log"
exec 2>>"/var/log/wicd/${script_name}.log"
exec 1>&2

connection_type="$1"
echo "Connection type: ${connection_type}"

if [ "${connection_type}" == "wired" ]; then
profile="$3"
echo "Profile: ${profile}"
elif [ "${connection_type}" == "wireless" ]; then
essid="$2"
bssid="$3"
echo "ESSID: ${essid}"
echo "BSSID: ${bssid}"
else
echo "Unknown connection type: ${connection_type}" >&2
exit
fi

case $2 in "wired" | "myoffice_essid")
# I am at office:
rc.d start rpcbind nfs-common cupsd;
mount -a -t nfs;;
esac
# I am anywhere:
rc.d start samba smbnetfs


Predisconnect script ( /etc/wicd/scripts/predisconnect/share_umount.sh ):

Before disconnecting a network this script handles the file unmounting and daemon stopping process in the opposite way I used in my connect script.

#!/bin/bash

script="$(basename "$0")"
script_name="${script/.sh/}"

echo "Running ${script}" >"/var/log/wicd/${script_name}.log"
exec 2>>"/var/log/wicd/${script_name}.log"
exec 1>&2

echo umounting nfs and smb filesystems
#umount /media/Erba_nfs
umount -a -t nfs
rc.d stop cupsd smbnetfs samba nfs-common rpcbind


Wicd's actual (1.7.0) version is not able to handle ad-hoc networking. Scripts can help you again, see the workaround here.

2 comments:

  1. Thank you, György. This is exactly what I was looking for :D

    ReplyDelete
  2. I found that having this at the top of my script file cause the script not to run (Raspbian):
    #!/bin/sh

    Changing to this (as you have it) made life wonderful:
    #!/bin/bash

    Small change. Big difference! Here it is in case anyone else runs into this issue.

    ReplyDelete