feat: refactored sshfs function. Now accepts -p, -o, -F, -u -n. by default will mount with the current user. use -u for different user, and -n for no user. -F for alternate ssh config file, -- for alternate port (22 is default) -o for additional sshfs options.

master
Kebler Network System Administrator 2022-01-22 14:27:47 -08:00
parent 7181f1ee69
commit 67417a609a
1 changed files with 56 additions and 10 deletions

View File

@ -8,23 +8,69 @@ function smount() {
local HOST local HOST
local PORT local PORT
local config local CONFIG=$SSH_CONFIG
local PORT=22
declare SSHOPTS
declare OPTION
declare OPTARG
declare OPTIND
declare MNTUSER
while getopts 'u:np:o:F:' OPTION; do
# echo $OPTION $OPTARG
case "$OPTION" in
p)
PORT=$OPTARG
# echo option d: $DIRS
;;
u)
MNTUSER=$OPTARG
;;
n)
MNTUSER=_NONE_
;;
o)
echo "adding sshfs option: $OPTARG"
SSHOPTS="$SSHOPTS -o $OPTARG"
;;
F)
echo "using SSH Config file at: $OPTARG"
CONFIG=$OPTARG
;;
*)
echo unknown option $OPTION
;;
esac
done
shift $((OPTIND - 1))
# first item is nowsource, second is local mount point, third is possbile local user
HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1") HOST=$(sed 's/.*@\(.*\):.*/\1/' <<<"$1")
# TODO sed search for -p and extract port, below assumes 3 and 4 position
PORT=$([[ $3 = "-p" ]] && echo $4 || echo 22)
[[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1 [[ ! $(host_reachable $HOST $PORT) ]] && echo host $HOST not reachable, aborting mount && return 1
if [[ $(mounted $2) ]]; then if [[ $(mounted $2) ]]; then
echo "aborting mount: $1 already mounted at $2" echo "aborting mount: $1 already mounted at $2"
else else
echo "SSHFS: mounting $1 at $2"
mkdir -p $2 mkdir -p $2
# can add any options after mount point directory like -o default_permissions # can add any options after mount point directory like -o default_permissions
if [[ $SSH_CONFIG ]]; then config=$([[ -f $CONFIG ]] && echo "-F $CONFIG")
[[ ! -f "$SSH_CONFIG" ]] && ssh_config "$SSH_CONFIG" if [[ ! $MNTUSER == "_NONE_" ]]; then
config=$([[ -f $SSH_CONFIG ]] && echo "-F $SSH_CONFIG") MNTUSER=${MNTUSER:-$USER}
fi id=$(id -u ${MNTUSER})
# echo sshfs "$*" "$config" if [[ $id ]]; then
sshfs $* $config SSHOPTS="$SSHOPTS -o uid=$id -o allow_other"
else
echo no user ${MNTUSER} on this machine, aborting mount
return 1
fi
else
MNTUSER=""
fi
args="-p $PORT $SSHOPTS $config $1 $2"
echo SSHFS $([[ $MNTUSER ]] && echo mounted as user ${MNTUSER}): $args
sshfs $args
fi fi
} }