2014-11-28 17:02:20 +01:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
|
|
cd "$(dirname "$(readlink -f "$0")")"
|
|
|
|
|
# Test if config.cfg exists and set needed variables
|
|
|
|
|
if [ ! -e config.cfg ]; then echo "Missing config.cfg file. Edit and rename config.cfg.sample"; exit 1; fi
|
|
|
|
|
source config.cfg
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add SSH key to local keyring if not already happened
|
|
|
|
|
function sshadd {
|
|
|
|
|
ssh-add -l >/dev/null || ssh-add
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Choose preconfigured HOST to mount
|
|
|
|
|
function mount {
|
|
|
|
|
if ! SSH=$(zenity --list \
|
|
|
|
|
--height=400 \
|
|
|
|
|
--text="Please choose. Cancel to unmount drives." \
|
|
|
|
|
--title="Choose SSH server" \
|
|
|
|
|
--column "Preconfigured SSH servers" \
|
2015-03-23 17:55:58 +03:00
|
|
|
"<manual input>" ${PRESSH[*]}); then
|
2014-11-28 17:02:20 +01:00
|
|
|
unmountquestion # If you press cancel, it should ask you to unmount all drives
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# If you double click on an entry, it gives 'server1|server1' as result instead of 'server1'
|
|
|
|
|
# This command cuts of everything after |
|
|
|
|
|
SSH=$(echo $SSH | awk -F\| '{ print $1 }')
|
|
|
|
|
|
2015-03-23 17:55:58 +03:00
|
|
|
if [ "$SSH" == "<manual input>" ]; then
|
|
|
|
|
MAN=1
|
|
|
|
|
SSHUSER=$(zenity --entry --text "SSH user" --title "Enter SSH user")
|
|
|
|
|
SSHPASS=$(zenity --entry --hide-text --text "SSH password" --title "Enter SSH password")
|
|
|
|
|
SSHHOST=$(zenity --entry --text "SSH host" --title "Enter SSH host")
|
|
|
|
|
SSHPORT=$(zenity --entry --text "SSH port (default 22)" --title "Enter SSH port")
|
|
|
|
|
SSHPATH=$(zenity --entry --text "Desired root directory (default /)" --title "Enter SSH root directory")
|
|
|
|
|
if [ "$SSHPORT" == "" ]; then
|
|
|
|
|
SSHPORT=22
|
|
|
|
|
fi
|
|
|
|
|
if [ "$SSHPATH" == "" ]; then
|
|
|
|
|
SSHPATH="/"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
SSH="$SSHUSER-$RANDOM"
|
|
|
|
|
fi
|
|
|
|
|
|
2014-11-28 17:02:20 +01:00
|
|
|
# Make a local directory if not available
|
|
|
|
|
if [ ! -e "$LOCALMOUNTDIR"/"$SSH" ]; then
|
|
|
|
|
mkdir -p "$LOCALMOUNTDIR"/"$SSH"
|
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
# Command to mount actually
|
2015-03-23 17:55:58 +03:00
|
|
|
if [ $MAN = 0 ]; then
|
|
|
|
|
sshfs "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
|
|
|
|
|
else
|
|
|
|
|
echo "$SSHPASS" | sshfs -o password_stdin -p "$SSHPORT" "$SSHUSER"@"$SSHHOST":"$SSHPATH" "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
|
|
|
|
|
fi
|
2014-11-28 17:02:20 +01:00
|
|
|
|
|
|
|
|
quitquestion # one more ssh server or quit?
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Ask if all preconfigured SSHFS drives should be unmounted
|
|
|
|
|
function unmountquestion {
|
|
|
|
|
zenity --question --text="Unmount all preconfigured\nSSHFS drives now?"
|
|
|
|
|
if [ "$?" = "0" ]; then
|
|
|
|
|
unmount # unmount function
|
|
|
|
|
else
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Procedure to unmount all preconfigured SSHFS drives and exit program afterwards
|
|
|
|
|
function unmount {
|
2015-03-23 17:55:58 +03:00
|
|
|
#for ((i = 0; i < ${#PRESSH[*]}; i++))
|
|
|
|
|
#do
|
|
|
|
|
#fusermount -u "$LOCALMOUNTDIR"/"${PRESSH[$i]}"
|
|
|
|
|
#echo ""${PRESSH[$i]}" unmounted."
|
|
|
|
|
#done
|
|
|
|
|
|
|
|
|
|
for FILE in "$LOCALMOUNTDIR"/*;
|
2014-11-28 17:02:20 +01:00
|
|
|
do
|
2015-03-23 17:55:58 +03:00
|
|
|
if [ "$(cat /etc/mtab | grep -q "$FILE"; echo $?)" == "0" ]; then
|
|
|
|
|
fusermount -u "$FILE"
|
|
|
|
|
echo "$FILE unmounted."
|
|
|
|
|
fi
|
2014-11-28 17:02:20 +01:00
|
|
|
done
|
2015-03-23 17:55:58 +03:00
|
|
|
|
2014-11-28 17:02:20 +01:00
|
|
|
exit 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Should another SSHFS storage be mounted?
|
|
|
|
|
function quitquestion {
|
|
|
|
|
zenity --question \
|
|
|
|
|
--text="Mount another SSHFS storage?"
|
|
|
|
|
if [ "$?" = "1" ]; then
|
|
|
|
|
exit 0
|
|
|
|
|
fi
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sshadd # sshadd function
|
|
|
|
|
|
|
|
|
|
# Loop for endless mounts until stopped by unmount or unmountquestion
|
|
|
|
|
while :
|
|
|
|
|
do
|
|
|
|
|
mount # mount function
|
|
|
|
|
done
|