Page 1 of 1

Script to Mount Windows Shares at Logon

Posted: Wed Jun 20, 2012 10:42 am
by Fergsauce
I have found the old method of editing fstab to auto-mount shares at startup not useful for a NAS that goes to sleep after a period of time. The NAS takes a few seconds to startup and the fstab mount fails.

So I tried a different method, and made a little script to mount them at logon. Then I added more to it.. then some more.. and I thought maybe someone out there would benefit from it. It also has room for improvement probably.

If you need permissions to the Windows share, connect to it via nautilus by going to 'File' > 'Connect to Server...' and fill out the information. Make sure you check the 'Remember Password' checkbox once you enter the correct credentials. Also, unmount it if you plan on testing this script.

1. Paste the following code in a file somewhere in your home directory and name it mount-shares.sh (or whatever you want)
2. Make it executable (chmod +x <path_to_file>)
3. Open 'Startup Applications' and in the command field enter the path to the script file followed by the parameters it requires.

(for parameters just run it in the command line: bash <path_to_file>)

Code: Select all

#!/bin/bash
# mount-shares.sh by Fergsauce
# This (bash) shell script is used to mount Windows shares and create symlinks
# to them in your home directory for easy access.
# 
# If permissions are required, first open the share through nautilus by
# clicking "File" > "Connect to Server..." and connecting to the desired
# server. Make sure to save the username and password used.
# 
# To use this at logon, open 'Startup Applications', click 'Add' and in the 
# command field, enter the path to this file followed by the parameters you want.
# 
# Run with no parameters to see usage.

reset_color='\e[0m'
color_red='\e[1;31m'
color_green='\e[1;32m'

fnExit()
{
	echo -ne $reset_color
	if [ "$1" != "0" ]; then
		echo -e "Error: $1\n"
	fi
	exit $1
}

fnUsage()
{
	echo -e $reset_color"\nUsage:\nbash mount-shares.sh [-s -S] <ip_of_server> [-n -N] <share_name>"
	echo -e "Optional parameter: [-f -F] <path_to_symlink> (The default folder is ~/Shares/<share_name>)\n"
	echo "Examples:"
	echo "sh mount-shares.sh -s 192.168.1.100 -n Shared"
	echo -e "sh mount-shares.sh -s 192.168.1.100 -n Documents -f /Shares/NAS/Documents\n"
	fnExit 1
}

if [ $# == 0 ]; then
	fnUsage
fi

if [ "$1" == "-s" ] || [ "$1" == "-S" ]; then
	server_ip="$2"
elif [ "$3" == "-s" ] || [ "$3" == "-S" ]; then
	server_ip="$4"
elif [ "$5" == "-s" ] || [ "$5" == "-S" ]; then
	server_ip="$6"
fi

if [ "$1" == "-n" ] || [ "$1" == "-N" ]; then
	share_name="$2"
elif [ "$3" == "-n" ] || [ "$3" == "-N" ]; then
	share_name="$4"
elif [ "$5" == "-n" ] || [ "$5" == "-N" ]; then
	share_name="$6"
fi

folder="Shares"
if [ "$1" == "-f" ] || [ "$1" == "-F" ]; then
	folder="$2"
elif [ "$3" == "-f" ] || [ "$3" == "-F" ]; then
	folder="$4"
elif [ "$5" == "-f" ] || [ "$5" == "-F" ]; then
	folder="$6"
fi

if [ -z $server_ip ]; then
	echo -e $color_red"Could not read the server ip"$reset_color
	fnUsage
fi

if [ -z $share_name ]; then
	echo -e $color_red"Could not read the share name"
	fnUsage
fi

if [ ! -d ~/$folder ]; then
	echo "Making the local folder: $folder"
fi

mkdir -p ~/$folder

if [ ! -d ~/$folder ]; then
	echo -e $color_red"Could not create target directory: ~/$folder"
	fnExit 2
fi

echo "Mounting $share_name..."
gvfs-mount smb://$server_ip/$share_name

i=0
while [ $i -lt 10 ] && [ ! -d ~/".gvfs/$share_name on $server_ip/" ]
do
i=$[$i+1]
sleep 1
done

if [ ! -d ~/".gvfs/$share_name on $server_ip/" ]; then
	echo -e $color_red"gvfs folder not found: "~/".gvfs/$share_name on $server_ip/"$reset_color
	echo -e "Check if there were errors mounting the share, or if it is located in the ~/.cache directory.\n"
	fnExit 3
fi

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
	echo "Creating symlink..."
	ln -s -f ~/".gvfs/$share_name on $server_ip/" ~/"$folder/$share_name"
fi

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
	echo "Attempting to fix gvfs..."
	fusermount -zu ~/.gvfs
	/usr/lib/gvfs/gvfs-fuse-daemon ~/.gvfs
fi

if [ ! -e ~/"$folder/$share_name" ] && [ -L ~/"$folder/$share_name" ]; then
	echo -e $color_red"Error creating symlink. Try manually."
	fnExit 4
fi

echo -e $color_green"Success! Your share is mounted and can be access at:"$reset_color
echo -e ~/"$folder/$share_name\n"
fnExit 0
*Edit: It should be noted that I am not a shell programmer by trade.

Also, goes without saying, but... This script is provided "as-is" and makes no warranty of any kind. Use of the information in this post is at your own risk.