Mount Network Drive

Luket
New Contributor

Hi All,
I'm looking for a script that can mount network drives and be able to have it displayed on the desktop or even in the sidebar in finder.

Using Yosemite 10.10.3

14 REPLIES 14

c_kay
New Contributor III

I use the following login hook. It uses Kerberos authentication but you could use name and password. You can replace the afp:// with smb:// was well.

#!/bin/sh

user=$1

server=hostname.of.server

share=ShareName

# Mount network share as user

sudo -u $user mkdir /Volumes/$share

sudo -u $user mount_afp "afp://;AUTH=Client%20Krb%20v2@$server/$share" /Volumes/$share

if [ $? ]
then rmdir /Volumes/$share
fi

exit 0

bentoms
Release Candidate Programs Tester

@Luket We use this

@c.kay Loginhook or launch agent?

jaharmi
Contributor

I know you mentioned a script, but there are also configuration profile "Login Items" options to mount network shares — in Profile Manager (at least v4) and JSS (at least 9.7). I have not tried them, so not sure whether or not they work — or how well.

davidacland
Honored Contributor II
Honored Contributor II

We've tried the login item route with config profiles as it would have been a good quick win, but we got bitten. It was intermittently not working and removing the profile left the login item in place so we could remove, only add.

If it's any help we've been using this script recently to mount drives:

#!/bin/sh

protocol="$4" # This is the protocol to connect with (afp | smb)
    echo "Protocol: $4"
serverName="$5"   # This is the address of the server, e.g. my.fileserver.com
    echo "Server: $5"
shareName="$6"    # This is the name of the share to mount
    echo "Sharename: $6"

# Mount the drive 
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "$protocol://${serverName}/${shareName}"
    end tell
EOT`

exit 0

We add it to the JSS and set the necessary labels for $4, $5 & $6. It can then be used in lots of policies just by setting the necessary values.

We've used the mount_afp method as well, but there has to be quite a lot of logic when you use if $? rmdir. There are quite a few scenarios that can cause a false positive and then proceed to empty the network drive. This is one of the reasons we now prefer AppleScript mounting.

msnowdon
Contributor

We use a script:

!/bin/bash

sudo -u $3 /usr/sbin/jamf mount -server BAN-S.ANDOVERMA.LOCAL -share COMMON-S -type smb -visible

exit 0

Replace the server's fqdn and share name with yours. I found this on JAMF Nation and its been working great. I was using a Configuration profile to do it but it takes a couple of logins to get it to show up which was not ideal in a lab environment where users didn't always use the same computer.

msnowdon
Contributor

Maybe someone can help me. My script is mounting network shares but my home directory would mount on its own and show a folder on the dock. That broke when I upgraded from Casper 8 to 9. All I would see on the dock was a question mark. JAMF sent me a script called fixquestionmark.sh which worked great until I tested it out on a Yosemite machine.

Any ideas?

bentoms
Release Candidate Programs Tester

@msnowdon I thought the home folder was mounted & added to the dock as part told the AD binding? I've not done that for a while but is that something you can try?

daz_wallace
Contributor III

My understanding was the same as @bentoms, in that if you force a local home but leave the "Use UNC path..." option on, it would map the home folder and add it to the dock.

Normally, if there's a "?" then it didn't mount or mounted after the dock loaded. Unfortunately this is why we implemented our own system to map a user's home folder. Not seen it as part of Casper related behaviour I'm afraid!

Darren

msnowdon
Contributor

@bentoms & @daz_dar ,

I have my computer bound to AD with those items selected. It mounts the home folder in the dock just fine until I enroll the computer into Casper. Then I just get a question mark. This behavior started back when we upgraded the JSS from 8 to 9. JAMF support sent me a script which fixed the problem until I started playing around with Yosemite.

I was able to find another script on JAMF Nation to mount the home directory with Yosemite but I still see the question mark unless I go back into the Directory Utility and uncheck the option "use UNC path..."

Thanks for the input.

Mark

bentoms
Release Candidate Programs Tester

@msnowdon is the JSS managing some dock settings then?

msnowdon
Contributor

@bentoms

No but for the heck of it I went back into my Yosemite machine and re-checked the option in the Directory Utility to "Use UNC path...", rebooted and it loaded the home directory on the dock! No question mark this time.

I've been making a lot of tweaks such as converting all my MCX Preferences to Configuration Profiles since Yosemite doesn't recognize them but I'm not sure what I changed that corrected the issue.

But thanks for your help Ben.

dstranathan
Valued Contributor II

Just a follow-up:

I use David's AppleScript idea and it has worked great. Much better than the unix mount commands.

I want to clean the code up a bit (make it easier to read, more modular etc). So I am going to rewrite my mount scripts soon. I'm playing with a few ideas.

My objectives:

1) Convert the AppleScript mount code into a function.

2) Replace the backticks with $(). This command substitution and elimantion of the EOT (here document) appears to be my problem - see below,

3) Misc cosmetic changes to keep things tidy, modern and consistent.

Im having issues here. Beating head against a desk. Script wont exit. Just spins forever. Everything looks good to me. Need a sanity check, please!

David's original code (from earlier in this post):

    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder" 
    activate
    mount volume "$protocol://${serverName}/${shareName}"
    end tell
EOT`

My proposed changes:

function mountAppleScript() {

    input=$(/usr/bin/osascript  &>/dev/null
    tell application "Finder" 
    mount volume "${protocol}://${serverName}/${shareName}"
    end tell)

}

mountAppleScript

McKinnonTech
New Contributor III

@dstranathan Did you ever get an answer on this?

dstranathan
Valued Contributor II

Yes, I revised my SMB mount scripts this summer. The AppleScript function looks like this:

function AppleScript_Mount()
{
    mount_script=`/usr/bin/osascript  > /dev/null << EOT
    tell application "Finder"
    mount volume "${protocol}://${serverName}/${shareName}"
    end tell
EOT`
}