This is a bash script I deploy from Jamf to mount DFS shares
#!/bin/sh
# Get current logged in user
curUser="$(stat -f"%Su" /dev/console)"
# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
delay 5
tell application "Finder"
mount volume "smb://PATH_TO_SHARE"
end tell
EOT`
echo "Opening Network shared drive..."
# open network drive in Finder
sudo -u $curUser open /Volumes/PATH_TO_SHARE/
exit 0
If your file share requires authentication, then the user will get prompted to login
This is a bash script I deploy from Jamf to mount DFS shares
#!/bin/sh
# Get current logged in user
curUser="$(stat -f"%Su" /dev/console)"
# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
delay 5
tell application "Finder"
mount volume "smb://PATH_TO_SHARE"
end tell
EOT`
echo "Opening Network shared drive..."
# open network drive in Finder
sudo -u $curUser open /Volumes/PATH_TO_SHARE/
exit 0
If your file share requires authentication, then the user will get prompted to login
Thanks @junjishimazaki,
Like a mention before, I'm not so good at this with bash.
I've tried to edit the script you shared a bit to fit our needs but I don't think I get it really right. This is what I'm trying to accomplish.
#!/bin/sh
# Get current logged in user
curUser="$(stat -f"%Su" /dev/console)"
# Mount the drive
mount_script=`/usr/bin/osascript > /dev/null << EOT
delay 5
set userName to short user name of (system info)
set sharePath to "smb://" & userName & "our_server_address/home$/" & userName
tell application "Finder"
mount volume sharePath
end tell
EOT`
echo "Opening Network shared drive..."
# open network drive in Finder
sudo -u $curUser open /Volumes/$curUser/
exit 0
Maybe it's not necessary to declare variable for both bash and apple script? :)
Hey Jonas,
You’re right—using a Bash script for this is a cleaner approach, especially when deploying via Jamf. Below is a Bash script that replicates your AppleScript functionality. It dynamically retrieves the currently logged-in user and mounts the SMB share accordingly:
#!/bin/bash
# Get the currently logged-in user
currentUser=$(stat -f%Su /dev/console)
# Define the SMB share path
serverAddress="our_server_address"
sharePath="smb://$serverAddress/homes/$currentUser"
# Check if the share is already mounted
if mount | grep "$sharePath" > /dev/null; then
echo "Share already mounted."
exit 0
fi
# Create mount point if it doesn't exist
mountPoint="/Volumes/$currentUser"
if
! -d "$mountPoint" ]; then
mkdir -p "$mountPoint"
fi
# Mount the SMB share
osascript -e "try" -e "mount volume \\"$sharePath\\"" -e "end try"
# Verify if the mount was successful
if mount | grep "$serverAddress"; then
echo "Successfully mounted $sharePath"
else
echo "Failed to mount $sharePath"
exit 1
fi
Explanation:
- •Retrieves the currently logged-in user with stat -f%Su /dev/console
- •Constructs the SMB path dynamically
- •Checks if the share is already mounted before proceeding
- •Creates a mount point if it doesn’t exist
- •Uses osascript to mount the volume via Finder, avoiding password prompts when possible
- •Verifies if the mount was successful
This should work seamlessly in Jamf deployments. Let me know if you need any tweaks!
Hey Jonas,
You’re right—using a Bash script for this is a cleaner approach, especially when deploying via Jamf. Below is a Bash script that replicates your AppleScript functionality. It dynamically retrieves the currently logged-in user and mounts the SMB share accordingly:
#!/bin/bash
# Get the currently logged-in user
currentUser=$(stat -f%Su /dev/console)
# Define the SMB share path
serverAddress="our_server_address"
sharePath="smb://$serverAddress/homes/$currentUser"
# Check if the share is already mounted
if mount | grep "$sharePath" > /dev/null; then
echo "Share already mounted."
exit 0
fi
# Create mount point if it doesn't exist
mountPoint="/Volumes/$currentUser"
if
! -d "$mountPoint" ]; then
mkdir -p "$mountPoint"
fi
# Mount the SMB share
osascript -e "try" -e "mount volume \\"$sharePath\\"" -e "end try"
# Verify if the mount was successful
if mount | grep "$serverAddress"; then
echo "Successfully mounted $sharePath"
else
echo "Failed to mount $sharePath"
exit 1
fi
Explanation:
- •Retrieves the currently logged-in user with stat -f%Su /dev/console
- •Constructs the SMB path dynamically
- •Checks if the share is already mounted before proceeding
- •Creates a mount point if it doesn’t exist
- •Uses osascript to mount the volume via Finder, avoiding password prompts when possible
- •Verifies if the mount was successful
This should work seamlessly in Jamf deployments. Let me know if you need any tweaks!
Thank you very much @Valcovish,
This script just works and very good as well. Now when I've been testing a lot, it's easy to understand the code as well. 😊👍
Glad to hear that, please consider marking the topic as resolved.