From cb938ad6ca05ec84c188ae77c10135fbf8f99796 Mon Sep 17 00:00:00 2001 From: mxmehl Date: Wed, 5 Apr 2017 16:59:39 +0200 Subject: [PATCH] enable configuration of where predefined mountpoints are mounted to --- config.cfg.sample | 6 +++--- mnt-sftp.sh | 53 ++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 46 insertions(+), 13 deletions(-) diff --git a/config.cfg.sample b/config.cfg.sample index 118ed42..da694fb 100644 --- a/config.cfg.sample +++ b/config.cfg.sample @@ -6,9 +6,9 @@ LOCALMOUNTDIR=/home/user/remote SSHCONFIG=$HOME/.ssh/config # If you don't want to use your .ssh/config file or need to define the passwords manually, you can add predefined entries here. -# Format: user;;host;;port;;pass;;path -#PRESSH[0]='user1;;domain1.org;;22;;secretpassword1;;/' -#PRESSH[1]='user2;;domain2.org;;23;;secretpassword2;;/home/user2/' +# Format: user;host;port;pass;path;localpath +#PRESSH[0]='user1;domain1.org;22;secretpassword1;/;/home/mypc/remote' +#PRESSH[1]='user2;domain2.org;23;secretpassword2;/home/user2/;/media/mylocalspace' ## mnt-share.sh diff --git a/mnt-sftp.sh b/mnt-sftp.sh index 1a2eed2..c0c4fb6 100755 --- a/mnt-sftp.sh +++ b/mnt-sftp.sh @@ -41,13 +41,14 @@ function sshadd { for ((i = 0; i < ${#PRESSH[*]}; i++)) do - PREUSER[$i]=$(echo ${PRESSH[$i]} | awk -F";;" '{ print $1 }') - PREHOST[$i]=$(echo ${PRESSH[$i]} | awk -F";;" '{ print $2 }') - PREPORT[$i]=$(echo ${PRESSH[$i]} | awk -F";;" '{ print $3 }') - PREPASS[$i]=$(echo ${PRESSH[$i]} | awk -F";;" '{ print $4 }') - PREPATH[$i]=$(echo ${PRESSH[$i]} | awk -F";;" '{ print $5 }') + PREUSER[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $1 }') + PREHOST[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $2 }') + PREPORT[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $3 }') + PREPASS[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $4 }') + PREPATH[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $5 }') + PRELOCAL[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $6 }') - PRESSHLIST[$i]=${PREUSER[$i]}"@"${PREHOST[$i]} + PRESSHLIST[$i]=${PREUSER[$i]}"@"${PREHOST[$i]}":"${PREPATH[$i]} done # Read all hosts from ~/.ssh/config @@ -57,6 +58,7 @@ SSHLIST=$(grep -E "Host\s[[:alnum:]]" $SSHCONFIG | awk -F' ' '{ print $2 }') function mount { if ! SSH=$(zenity --list \ --height=400 \ + --width=350 \ --text="Please choose. Cancel to unmount drives." \ --title="Choose SSH server" \ --column "Available SSH servers" \ @@ -75,10 +77,15 @@ function mount { if [ "$SSH" == "" ]; then MAN="1" SSHUSER=$(zenity --entry --text "SSH user" --title "Enter SSH user") + checkexit $? SSHHOST=$(zenity --entry --text "SSH host" --title "Enter SSH host") + checkexit $? SSHPORT=$(zenity --entry --text "SSH port (default 22)" --title "Enter SSH port") + checkexit $? SSHPASS=$(zenity --entry --hide-text --text "SSH password" --title "Enter SSH password") + checkexit $? SSHPATH=$(zenity --entry --text "Desired root directory on server (default /)" --title "Enter SSH root directory") + checkexit $? if [ "$SSHPORT" == "" ]; then SSHPORT=22 fi @@ -99,6 +106,7 @@ function mount { SSHPORT=${PREPORT[$i]} SSHPASS=${PREPASS[$i]} SSHPATH=${PREPATH[$i]} + SSHLOCAL=${PRELOCAL[$i]} fi done @@ -107,16 +115,22 @@ function mount { MAN="0" fi + if [ "$SSHLOCAL" != "" ]; then + MOUNTDIR="$SSHLOCAL" + else + MOUNTDIR="$LOCALMOUNTDIR"/"$SSH" + fi + # Make a local directory if not available - if [ ! -e "$LOCALMOUNTDIR"/"$SSH" ]; then - mkdir -p "$LOCALMOUNTDIR"/"$SSH" + if [ ! -e "$MOUNTDIR" ]; then + mkdir -p "$MOUNTDIR" fi # Command to mount actually if [ "$MAN" = "0" ]; then - sshfs -C "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks & + sshfs -C "$SSH": "$MOUNTDIR"/ -o follow_symlinks & else - echo "$SSHPASS" | sshfs -C -o password_stdin -p "$SSHPORT" "$SSHUSER"@"$SSHHOST":"$SSHPATH" "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks & + echo "$SSHPASS" | sshfs -C -o password_stdin -p "$SSHPORT" "$SSHUSER"@"$SSHHOST":"$SSHPATH" "$MOUNTDIR"/ -o follow_symlinks & fi quitquestion # one more ssh server or quit? @@ -144,6 +158,20 @@ function unmount { fi done + # Go through predefined mounts and unmount them if they're mounted + for ((i = 0; i < ${#PRESSHLIST[*]}; i++)) + do + PREUSER[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $1 }') + PREHOST[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $2 }') + PREPATH[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $5 }') + PRELOCAL[$i]=$(echo ${PRESSH[$i]} | awk -F";" '{ print $6 }') + SSH[$i]=${PREUSER[$i]}"@"${PREHOST[$i]}":"${PREPATH[$i]} + if [ "$(cat /etc/mtab | grep -q "${SSH[$i]}"; echo $?)" == "0" ]; then + fusermount -u ${PRELOCAL[$i]} + echo "${PRELOCAL[$i]} unmounted." + fi + done + # Remove all empty directories which have been created by the manual/predefined mount process (m-XYZ-$RANDOM) find "$LOCALMOUNTDIR"/ -maxdepth 1 -type d -empty -regextype sed -regex ".*/[pm]-.\+\?-[0-9]\{2,6\}" -delete @@ -159,6 +187,11 @@ function quitquestion { fi } +# Check whether one clicked "cancel" in zenity question +function checkexit { + if [ $1 = 1 ]; then exit 1; fi +} + sshadd # sshadd function # Loop for endless mounts until stopped by unmount or unmountquestion