Last active
July 30, 2022 14:31
-
-
Save ericswpark/8c48774cdd691bd37d4881629bd16bf0 to your computer and use it in GitHub Desktop.
Script to easily mount SMB shares on Linux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# You must have cifs-tools installed | |
set -e | |
# Set variables here | |
SERVER_HOSTNAME="serverhostnamehere" | |
USERNAME="usernamehere" | |
# Check permissions | |
# From: https://stackoverflow.com/a/42876846 | |
if [[ "$EUID" = 0 ]]; then | |
# Do nothing, we are already root | |
: | |
else | |
echo "This script needs to run with elevated permissions." | |
sudo -k # make sure to ask for password on next sudo | |
if sudo true; then | |
# Authenticated successfully | |
: | |
else | |
echo "Failed to gain elevated permissions." | |
exit 1 | |
fi | |
fi | |
read -p "Enter shares you want to mount separated by spaces: " TARGET_SHARES | |
for share in ${TARGET_SHARES[@]} | |
do | |
echo "Mounting share: $share..." | |
# Make sure directory exists | |
if [ ! -d /mnt/$SERVER_HOSTNAME/$share ]; then | |
echo "$share: mount target directory does not exist; creating..." | |
sudo mkdir -p /mnt/$SERVER_HOSTNAME/$share | |
echo "$share: mount target directory created." | |
fi | |
# Mount | |
echo "$share: mounting..." | |
sudo mount -t cifs -o rw,uid=$(id -u $(whoami)),gid=$(id -g $(whoami)),username=$USERNAME //$SERVER_HOSTNAME/$share /mnt/$SERVER_HOSTNAME/$share | |
echo "$share: mounted share successfully." | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment