Skip to content

Instantly share code, notes, and snippets.

@manix84
Last active October 11, 2015 03:57
Show Gist options
  • Save manix84/3799047 to your computer and use it in GitHub Desktop.
Save manix84/3799047 to your computer and use it in GitHub Desktop.
Mount SSH FS. A very quick way to mount an sshfs drive, as I was finding it a pain.
# Usage: mount_ssh [your_username@]some.server.com:/path/to/mount "[Your Server Name]"
function mount_ssh() {
# Ensure SSHFS is installed before trying to run any commands.
if ! type sshfs >/dev/null 2>&1; then
echo "SSHFS is required to run this command."
echo "If you have Homebrew installed, please run \"brew install sshfs\""
return
fi
local connectionPattern="((.*)@)?(.*)(:(.*))?"
local remoteHost
local friendlyName
if [[ -n "${1}" && ${1} =~ ${connectionPattern} ]]; then
remoteHost=${BASH_REMATCH[3]}
friendlyName=`expr "${remoteHost}" : '\([0-9A-Za-z_-]\{1,30\}\)'`
else
echo "A host name is required"
return
fi
# Auto requesting sudo access as it is required for the creation and alteration of the Network folder.
sudo -v
local directoryName=/Network/${friendlyName}
local volumeName="${friendlyName} (SSHFS)"
if [[ -n "${2}" ]]; then
local volumeName=${2}
fi
if [ -d ${directoryName} ]; then
read -p "\"${directoryName}\" already exists. Would you like to remount it? [y/N]: " yn
case $yn in
[Yy]* )
cd
sudo umount -f ${directoryName};;
* )
echo "Exiting...";
return;;
esac
fi
# An ugly chain of dependant commands. If the command previous to each fails, something has gone
# wrong and we need to stop.
sudo mkdir -p ${directoryName} &&
sudo chown ${USER} ${directoryName} &&
sudo chmod 0755 ${directoryName} &&
sshfs ${1} ${directoryName} -o transform_symlinks,compression=yes,volname="${volumeName}" &&
cd ${directoryName} &&
echo "${remoteHost} mounted to ${directoryName}, named \"${volumeName}\"" && return
echo "${remoteHost} failed to mount."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment