Browse Source

automatically recognize .ssh/config entries to shrink PRESSH-list; add better support for predefined hosts; remove unnecessary directories on unmount

master
mxmehl 7 years ago
parent
commit
b89ae2d245
  1. 11
      config.cfg.sample
  2. 58
      mnt-sftp.sh

11
config.cfg.sample

@ -2,10 +2,13 @@
LOCALMOUNTDIR=/home/user/remote
## mnt-sftp.sh
# Preconfigured HOSTs in ~/.ssh/config can be used
PRESSH[0]=server1
PRESSH[1]=user@server2.tld
PRESSH[2]=root@12.34.56.78
# Please insert predefined hosts in your SSH Config-file (default ~/.ssh/config). Please use keyfiles for passwordless authentication.
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/'
## mnt-share.sh

58
mnt-sftp.sh

@ -11,6 +11,20 @@ function sshadd {
ssh-add -l >/dev/null || ssh-add
}
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 }')
PRESSHLIST[$i]=${PREUSER[$i]}"@"${PREHOST[$i]}
done
# Read all hosts from ~/.ssh/config
SSHLIST=$(grep "Host\s" $SSHCONFIG | awk -F' ' '{ print $2 }')
# Choose preconfigured HOST to mount
function mount {
if ! SSH=$(zenity --list \
@ -18,7 +32,7 @@ function mount {
--text="Please choose. Cancel to unmount drives." \
--title="Choose SSH server" \
--column "Preconfigured SSH servers" \
"<manual input>" ${PRESSH[*]}); then
"<manual input>" $SSHLIST ${PRESSHLIST[*]}); then
unmountquestion # If you press cancel, it should ask you to unmount all drives
fi
@ -26,13 +40,17 @@ function mount {
# This command cuts of everything after |
SSH=$(echo $SSH | awk -F\| '{ print $1 }')
## Recognize if the input was entered manually, by .ssh/config or by the predefined SSH list in config.cfg
ISPRE=$(echo ${PRESSHLIST[@]} | grep -q "$SSH" ; echo $?)
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")
SSHPASS=$(zenity --entry --hide-text --text "SSH password" --title "Enter SSH password")
SSHPATH=$(zenity --entry --text "Desired root directory on server (default /)" --title "Enter SSH root directory")
if [ "$SSHPORT" == "" ]; then
SSHPORT=22
fi
@ -40,7 +58,25 @@ function mount {
SSHPATH="/"
fi
SSH="$SSHUSER-$RANDOM"
SSH="m-$SSHUSER-$RANDOM"
elif [ "$ISPRE" == "0" ]; then
MAN="1"
# Detect which predefined host was chosen and set variables accordingly
for ((i = 0; i < ${#PRESSHLIST[*]}; i++))
do
if [ "$(echo ${PRESSHLIST[$i]} | grep -q "$SSH" ; echo $?)" == "0" ]; then
SSHUSER=${PREUSER[$i]}
SSHHOST=${PREHOST[$i]}
SSHPORT=${PREPORT[$i]}
SSHPASS=${PREPASS[$i]}
SSHPATH=${PREPATH[$i]}
fi
done
SSH="p-$SSHUSER-$RANDOM"
else
MAN="0"
fi
# Make a local directory if not available
@ -49,10 +85,10 @@ function mount {
fi
# Command to mount actually
if [ "$MAN" != "1" ]; then
sshfs "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
if [ "$MAN" = "0" ]; then
sshfs -C "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
else
echo "$SSHPASS" | sshfs -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" "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
fi
quitquestion # one more ssh server or quit?
@ -71,11 +107,6 @@ function unmountquestion {
# Procedure to unmount all preconfigured SSHFS drives and exit program afterwards
function unmount {
#for ((i = 0; i < ${#PRESSH[*]}; i++))
#do
#fusermount -u "$LOCALMOUNTDIR"/"${PRESSH[$i]}"
#echo ""${PRESSH[$i]}" unmounted."
#done
for FILE in "$LOCALMOUNTDIR"/*;
do
@ -85,6 +116,9 @@ function unmount {
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
exit 0
}

Loading…
Cancel
Save