Mount network share

schradera
New Contributor III

Hello all! Hoping to gain some knowledge / help from anyone that likes to use terminal commands. I'm having trouble mounting a network share with the end goal of using that network share as a time machine back location for a few of our machines. Here's the script I have so far. It works when executed locally BUT I get an error code when we try to run it attached to a policy. I have VERY limited experience with scripting and terminal commands. I've attached the error I get. Thanks in advance!

Client Machines are running macOS 10.10.5
JSS Version JSS 9.101.0

Begin Script

Mount the network Drive

osascript -e ‘Mount Volume “afp://[username]:[password]@[IP Address]/TimeMachine”’
sleep 5

Hide the network drive to help prevent the user from unmounting it

chflags hidden “/Volumes/TimeMachine”
sleep 5

Restart Finder to show new session

osascript -e ‘quit application “Finder”’
48f4c93d1c9141ea9413760cb0aa1253

1 ACCEPTED SOLUTION

Asnyder
Contributor III

@schradera We use time machine for our teacher backups as well. However, I don't automate the setup. I just manually set it up on each computer. But basically, after the drive has been mounted once and set for backup it shouldn't have to be mounted again. I'm sure this could be automated using

tmutil

though.

View solution in original post

14 REPLIES 14

Asnyder
Contributor III

Can you post the entire script again but using the ">_" button on here. It makes it easier to look at compared to plain text.

Here is an example script that I use for mounting shares. I know it says for AD machines only but it does work in my environment and we don't use AD.

EDIT: You may need to run the mount command as the logged in user. Policies run as root.

#!/bin/bash
#2017 Version Samuel Look
#All care no responsibility
#Mounts the requested share if it doesn't already exist if left blank it will attempt to mount AD SMBhome
#Accepts shares in the form smb://server/share
#Intended to be run as a Login policy from Casper on AD bound machines only and has only been tested in this context.

##### Start seperate process #####
(

##### SUBROUTINES #####

Share_Path_Valid() {
if [[ -z "$Share_Path" ]]; then
Machine_Domain=$(dscl /Active Directory/ -read . SubNodes | awk '{print $2}')
Share_Path="$(dscl "/Active Directory/$Machine_Domain/All Domains" -read /Users/$Current_User SMBHome | awk '!/is not valid/' | sed -e 's/SMBHome: /smb:/g' -e 's/\///g')"
fi
if [[ "$Share_Path" ]]; then
logger "Sharemount:$Share_Name Path check PASS $Share_Path"
return 0
else
logger "Sharemount:$Share_Name Path check FAIL"
return 1
fi
}

#####

User_Ready() {
Loop_End=$((SECONDS + 60))
Current_User=$(stat -f%Su /dev/console | awk '!/root/')
while [[ -z "$Current_User" ]] && [[ $SECONDS -lt $Loop_End ]]; do
sleep 10
Current_User=$(stat -f%Su /dev/console | awk '!/root/')
done
if [[ "$Current_User" ]]; then
logger "Sharemount:$Share_Name User check PASS $Current_User"
return 0
else
logger "Sharemount:$Share_Name User check FAIL"
return 1
fi
}

#####

Finder_Ready() {
Loop_End=$((SECONDS + 60))
while [[ -z "$(ps -c -u $Current_User | awk /CoreServicesUIAgent/)" ]] && [[ $SECONDS -lt $Loop_End ]]; do
sleep 10
done
if [[ "$(ps -c -u $Current_User | awk /Finder/)" ]]; then
logger "Sharemount:$Share_Name Finder check PASS"
return 0
else
logger "Sharemount:$Share_Name Finder check FAIL"
return 1
fi
}

#####

Not_Mounted() {
if [[ -z "$(mount | awk '/'$Current_User'/ && //'$Share_Name' /')" ]]; then
logger "Sharemount:$Share_Name Mount check PASS $Share_Name"
return 0
else
logger "Sharemount:$Share_Name Mount check FAIL already mounted"
return 1
fi
}

#####

Mount_Drive() {
True_Path=$(echo $Share_Path | sed 's//////'$Current_User'@/g')
logger "Sharemount:$Share_Name Attempting to mount $True_Path"
sudo -u $Current_User osascript -e 'mount volume "'$True_Path'"'
}

##### START #####

Share_Path=$4
Share_Name="$(echo $Share_Path | awk -F"/" '{print $NF}')"

if User_Ready && Finder_Ready && Share_Path_Valid && Not_Mounted; then
sleep 4
Mount_Drive
else
logger "Sharemount:$Share_Name Conditions not met to attempt drive mounting $Share_Path"
fi

##### End seperate process #####
) &

