A collection of scripts and configurations for Bash (and others)
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

140 lines
2.7KB

  1. #!/bin/bash
  2. # Script heavily modified from...
  3. # https://github.com/thefekete/sshfs-mount/blob/master/sshfs-mount
  4. # These are all default values
  5. REMOTE_IN_SSHCONFIG=-1
  6. REMOTE_USER="obsidianblk"
  7. REMOTE_HOST="Trappist1"
  8. REMOTE_PATH="/home/obsidianblk"
  9. REMOTE_PORT=22
  10. MOUNT_POINT="$HOME/remote"
  11. PORT_VAR=""
  12. NEXT_ARG_IS_FILE=-1
  13. OP=""
  14. SRC_FILE=""
  15. URI=""
  16. function usage() {
  17. echo "Usage: $0 [-o <path to mount options>] mount|unmount|remount"
  18. }
  19. function defineURI() {
  20. URI="$REMOTE_HOST:$REMOTE_PATH"
  21. if [ $REMOTE_IN_SSHCONFIG -lt 0 ]; then
  22. URI="$REMOTE_USER@$URI"
  23. PORT_VAR="-p $REMOTE_PORT"
  24. fi
  25. }
  26. if [[ "$#" -lt 1 ]]; then
  27. echo "Missing operation argument."
  28. usage
  29. exit 1
  30. fi
  31. for arg in "$@"; do
  32. if [ $NEXT_ARG_IS_FILE -ge 0 ]; then
  33. if [ -f "$arg" ]; then
  34. SRC_FILE="$arg"
  35. source "$arg"
  36. NEXT_ARG_IS_FILE=-1
  37. else
  38. echo "ERROR: Option file $arg missing or invalid."
  39. usage
  40. exit 1
  41. fi
  42. else
  43. case "$arg" in
  44. mount) ;&
  45. unmount) ;&
  46. remount)
  47. if [ "$OP" != "" ]; then
  48. echo "ERROR: Multiple operations defined."
  49. usage
  50. exit 1
  51. fi
  52. OP="$arg"
  53. ;;
  54. -o)
  55. NEXT_ARG_IS_FILE=0
  56. ;;
  57. esac
  58. fi
  59. done
  60. if ! [[ -d "$MOUNT_POINT" ]]; then
  61. echo "ERROR: [$MOUNT_POINT] does not exist or is not a directory."
  62. usage
  63. exit 1
  64. fi
  65. case "$OP" in
  66. mount)
  67. defineURI
  68. if ! mount | grep "$URI on $MOUNT_POINT" > /dev/null
  69. then
  70. echo "Mounting $URI on $MOUNT_POINT... "
  71. sshfs -C $PORT_VAR -o reconnect,ServerAliveInterval=15,ServerAliveCountMax=3 $URI $MOUNT_POINT &&
  72. ( # Success
  73. echo "Mounted."
  74. notify-send -i server 'Successfully Mounted' "$URI" &> /dev/null
  75. exit 0
  76. ) ||
  77. ( # Failure
  78. echo "Mount Failed!"
  79. notify-send -i error 'Failed to mount' "$URI" &> /dev/null
  80. exit 1
  81. )
  82. else
  83. echo "Remote folder is already mounted."
  84. exit 1
  85. fi
  86. ;;
  87. unmount)
  88. defineURI
  89. echo "$URI"
  90. if mount | grep "$URI on $MOUNT_POINT" > /dev/null
  91. then
  92. echo "Unmounting $URI from $MOUNT_POINT... "
  93. fusermount -u $MOUNT_POINT &&
  94. ( # Success
  95. echo "Unmount Successful."
  96. notify-send -i server 'Successfully Unmounted' "$URI" &> /dev/null
  97. exit 0
  98. ) ||
  99. ( # Failure
  100. echo "FAILED to Unmount!"
  101. notify-send error 'Failed to unmount' "$URI" &> /dev/null
  102. exit 1
  103. )
  104. else
  105. echo "Remote folder not mounted."
  106. exit 1
  107. fi
  108. ;;
  109. remount)
  110. $SRC_ARG=""
  111. if $SRC_FILE != ""; then
  112. SRC_ARG="-o $SRC_FILE"
  113. fi
  114. $0 unmount $SRC_ARG $OP &&
  115. $0 mount $SRC_SRG $OP
  116. ;;
  117. *)
  118. usage
  119. exit
  120. ;;
  121. esac