automatically recognize .ssh/config entries to shrink PRESSH-list; add better support for predefined hosts; remove unnecessary directories on unmount
This commit is contained in:
@@ -2,10 +2,13 @@
|
|||||||
LOCALMOUNTDIR=/home/user/remote
|
LOCALMOUNTDIR=/home/user/remote
|
||||||
|
|
||||||
## mnt-sftp.sh
|
## mnt-sftp.sh
|
||||||
# Preconfigured HOSTs in ~/.ssh/config can be used
|
# Please insert predefined hosts in your SSH Config-file (default ~/.ssh/config). Please use keyfiles for passwordless authentication.
|
||||||
PRESSH[0]=server1
|
SSHCONFIG=$HOME/.ssh/config
|
||||||
PRESSH[1]=user@server2.tld
|
|
||||||
PRESSH[2]=root@12.34.56.78
|
# 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
|
## mnt-share.sh
|
||||||
|
|||||||
58
mnt-sftp.sh
58
mnt-sftp.sh
@@ -11,6 +11,20 @@ function sshadd {
|
|||||||
ssh-add -l >/dev/null || ssh-add
|
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
|
# Choose preconfigured HOST to mount
|
||||||
function mount {
|
function mount {
|
||||||
if ! SSH=$(zenity --list \
|
if ! SSH=$(zenity --list \
|
||||||
@@ -18,7 +32,7 @@ function mount {
|
|||||||
--text="Please choose. Cancel to unmount drives." \
|
--text="Please choose. Cancel to unmount drives." \
|
||||||
--title="Choose SSH server" \
|
--title="Choose SSH server" \
|
||||||
--column "Preconfigured SSH servers" \
|
--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
|
unmountquestion # If you press cancel, it should ask you to unmount all drives
|
||||||
fi
|
fi
|
||||||
|
|
||||||
@@ -26,13 +40,17 @@ function mount {
|
|||||||
# This command cuts of everything after |
|
# This command cuts of everything after |
|
||||||
SSH=$(echo $SSH | awk -F\| '{ print $1 }')
|
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
|
if [ "$SSH" == "<manual input>" ]; then
|
||||||
MAN="1"
|
MAN="1"
|
||||||
SSHUSER=$(zenity --entry --text "SSH user" --title "Enter SSH user")
|
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")
|
SSHHOST=$(zenity --entry --text "SSH host" --title "Enter SSH host")
|
||||||
SSHPORT=$(zenity --entry --text "SSH port (default 22)" --title "Enter SSH port")
|
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
|
if [ "$SSHPORT" == "" ]; then
|
||||||
SSHPORT=22
|
SSHPORT=22
|
||||||
fi
|
fi
|
||||||
@@ -40,7 +58,25 @@ function mount {
|
|||||||
SSHPATH="/"
|
SSHPATH="/"
|
||||||
fi
|
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
|
fi
|
||||||
|
|
||||||
# Make a local directory if not available
|
# Make a local directory if not available
|
||||||
@@ -49,10 +85,10 @@ function mount {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
# Command to mount actually
|
# Command to mount actually
|
||||||
if [ "$MAN" != "1" ]; then
|
if [ "$MAN" = "0" ]; then
|
||||||
sshfs "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
|
sshfs -C "$SSH": "$LOCALMOUNTDIR"/"$SSH"/ -o follow_symlinks &
|
||||||
else
|
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
|
fi
|
||||||
|
|
||||||
quitquestion # one more ssh server or quit?
|
quitquestion # one more ssh server or quit?
|
||||||
@@ -71,11 +107,6 @@ function unmountquestion {
|
|||||||
|
|
||||||
# Procedure to unmount all preconfigured SSHFS drives and exit program afterwards
|
# Procedure to unmount all preconfigured SSHFS drives and exit program afterwards
|
||||||
function unmount {
|
function unmount {
|
||||||
#for ((i = 0; i < ${#PRESSH[*]}; i++))
|
|
||||||
#do
|
|
||||||
#fusermount -u "$LOCALMOUNTDIR"/"${PRESSH[$i]}"
|
|
||||||
#echo ""${PRESSH[$i]}" unmounted."
|
|
||||||
#done
|
|
||||||
|
|
||||||
for FILE in "$LOCALMOUNTDIR"/*;
|
for FILE in "$LOCALMOUNTDIR"/*;
|
||||||
do
|
do
|
||||||
@@ -85,6 +116,9 @@ function unmount {
|
|||||||
fi
|
fi
|
||||||
done
|
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
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user