#!/bin/sh ######################################################################### # # FILE : winmount # DATE : 19.9.2001 # AUTHOR : Patrick Meier / patrick.meier@gmx.net # DESCRIPTION : mounts a windows driver or directory with samba # # Copyrigth (C) 2001 by Patrick Meier, Switzerland # ######################################################################### PN=`basename "$0"` ############################################################################# # settings ############################################################################# STARTSTRING="==>" USERNAME="$USER" PASSWORD="" MOUNTCOMMAND="/bin/mount" SAMBA_PARAM=" -t smbfs -o" MAINMOUNTPOINT="/mnt" SAMBA_CONFIG="/etc/samba/smb.conf" ############################################################################# ######################################################################### # Usage ######################################################################### Usage() { echo "$PN - mounts a windows driver or directory with samba" echo "usage: $PN [ -u username ] [-p password ] [-m localmount-point ] directory1..." exit 1 } ############################################################################# # read config ############################################################################# DOMAINNAME="`cat "$SAMBA_CONFIG" | grep "workgroup" | awk -F"=" '{print $2}' | sed 's/ //g'`" #FILEMASK="`cat "$SAMBA_CONFIG" | grep "create mode" | awk -F"=" '{print $2}' | sed 's/ //g'`" #DIRECTORYMASK="`cat "$SAMBA_CONFIG" | grep "directory mode" | awk -F"=" '{print $2}' | sed 's/ //g'`" ############################################################################# # get password ############################################################################# getPassword() { # get password message="${STARTSTRING}windows password:" while [ -z "$PASSWORD" ] do printf "$message" stty -echo read PASSWORD stty sane #stty erase ^H printf "\n" done } ############################################################################# # mount a windows drive or directory ############################################################################# mountWinDriver() { WINMOUNTPOINT="$@" WINMOUNTPOINT="`echo ${WINMOUNTPOINT%/}`" # remove trailing '/' MSHOST="`echo ${WINMOUNTPOINT##//}`" MSPATH="`echo $MSHOST/$MSPATH | awk '{ l=split($0, p, "/"); if( l>2 ) for(i=2; i<=l-1; i++) printf p[i]"/"; }'`" MSHOST="`echo ${MSHOST%%/*}`" MSHOST=`echo $MSHOST | tr 'A-Z' 'a-z'` LOCALMOUNTPOINT="${MAINMOUNTPOINT}/$MSHOST/$MSPATH" LOCALMOUNTPOINT="`echo ${LOCALMOUNTPOINT%/}`" # remove trailing '/' # test mount points if [ -d "$MAINMOUNTPOINT" ] then : else echo "${STARTSTRING}ERROR: $MAINMOUNTPOINT path not found!" exit -1 fi if [ -d "$LOCALMOUNTPOINT" ] then : else echo "${STARTSTRING}$LOCALMOUNTPOINT path not found, create it..." PATHTOCREATE="`echo $MSHOST/$MSPATH | awk '{ l=split($0, p, "/"); for(i=0; i<=l; i++) print p[i]; }'`" CURRENTPATH="$MAINMOUNTPOINT" for i in $PATHTOCREATE do CURRENTPATH="$CURRENTPATH/$i" if [ -d "$CURRENTPATH" ] then : else if( eval "mkdir $CURRENTPATH" ) then : else echo "${STARTSTRING}ERROR: Can not create $CURRENTPATH!" exit -1 fi fi done fi if [ -n "`$MOUNTCOMMAND | grep "$WINMOUNTPOINT" | awk '{print $1}'`" ] then echo "${STARTSTRING}ERROR: already mounted!" exit -1 fi if [ -z "$MSPATH" ] then echo "${STARTSTRING}ERROR: you specified an invalid share name!" exit -1 fi getPassword MOUNTOPTIONS="username=${USERNAME},password=${PASSWORD},workgroup=${DOMAINNAME},guest,rw" echo "${STARTSTRING}mounting $WINMOUNTPOINT to $LOCALMOUNTPOINT" cmd="$MOUNTCOMMAND $SAMBA_PARAM $MOUNTOPTIONS $WINMOUNTPOINT $LOCALMOUNTPOINT>dev/null" if ( eval "$cmd" ) then : else echo "${STARTSTRING}ERROR: could not mount $WINMOUNTPOINT to $LOCALMOUNTPOINT!" exit -1 fi } ############################################################################# # main ############################################################################# while [ $# -gt 0 ] do case "$1" in -u) if [ $# -gt 1 ] ; then USERNAME="$2"; shift; else ParameterFailed "$1 archive-name.tar"; fi;; -p) if [ $# -gt 1 ] ; then PASSWORD="$2"; shift; else ParameterFailed "$1 archive-name.tar"; fi;; -m) if [ $# -gt 1 ] ; then MAINMOUNTPOINT="$2"; shift; else ParameterFailed "$1 archive-name.tar"; fi;; -*) Usage; exit 1;; *) break;; esac shift done if [ $# -lt 1 ] then echo "${STARTSTRING}ERROR: no direcory found!" Usage exit 1 fi for i in $@ do mountWinDriver $i done ############################################################################# # EOF #############################################################################