Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Mount All Available CIFS Shares using GVFS

Here’s a bash script that will automatically mount all available SAMBA shares that has access permission for the current user.   The script composes of two part: (1) The script itself and (2) the user’s credentials.

But current problem is that the user must store the password to access the SAMBA shares in the keyring before the script runs smoothly. It requires the user to access the any of the permitted shares and store the password permanently to the keyring, then add the script to the Startup Applications(System Settings -> Startup Applications)

Autfile is a simple text file contain the following:

username = myuser
password = mypass
domain = localdomain
servername = localserver

Here’s the code:

#!/bin/bash
ARGS="$*"

BASEDIR="/home/rnartos/.scripts/uicsmb"
OPTYPE=""
CMDTYPE=""
FILTERARGS=`echo "$ARGS" | sed 's/ /#/g' | sed 's/-/ -/g'`
AUTHFILE="$BASEDIR/authfile"
NETBASENAME="WinShares"



if [ ! -f $AUTHFILE ]; then
   echo "ERROR: neither default or user-defined authfile not exist."
   exit 0
fi

for CMD in $FILTERARGS; do
   MCMD=`echo $CMD | sed 's/#/ /g'| awk '{print $1}' `

   #umount
   if [ "$MCMD" = "-u" ]; then
      OPTYPE=" -u "
   fi

   #help
   if [ "$CMD" = "-h"  ]; then
     echo "GFVS WinShares - rnartos"
     echo "$0 -u -a <authfile> -h"
     echo "-u umount current shares"
     echo "-a process authentication file"
     echo "-h this help message."
     exit 0
   fi

   #authfile
   if [ "$MCMD" = "-a"  ]; then
     PCMD=`echo $CMD | sed 's/#/ /g'| awk '{print $2}' `
     if [ "$PCMD" = ""  ]; then
        echo "ERROR:  parameters for -a <authfile>"
     else
         if [ -f $PCMD ]; then
             AUTHFILE="$PCMD"
         else
           echo "$PCMD authfile not exist."
           exit 0
         fi
     fi
   fi
done


SERVERNAME=`cat $AUTHFILE | grep servername | sed 's/=//g' | awk '{print $2}'`
DOMAINNAME=`cat $AUTHFILE | grep domain | sed 's/=//g' | awk '{print $2}'`
USERNAME=`cat $AUTHFILE | grep username | sed 's/=//g' | awk '{print $2}'`
PASSWORD=`cat $AUTHFILE | grep password | sed 's/=//g' | awk '{print $2}'`
CHECKMOUNT=`gvfs-mount -l | grep "smb:"`
GETIP=`nmblookup $SERVERNAME | grep -v "query" | awk '{print $1}'`
GETSHARES=`smbclient --debuglevel=0 -A $AUTHFILE -L $SERVERNAME -I $GETIP -g  2>/dev/null | grep "Disk" | sed 's/|/ /g' | awk '{print $2}'`
WINSHARENAME="$HOME/$NETBASENAME/$DOMAINNAME/$SERVERNAME"

if [ ! -d $WINSHARENAME ];  then
   mkdir -p $WINSHARENAME
fi


for NAMES in $GETSHARES; do

   CHECKSHARE=`smbclient //$SERVERNAME/$NAMES -A $AUTHFILE -c '' 2> /dev/null | grep 'DENIED'`

   if [ "x$CHECKSHARE" = "x" ]; then
     if [ "$OPTYPE" = "" ]; then
         if [ "$CHECKMOUNT" = ""  ]; then
           CMDTYPE="mounting"
         fi
     else
        CMDTYPE="umounting"
     fi
     if [ "$CMDTYPE" = "" ]; then
        echo "Shares Already mounted."
        exit 0
     else
        echo "$CMDTYPE $NAMES"
	if [ "$OPTYPE" = "" ]; then
           gvfs-mount $OPTYPE smb://$SERVERNAME/$NAMES 2> /dev/null
           GVFSNAME=`echo $NAMES | tr '[:upper:]' '[:lower:]'`
           ln -sf "$HOME/.gvfs/$GVFSNAME on $SERVERNAME" $WINSHARENAME/$NAMES
        else
           gvfs-mount $OPTYPE smb://$SERVERNAME/$NAMES 2> /dev/null
        fi
     fi
   fi
done

exit 0 

One response to “Mount All Available CIFS Shares using GVFS

  1. Ricardo Gerhardt February 10, 2012 at 5:46 am

    How can i store a md5 password in the Autfile?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: