Skip to content

Instantly share code, notes, and snippets.

@joeperpetua
Created February 22, 2023 12:28
Show Gist options
  • Select an option

  • Save joeperpetua/f05c152894d8c9822277110ef2e055c1 to your computer and use it in GitHub Desktop.

Select an option

Save joeperpetua/f05c152894d8c9822277110ef2e055c1 to your computer and use it in GitHub Desktop.
Script to mount/unmount Synology encrypted shared folders via CLI using synoshare built-in tool.
#!/usr/bin/sh
HELP='false'
while getopts ":hm:s:" flag
do
case "${flag}" in
h) HELP='true';;
m) METHOD=${OPTARG};;
s) SHARED_FOLDER=${OPTARG};;
\?) echo "Invalid option: -$OPTARG" >&2; exit 1;;
:) echo "Option -$OPTARG requires an argument."; exit 1;;
esac
done
if ${HELP} || [ "$#" == 0 ]; then
echo -e "\nThis script helps you to mount and unmount Synology NAS encrypted shared folders in a secure way using the built-in synoshare tool in DSM.\n";
echo -e "\nUsage:";
echo -e " sh ./synomount.sh [-h] -m METHOD -s SHARED_FOLDER"
echo -e "\nOptions:";
echo -e " -h Shows this message";
echo -e " -m METHOD Method to execute { unmount | mount }";
echo -e " -s SHARED_FOLDER Shared folder name to mount/unmount";
echo -e "\nExample:";
echo " sh ./synomount.sh -m mount -s test_folder";
echo " sh ./synomount.sh -m unmount -s test_folder";
exit 1;
fi
: ${METHOD:?Missing method, please specify the -m argument.};
: ${SHARED_FOLDER:?Missing shared folder, please specify the -s argument.};
if [ ${METHOD} = "unmount" ]; then
echo "Unmounting ${SHARED_FOLDER}...";
synoshare --enc_unmount $SHARED_FOLDER;
echo "Process done.";
elif [ ${METHOD} = "mount" ]; then
echo -n "Enter encryption key:";
read -s ENC_KEY;
echo -e "\nMounting ${SHARED_FOLDER}...";
/usr/syno/sbin/synoshare --enc_mount $SHARED_FOLDER $ENC_KEY
echo "Process done.";
else
echo -e "No valid method was provided.\nAccepted methods are 'mount' or 'unmount'.";
fi
@evandorkins
Copy link

Forgive the noob question here, but is there a way to enhance this script so it doesn't prompt user for key info, rather it would be able to get that info from a file somewhere on the file system? Goal for me is to set this script to run on certain scheduled times or when a specific user logs in.

@joeperpetua
Copy link
Author

Forgive the noob question here, but is there a way to enhance this script so it doesn't prompt user for key info, rather it would be able to get that info from a file somewhere on the file system? Goal for me is to set this script to run on certain scheduled times or when a specific user logs in.

If your objective is to run this automatically, then you can simply use the native DSM command to mount the shared folder and add it to a cron task or scheduled task in DSM:
synoshare --enc_mount example_shared_folder example_password

The downside is that this will leave the encryption key in plain text inthe /var/log/bash_history.log file, but as you want to leave the encryption key in a file anyways, it should not be too different. Not secure but that is on you.

This gist was thought to not leave any trace of the encryption key in the file system, so what you are asking can certainly be done, but would be opposite of the whole idea behind this script.

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