transferring several commands from private repo

This commit is contained in:
Ville Rantanen
2021-10-08 11:19:58 +03:00
parent 754938ca32
commit 707eba3ead
20 changed files with 1140 additions and 289 deletions

107
web/sshfs-mount Executable file
View File

@@ -0,0 +1,107 @@
#!/bin/bash
_helpexit() {
printf "Usage: %s [-u] [-nosave] host [path]
host can be from ssh config
path is the remote path. Defaults to remote home.
-u will unmount all FUSE.sshfs paths if no path given!
-nosave will not use host key checking, nor save it anywhere. potentially dangerous.
" "$( basename $0 )"
echo Current mounts:
cat /proc/mounts | grep fuse.sshfs | awk '{ print $1 "\t" $2 }'
exit 1
}
_menu() {
echo Current mounts:
cat /proc/mounts | grep fuse.sshfs | awk '{ print $1 "\t" $2 }'
choice=$( grep -Pi "^host ([^*]+)$" $HOME/.ssh/config | \
sed 's/host //i' | tr ' ' '\n' | sort | \
smenu -t 1 -a c:0/2 i:3 -n 25 -m "Select server" \
-N -D n:1 i:1 )
echo $choice
if [[ -z "$choice" ]]; then
_helpexit
fi
host="$choice"
}
[[ -z "$1" ]] && _menu
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-h" ]] && _helpexit
[[ ${!i} = "--help" ]] && _helpexit
done
unmount=false
nosave=false
for (( i=1; i<=$#; i++ )); do
[[ ${!i} = "-u" ]] && { unmount=true; continue; }
[[ ${!i} = "-nosave" ]] && { nosave=true; continue; }
if [[ -z "$host" ]]; then
host="${!i}"
else
localpath="${!i}"
fi
done
mkdir -p ~/mnt
valid_name=$( echo "$host" | sed -e 's/[^A-Za-z0-9._@-]//g')
path=:"${localpath}"
if [[ "$nosave" = true ]]; then
NOSAVE="-o ssh_command='ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'"
fi
if [[ "$unmount" = true ]]; then
cd ~/mnt
if [[ -n "$host" ]]; then
fusermount -u -z ~/mnt/$valid_name
rmdir --ignore-fail-on-non-empty ~/mnt/$valid_name &>/dev/null
else
# no path, unmount all
cat /proc/mounts | grep fuse.sshfs | awk '{ print $2 }' | while read dir; do
echo Unmounting $dir
fusermount -u -z "$dir"
rmdir --ignore-fail-on-non-empty "$dir" &>/dev/null
done
fi
exit
fi
if [[ -z "$host" ]]; then
echo "No host given"
_helpexit
fi
if [[ -d "$valid_name" ]]; then
device1=$( stat -c "%d" "$valid_name" )
device2=$( stat -c "%d" . )
else
device1=valid
device2=valid
fi
if [[ $device1 = $device2 ]]; then
echo "Mounting $valid_name$path in ~/mnt/$valid_name"
cd ~/mnt
mkdir -p "$valid_name"
eval sshfs \
-o reconnect \
-o ServerAliveInterval=45,ServerAliveCountMax=2 \
$NOSAVE \
-o follow_symlinks "$valid_name$path" "$valid_name"
if [[ $? -gt 0 ]]; then
rmdir "$valid_name"
else
df -h "$valid_name"
fi
else
echo "~/mnt/$valid_name is already mounted"
fi