Browse Source

Added a folder for some useful bash scripts

master
Bryan Miller 4 years ago
parent
commit
b68e73eb52
No known key found for this signature in database
1 changed files with 136 additions and 0 deletions
  1. +136
    -0
      scripts/sshmnt

+ 136
- 0
scripts/sshmnt View File

@@ -0,0 +1,136 @@
#!/bin/bash

# These are all default values
REMOTE_IN_SSHCONFIG=-1
REMOTE_USER="obsidianblk"
REMOTE_HOST="Trappist1"
REMOTE_PATH="/home/obsidianblk"
REMOTE_PORT=22
MOUNT_POINT="$HOME/remote"

PORT_VAR=""

NEXT_ARG_IS_FILE=-1
OP=""
SRC_FILE=""

URI=""

function usage() {
echo "Usage: $0 [-o <path to mount options>] mount|unmount|remount"
}

function defineURI() {
URI="$REMOTE_HOST:$REMOTE_PATH"
if [ $REMOTE_IN_SSHCONFIG -lt 0 ]; then
URI="$REMOTE_USER@$URI"
PORT_VAR="-p $REMOTE_PORT"
fi
}

if [[ "$#" -lt 1 ]]; then
echo "Missing operation argument."
usage
exit 1
fi

for arg in "$@"; do
if [ $NEXT_ARG_IS_FILE -ge 0 ]; then
if [ -f "$arg" ]; then
SRC_FILE="$arg"
source "$arg"
NEXT_ARG_IS_FILE=-1
else
echo "ERROR: Option file $arg missing or invalid."
usage
exit 1
fi
else
case "$arg" in
mount) ;&
unmount) ;&
remount)
if [ "$OP" != "" ]; then
echo "ERROR: Multiple operations defined."
usage
exit 1
fi
OP="$arg"
;;
-o)
NEXT_ARG_IS_FILE=0
;;
esac
fi
done


if ! [[ -d "$MOUNT_POINT" ]]; then
echo "ERROR: [$MOUNT_POINT] does not exist or is not a directory."
usage
exit 1
fi

case "$OP" in
mount)
defineURI
if ! mount | grep "$URI on $MOUNT_POINT" > /dev/null
then
echo "Mounting $URI on $MOUNT_POINT... "
sshfs -C $PORT_VAR -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 $URI $MOUNT_POINT &&
( # Success
echo "Mounted."
notify-send -i server 'Successfully Mounted' "$URI" &> /dev/null
exit 0
) ||
( # Failure
echo "Mount Failed!"
notify-send -i error 'Failed to mount' "$URI" &> /dev/null
exit 1
)
else
echo "Remote folder is already mounted."
exit 1
fi
;;

unmount)
defineURI
echo "$URI"
if mount | grep "$URI on $MOUNT_POINT" > /dev/null
then
echo "Unmounting $URI from $MOUNT_POINT... "
fusermount -u $MOUNT_POINT &&
( # Success
echo "Unmount Successful."
notify-send -i server 'Successfully Unmounted' "$URI" &> /dev/null
exit 0
) ||
( # Failure
echo "FAILED to Unmount!"
notify-send error 'Failed to unmount' "$URI" &> /dev/null
exit 1
)
else
echo "Remote folder not mounted."
exit 1
fi
;;

remount)
$SRC_ARG=""
if $SRC_FILE != ""; then
SRC_ARG="-o $SRC_FILE"
fi
$0 unmount $SRC_ARG $OP &&
$0 mount $SRC_SRG $OP
;;

*)
usage
exit
;;
esac




Loading…
Cancel
Save