2009
05.05

I needed a way to auto mount WIndows Home Server shares whenever I’m working in the office. Here is how to do it using a smbfs and a small bash and perl script.

Install smbfs
smbfs is required to run: /usr/bin/smbmount

user@computer:$ sudo apt-get install smbfs

Enable your user account to run /usr/bin/smbmount as root

user@computer:$ sudo visudo

Add following lines:

# Samba
artur ALL=NOPASSWD: /usr/bin/smbmount

Create credentials files for your account

user@computer:$ vim ~/.smbpasswd

Put following lines into the file:

username=artur
password=...

Create mount point

user@computer:$ mkdir ~/Office Server

Create mounting script

#!/bin/bash

case $1 in
        mount)
                sudo smbmount //192.168.0.2/Users/artur /home/artur/Office Server/Artur -o credentials=/home/artur/.smbpasswd
                sudo smbmount //192.168.0.2/Public  /home/artur/Office Server/Public -o credentials=/home/artur/.smbpasswd
        ;;
        umount)
                sudo umount /home/artur/Office Server/Artur
                sudo umount /home/artur/Office Server/Public
        ;;
        status)
                mount -t cifs  
        ;;
esac

Place this script where you like. I’m keeping it in /usr/local/sbin.

Mount shares

user@computer:$ /usr/local/sbin/mount_office_server_shares.sh mount

Check are they mounted:

user@computer:$ /usr/local/sbin/mount_office_server_shares.sh status

How to mount only if shares are available

I have a small Perl script running apps and mounting those shares which I run at start of Gnome session:

#!/usr/bin/perl -w

use strict;

my $start_time = time;
my $success = 0;

while (time - $start_time < 300)
{
        my $server_check = qx(nmblookup -A 192.168.0.2 | awk {'print $1'} | awk 'NR==2 {print;exit}');
        chomp $server_check;
        my $mount_check = qx(df -h | grep -c 192.168.0.2);
        if ($server_check eq "SERVER1" and $mount_check == 0)        
        {
                system("/usr/local/sbin/mount_office_server_shares.sh mount");
        }
        sleep(1);
}

This script will check is the Home Server available in the network, check are the shares are not mounted already and mount them if necessary. It will try to mount for 5 minutes and exit if unsuccessful.

Don’t forget to add this script to Startup in Gnome: System -> Prefrences -> Startup applications.

Bookmark and Share

1 comment so far

Add Your Comment
  1. da best. Keep it going! Thank you