Skip to content

Instantly share code, notes, and snippets.

@tarao
Last active October 13, 2015 08:27
Show Gist options
  • Save tarao/4167564 to your computer and use it in GitHub Desktop.
Save tarao/4167564 to your computer and use it in GitHub Desktop.
sshfs wrapper
#!/bin/sh -e
sshmount_url='https://raw.github.com/gist/4167564/sshmount.sh'
sshumount_url='https://raw.github.com/gist/4167564/sshumount.sh'
sshmount='sshmount'
sshumount='sshumount'
bin="$HOME/bin"
verbose=0
error() {
echo "$1" >&2
exit 1
}
info() {
[ $verbose != 0 ] && echo $1 >&2
return 0
}
[ "x$1" = 'x-v' ] && {
verbose=1
shift
}
help() {
cat <<EOF
Usage: $(basename "$0") [-v] [user@]host[:dir] [target]
EOF
exit
}
[ -z "$1" ] && help
[ "x$1" = 'x-h' ] || [ "x$1" = 'x--help' ] && help
type 'sshmount' >/dev/null 2>&1 || [ -x "$bin/sshmount" ] || {
info "Download \"$sshmount_url\" to \"$bin/sshmount\""
mkdir -p "$bin"
wget -O "$bin/sshmount" "$sshmount_url"
chmod a+x "$bin/sshmount"
}
[ -x "$bin/sshmount" ] && sshmount="$bin/sshmount"
[ -d "$HOME/.config/autostart" ] || mkdir -p "$HOME/.config/autostart"
info "Register automount host $1"
cat > "$HOME/.config/autostart/automount-$1.desktop" <<EOF
[Desktop Entry]
Type=Application
Terminal=false
Hidden=false
X-GNOME-Autostart-enabled=true
Exec=$sshmount "$1" "$2"
Name=sshfs $1
Comment=Automount $1 (via ssh)
EOF
type 'sshumount' >/dev/null 2>&1 || [ -x "$bin/sshumount" ] || {
info "Download \"$sshumount_url\" to \"$bin/sshumount\""
mkdir -p "$bin"
wget -O "$bin/sshumount" "$sshumount_url"
chmod a+x "$bin/sshumount"
}
[ -x "$bin/sshumount" ] && sshumount="$bin/sshumount"
code="[ -x \"$sshumount\" ] && STARTUP=\"$sshumount - \$STARTUP\""
[ -f "$HOME/.gnomerc" ] && grep "^\\$code\$" "$HOME/.gnomerc" >/dev/null || {
info "Install auto-umount to .gnomerc"
touch "$HOME/.gnomerc"
echo "$code" >> "$HOME/.gnomerc"
}
#!/bin/sh -e
timeout=5
options=${SSHMOUNT_OPTIONS:-auto_cache,reconnect,transform_symlinks,follow_symlinks}
help() {
cat <<EOF
Usage: $(basename "$0") [-u] [user@]host[:dir] [target] [args...]
EOF
exit
}
[ "x$1" = 'x-u' ] && { umount=1; shift; } || umount=0
[ -z "$1" ] && help
user=$(echo "$1" | cut -s -d @ -f 1)
[ -n "$user" ] && user="$user@"
rest=$(echo "$1" | cut -s -d @ -f 2)
[ -z "$rest" ] && rest="$1"
host=$(echo "$rest" | cut -s -d : -f 1)
dir=$(echo "$rest" | cut -s -d : -f 2)
[ -z "$host" ] && host="$rest"
[ -n "$dir" ] && base=$(basename "$dir")
[ -z "$dir" ] && dir='.'
shift
target="$1"
[ $# -gt 0 ] && shift
[ -z "$target" ] && {
target="$HOME/mnt/$host"
[ -n "$base" ] && target="$target-$base"
}
[ $umount = 1 ] && {
fusermount -u "$target"
exit
}
ssh -o "ConnectTimeout=$timeout" "$user$host" exit && {
mkdir -p "$target"
sshfs "$user$host:$dir" "$target" "-o$options" "$@"
}
#!/bin/sh
all=0
lf='
'
help() {
cat <<EOF
Usage: $(basename "$0") [-|host] [command...]
Arguments:
- Unmount all sshfs mount points.
host Unmount all sshfs mount point of the host.
command Actual unmount operation will be done after running this command.
EOF
exit
}
[ -z "$1" ] && help
[ "x$1" = 'x-' ] && all=1
host="$1"
shift
check_user() {
idnum=$(id -ru)
for x in $1; do
[ "x$x" = "xuser_id=$idnum" ] && return 0
done
return 1
}
parse_line() {
dev=$(echo "$1" | cut -f 1 -d ' ')
type=$(echo "$1" | cut -f 3 -d ' ')
target=$(echo "$1" | cut -f 2 -d ' ')
opt=$(echo "$1" | cut -f 4 -d ' ')
[ "x$type" = 'xfuse.sshfs' ] || return
IFS=',' check_user "$opt" || return
[ $all = 0 ] && [ -n "$host" ] && {
dev=$(echo "$dev" | cut -f 1 -d :)
[ "x$dev" = "x$host" ] || return
}
fusermount -u "$target"
}
parse_lines() {
for l in $@; do
parse_line "$l"
done
}
[ $# -gt 0 ] && "$@" # run commands
IFS="$lf" parse_lines "$(cat /etc/mtab)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment