Skip to content

Instantly share code, notes, and snippets.

@derrekbertrand
Last active August 29, 2015 14:16
Show Gist options
  • Save derrekbertrand/8911d178ebe23b15670c to your computer and use it in GitHub Desktop.
Save derrekbertrand/8911d178ebe23b15670c to your computer and use it in GitHub Desktop.
Mount SSH folder, and make damn sure it stays mounted.
#!/bin/bash
#should match 99.9% of SSH users
user_regex='[a-zA-Z][a-zA-Z0-9_]+'
#should match 90% of domains
host_regex='([a-zA-Z][a-zA-Z0-9\-]*\.)*[a-zA-Z][a-zA-Z0-9\-]*'
#should match paths starting with / and '' (which is valid for our use)
path_regex='(\/[A-Za-z0-9_\-\.]+)*\/?'
master_regex="^$user_regex\@$host_regex\:$path_regex\$"
#we need an argument
if [ -z "$1" ]; then
zenity --warning --text="Command requires a connection string."
else
#must match a connection string
if [[ $1 =~ $master_regex ]]; then
#get the host portion
host_str=`echo $1 | egrep -o \@$host_regex\: | egrep -o $host_regex`
#make sure we have a directory named after the host
if [ ! -d "${HOME}/Share/${host_str}" ]; then
mkdir -p "${HOME}/Share/${host_str}"
fi
#poke it every minute to prevent the connection from being dropped due to inactivity
while true
do
err=$(mount | grep "$1")
if [[ $err == "" ]]; then
#was not found; remount
err=$(sshfs $1 "${HOME}/Share/${host_str}")
#there's something wrong with what we're trying to mount
#offer a warning, and then stop trying to connect
if [[ $err != "" ]]; then
zenity --warning --text="$err"
exit
fi
fi
#just chill, til the next episode
sleep 60
done
else
zenity --warning --text="$1 is not a valid SSH connection string."
fi
fi
@derrekbertrand
Copy link
Author

I add this to my "Startup Applications" in Mint.

/full/path/to/ssh_mount.sh [email protected]:

It creates a folder called ~/Share/host.com and ensures that it stays mounted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment