Archive for February, 2010

Customizing Cups Default Printer using NetworkManager

February 16, 2010

Recently I discovered the scripting abilities of NetworkManager. I
finally wrote a very small script which calls lpadmin depending on my
current network environment.

NetworkManager looks for scripts in
/etc/NetworkManager/dispatcher.d. Currently I have:


lulu:/etc/NetworkManager/dispatcher.d> ls
01ifupdown 97customize-lulu

01ifupdown came with Karmic, 97customize-lulu is my new script.
NetworkManager calls the scripts in /etc/NetworkManager/dispatcher.d
with two arguments. If I e.g. plug in a patch cable with WLAN
disabled, the scripts will be run like so:


01ifupdown eth0 up
97customize-lulu eth0 up

In my 97customize-lulu, I figure out the current IPADDRESS using the
ip-command and a bit of perl. In a simple case I call lpadmin
appropriately. Finally I call logger to get some information in my
/var/log/messages.

When the script is called to unconfigure a nic, I umount all NFS and
CIFS based filesystems to avoid hanging IOs.

NIC=$1
ACTION=$2

case "$2" in
up)
IPADDRESS=`ip -o addr show $NIC | perl -ne 'if(/inet ([\d\.]*)\/\d\d/) {print $1, "\n";}'`
case "$IPADDRESS" in
192.168.178.*) lpadmin -d Home-HP-OfficeJet-G85
;;
172.26.0.74)
lpadmin -d Lab-X
;;
172.18.*.*) lpadmin -d Site-Foo
;;
esac
logger 97customize-lulu: NIC: $NIC ACTION: $ACTION IP: $IPADDRESS
;;
down)
umount -a -t nfs,cifs
logger 97customize-lulu: NIC: $NIC ACTION: $ACTION IP: $IPADDRESS RC$
;;
esac