Skip to content

Instantly share code, notes, and snippets.

@Klafyvel
Created October 24, 2019 18:54
Show Gist options
  • Save Klafyvel/b3783e0d3b137b883912027d534ad650 to your computer and use it in GitHub Desktop.
Save Klafyvel/b3783e0d3b137b883912027d534ad650 to your computer and use it in GitHub Desktop.
#! /bin/bash
SSH_PROXY=ssh.proxy.tld
SSH_USER=user
LOCAL_PORT=6666
FTP_USER=ftpuser
FTP_PASS=password123
FTP=0.0.0.0
MOUNT_PATH=~/ftp
OWNER=user
unmount_ftp() {
echo "Unmounting"
umount $MOUNT_PATH 2&> /dev/null
echo "Killing previous port forwarding."
awk_command="/$LOCAL_PORT/ {split(\$6,a,\",\"); split(a[2],b,\"=\"); print b[2]}"
kill -n 9 `ss -nltp | awk "$awk_command" | head -n 1`
}
mount_ftp() {
unmount_ftp
echo "Setting up dynamic port forwarding."
ssh -D $LOCAL_PORT -fN $SSH_USER@$SSH_PROXY
echo "Creating mount folder."
mkdir -p $MOUNT_PATH
echo "Mounting ftp system."
owner_id=`id -u $OWNER`
owner_group=`id -g $OWNER`
curlftpfs -o proxy=localhost:$LOCAL_PORT,socks5,user=$FTP_USER:$FTP_PASS,uid=$owner_id,gid=$owner_group $FTP $MOUNT_PATH && echo "FTP mounted at $MOUNT_PATH."
}
main_function() {
subcmd="$1"
case "$subcmd" in
mount )
mount_ftp
;;
unmount )
unmount_ftp
;;
* )
echo "Unknown subcommand: $subcmd"
echo "Usage : 'ftpfs.sh mount' or 'ftpfs.sh unmount'"
;;
esac
}
main_function "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment