[[PageOutline(2-5, Table of Contents, floated)]]
= SSHFS Mount Script =
Following are a pair of scripts to mount and unmount files systems via SFTP using Fuse, they are used on [[Crin3]].
== mnt-sshfs ==
{{{
#!bash
#!/bin/bash
# Mount a filesystem using SSHFS
# http://fuse.sourceforge.net/sshfs.html
#
# Copyright 2015 Chris Croome
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
echo "You must run '$0' as root or via sudo"
exit 1
fi
# the place file systems are to be mounted
MOUNT_POINT="/media"
if [[ ! -d "$MOUNT_POINT" ]]; then
echo "It looks like $MOUNT_POINT doesn't exist?"
exit 1
fi
# check for server to mount on standard input
if [[ $1 ]]; then
SERVER=$1
elif [[ ! $1 ]]; then
echo "Type the server you want to mount and then [ENTER]:"
read server
SERVER=$server
fi
# check the server is setup in ~/.ssh/config
SSH_CONFIG=$(grep "Host $SERVER" ~/.ssh/config)
if [[ ! $SSH_CONFIG ]]; then
echo "It looks like you need to edit yur ~/.ssh/config"
echo "to add entries like this for each server:"
echo
echo "Host example"
echo " User root"
echo " Hostname example.org"
echo
exit 1
fi
# check if the mount point exists and if not create it
if [[ ! -d "$MOUNT_POINT/$SERVER" ]]; then
echo "Creating $MOUNT_POINT/$SERVER"
mkdir "$MOUNT_POINT/$SERVER" || { echo "Failed to create $MOUNT_POINT/$SERVER" ; exit 1 ; }
fi
# check is the file system appears to already be mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep "$MOUNT_POINT/$SERVER")
if [[ $DF ]]; then
echo "It appears that $SERVER is already mounted at $DF?"
exit 1
fi
# Mount the file system
sshfs $SERVER:/ $MOUNT_POINT/$SERVER/ && \
# if this succeeded
{ echo "Mounted $SERVER:/ on $MOUNT_POINT/$SERVER/" ; exit 0 ; } || \
# if this failed
{ echo "Problem mounting $SERVER:/ $MOUNT_POINT/$SERVER/" ; exit 1 ; }
echo "Oops how did we get here?"
exit 1
}}}
== umnt-sshfs ==
{{{
#!bash
#!/bin/bash
# Un-ount a filesystem using SSHFS
# http://fuse.sourceforge.net/sshfs.html
#
# Copyright 2015 Chris Croome
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# check that the script is being run by root
if [[ "$(id -u)" != "0" ]] ; then
echo "You must run '$0' as root or via sudo"
exit 1
fi
# the place file systems are to be mounted
MOUNT_POINT="/media"
if [[ ! -d "$MOUNT_POINT" ]]; then
echo "It looks like $MOUNT_POINT doesn't exist?"
exit 1
fi
# check for server to unmount on standard input
if [[ $1 ]]; then
SERVER=$1
elif [[ ! $1 ]]; then
echo "Type the server you want to unmount and then [ENTER]:"
read server
SERVER=$server
fi
# check if the mount point exists
if [[ ! -d "$MOUNT_POINT/$SERVER" ]]; then
echo "It looks like "$MOUNT_POINT/$SERVER" doesn't exist?"
exit 1
fi
# check if the file system appears to already be mounted
DF=$(df | awk -F' ' '{ print $6 }' | grep -w "$MOUNT_POINT/$SERVER")
if [[ $DF ]]; then
# unmount the filesystem
fusermount -u $MOUNT_POINT/$SERVER && \
# if success
{ echo "Umounted $SERVER at $DF" ; exit 0 ; } || \
# if mounting failed
{ echo "Problem unmounting $MOUNT_POINT/$SERVER" ; exit 1 ; }
else
echo "It appears that $SERVER is not mounted at $MOUNT_POINT/$SERVER"
exit 1
fi
echo "Oops how did we get here?"
exit 1
}}}