##### FIN #####

schradera
New Contributor III
#!/bin/sh
#Mount the network Drive
osascript -e ‘Mount Volume “afp://[account]:[password]@[location]”’
sleep 5

#Hide the network drive to help prevent the user from unmounting it
chflags hidden “/Volumes/TimeMachine”
sleep 5

#Restart Finder to show new session
osascript -e ‘quit application “Finder”’

schradera
New Contributor III

Thanks for the response. I was wondering if the user account would have something to do with it. I'll try the script and see what it does for me. Thank you so much!

Asnyder
Contributor III

@schradera What did you write the script in? I just noticed it's using fancy quotes for everything. and given the error message for syntax I bet that's the issue.

Try This. Notice how the quotes look. It does make a difference.

#!/bin/sh
#Mount the network Drive
osascript -e 'Mount Volume "afp://[account]:[password]@[location]"'
sleep 5

#Hide the network drive to help prevent the user from unmounting it
chflags hidden "/Volumes/TimeMachine"
sleep 5

#Restart Finder to show new session
osascript -e 'quit application "Finder"'

tkimpton
Valued Contributor II

@Asnyder that looks like a mistake sometimes it happens if you copy text from Notes etc

Asnyder
Contributor III

@tkimpton That's correct but bash doesn't like fancy quotes. So if that's how to script is being presented to bash that's why it's throwing a syntax error. That's why I'm asking what text editor he used to make the script. From experience, those quotes will cause errors.

schradera
New Contributor III

@Asnyder Typically I use textedit and ensure I convert it to plain text. I work at a Vo-Tech school and our web design students sometimes use Brackets so just to try it I used that as well...so plenty of room for error. XD.

I'll make more of an effort to us vi in terminal I suppose.

I'm going to make an effort to test your script today and get back to you. Thanks again for the response.

schradera
New Contributor III

@Asnyder Well that script is just awesome. I really appreciate you sharing. After some minor settings it did mount the users network share. That is a good option if I can't get the Time Machine working because the information that the user chooses to put into the network share is on a server with some form of RAID and that information is already backed up with a different tool.

So if I look at it from a certain perspective that means that there should be no need to use Time Machine. The only real benefit to Time Machine is that backs up all the things without the users interaction.

Maybe I'll just have to get over this, BUT that's not exactly what I was looking for. I also did a poor job of explaining what I needed to do. We have a synology disk station that I had intended to use for the teachers Time Machine back up. Synology has a local user account and an AFP share for Time Machine specifically. So basically, if I can mount a network share and hide it, I could then use managed preferences in the JSS to configure time machine. The few lines of code I pulled from different resources and call a script work when manually entered but not when applied to other machines.

Any thoughts on that?

Asnyder
Contributor III

@schradera We use time machine for our teacher backups as well. However, I don't automate the setup. I just manually set it up on each computer. But basically, after the drive has been mounted once and set for backup it shouldn't have to be mounted again. I'm sure this could be automated using

tmutil

though.

milemin
New Contributor

Hi,

Are you looking for this;
https://www.jamf.com/jamf-nation/third-party-products/files/476/mountnetworkshare-sh-mount-a-network-share

Thanks

schradera
New Contributor III

@Asnyder tmutil worked best for my application. Thanks a TON!

Working on the first backup now. I'll mark this as solved once I know it completes.

schradera
New Contributor III

Awesome! Thanks for the help y'all!

Asnyder
Contributor III

Can you post a copy of the script here with sensitive info (usernames passwords) removed/replaced?

Look
Valued Contributor III

@Asnyder To be honest the AD only thing is simply because we have an AD environment so there was zero testing done in any other context when it was created. :-) I am guessing the only real issues outside of AD would be with it trying to determine the H: drive location when no path was passed as this is pulled from the domain information.
You might also get prompted for a username and password the very first time it tried to mount a shareas there would be no authentication available.