101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			101 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Bash
		
	
	
		
			Executable File
		
	
	
	
	
| #!/bin/bash
 | |
| # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
 | |
| source "$(dirname "$0")/lib/ulib/ulib" || exit 1
 | |
| urequire DEFAULTS
 | |
| 
 | |
| function display_help() {
 | |
|     uecho "$scriptname: Monter un partage Windows/Samba/CIFS
 | |
| 
 | |
| USAGE
 | |
|     $scriptname [user@]host[/path] [mountpoint]
 | |
| 
 | |
| Par défaut, le répertoire distant est montée sur un répertoire avec le même nom
 | |
| de base que l'hôte. Si le répertoire distant est déjà monté, il est démonté.
 | |
| Les options -M et -U permettent de modifier le comportement par défaut.
 | |
| 
 | |
| OPTIONS
 | |
|     -M
 | |
|         Forcer le montage
 | |
|     -U
 | |
|         Forcer le démontage
 | |
|     -o OPTIONS
 | |
|         Ajouter les options spécifiées à la commande de montage mount.cifs
 | |
|     -u USERNAME
 | |
|     -p PASSWORD
 | |
|     -c USERNAME:PASSWORD
 | |
|         Spécifier les credentials à utiliser pour la connexion"
 | |
| }
 | |
| 
 | |
| if ! progexists mount.cifs; then
 | |
|     [ -x /sbin/mount.cifs ] || die "Ce script nécessite cifs-utils. Sur debian, vous pouvez l'installer avec la commande
 | |
|     sudo apt-get install cifs-utils"
 | |
| fi
 | |
| 
 | |
| action=auto
 | |
| options=
 | |
| username=
 | |
| password=
 | |
| credentials=
 | |
| parse_opts "${PRETTYOPTS[@]}" \
 | |
|     --help '$exit_with display_help' \
 | |
|     -M action=mount \
 | |
|     -U action=umount \
 | |
|     -o:,--options: options= \
 | |
|     -u:,--user: username= \
 | |
|     -p:,--password:,--passwd: password= \
 | |
|     -c:,--credentials: credentials= \
 | |
|     @ args -- "$@" && set -- "${args[@]}" || die "$args"
 | |
| 
 | |
| if is_root; then
 | |
|     sudo=
 | |
| else
 | |
|     sudo=sudo
 | |
|     options="${options:+"$options,"}uid=$USER,gid=$USER,file_mode=0644,dir_mode=0755"
 | |
| fi
 | |
| 
 | |
| remote="${1#//}"
 | |
| [ -n "$remote" ] || die "Vous devez spécifier l'hôte et le répertoire distant à monter"
 | |
| splitfsep "$remote" / userhost path
 | |
| userhost="${userhost%:}"
 | |
| splituserhost "$userhost" cusername host
 | |
| [ -n "$username" ] || username="$cusername"
 | |
| [ -n "$username" ] || username="$USER"
 | |
| [ -n "$host" ] || die "Vous devez spécifier l'hôte distant à monter"
 | |
| [ -n "$path" ] || path='C$'
 | |
| 
 | |
| if [ -n "$credentials" ]; then
 | |
|     splitpair "$credentials" cusername cpassword
 | |
|     [ -n "$cusername" ] && username="$cusername"
 | |
|     [ -n "$cpassword" ] && password="$cpassword"
 | |
| fi
 | |
| [ -n "$username" ] && options="${options:+"$options,"}user=$username"
 | |
| [ -n "$password" ] && options="${options:+"$options,"}password=$password"
 | |
| 
 | |
| mountpoint="$2"
 | |
| [ -n "$mountpoint" ] || mountpoint="$host"
 | |
| [ -n "$mountpoint" ] || mountpoint=cifs
 | |
| mountpoint="$(abspath "$mountpoint")"
 | |
| 
 | |
| if [ "$action" == "auto" ]; then
 | |
|     if mount | grep -q "$mountpoint"; then
 | |
|         action=umount
 | |
|     else
 | |
|         action=mount
 | |
|     fi
 | |
| fi
 | |
| 
 | |
| if [ "$action" == "umount" ]; then
 | |
|     if $sudo umount "$mountpoint"; then
 | |
|         echo "Volume $mountpoint démonté avec succès"
 | |
|         rmdir "$mountpoint" 2>/dev/null
 | |
|         exit 0
 | |
|     fi
 | |
| elif [ "$action" == "mount" ]; then
 | |
|     mkdir -p "$mountpoint" || die
 | |
|     if $sudo mount -t cifs "//$host/$path" "$mountpoint" ${options:+-o "$options"}; then
 | |
|         echo "Volume //$host/$path monté avec succès sur $(ppath "$mountpoint")"
 | |
|         exit 0
 | |
|     fi
 | |
| fi
 | |
| exit 1
 | 
