Looking to create a SMB Auto Mount Policy

bmethvin
New Contributor

Wanting to create an Auto Mount Policy for SMB Share based on the current user who logs in. This would allow any user to access there SMB shared docs at any computer.

How do i pass through the current user to this?

smb://$currentUser@{ServerName}/$currentUser/Docs

14 REPLIES 14

tthurman
Contributor III

Current user can be found like this:

currentUser=$(/bin/ls -l /dev/console | /usr/bin/awk '{ print $3 }')

You might have trouble passing passwords though.

Regards,
TJ

bmethvin
New Contributor

Thanks @tthurman ! Not worried about passwords. Want the user to have to enter that themselves.

Ben

cbrewer
Valued Contributor II

Many of us have switched to using the following for finding the current logged in user. This code supposedly came from someone at Apple at some point.

loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

bmethvin
New Contributor

What is the best process for doing this? Script in JAMF, if so what have does yours look like? Or are others doing it another way?

Pacers31Colts18
New Contributor

What I did, because I found it to be annoying if a user wasn't on the network getting an error on login.

I followed Mac Mule's guide and created an application to mount the drives. I then just made this available in Self Service and let the users run as needed.

Some have it as a launch agent instead, but I didn't like that way personally.

bmethvin
New Contributor

@Pacers31Colts18 You wouldn't happen to have a link to that article would you?

Ben

cbrewer
Valued Contributor II

We don't auto mount, but we do use dockutil scripts to create SMB links in the dock for users. This works pretty well for our use.

Pacers31Colts18
New Contributor

@bmethvin

https://macmule.com/2011/09/08/how-to-map-drives-printers-based-on-ad-group-membership-on-osx/

bmethvin
New Contributor

We don't have an AD. I am trying to fake this script to choose the current logged in user...

So for example:

smb://$currentUser@{ServerName}/$currentUser/Docs

I need $currentUser to auto populate per the user logged in.

I am having no luck with this so far. It simple to accomplish by typing in the user but I want to be able to pass this to everyone per login.

donmontalvo
Esteemed Contributor III

We've been getting beat over the head by our UNIX team to stop using backticks...

So this:

loggedInUser=`python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");'`

Should be:

loggedInUser=$( python -c 'from SystemConfiguration import SCDynamicStoreCopyConsoleUser; import sys; username = (SCDynamicStoreCopyConsoleUser(None, None, None) or [None])[0]; username = [username,""][username in [u"loginwindow", None, u""]]; sys.stdout.write(username + "
");' )

http://pubs.opengroup.org/onlinepubs/9699919799/xrat/V4_xcu_chap02.html#tag_23_02_06_03

--
https://donmontalvo.com

osxadmin
Contributor II

@cbrewer "We don't auto mount, but we do use dockutil scripts to create SMB links in the dock for users. This works pretty well for our use."

could you provide your work-flow (scripts/steps) on how you are utilizing SMB links on the dock (using dockutil).

Thank you.

stevevalle
Contributor III

We disable auto mount on login to speed up login times, so I created an AppleScript app that the user is able to click on to mount a network drive. The app is located in the /Library/Application Support folder (but can be placed anywhere) and the icon is placed in the dock using dockutil.

The script looks something like this:

set userName to do shell script "whoami"
set mountString to "smb://network.share/home/" & userName

try
    if (list disks) does not contain userName then
        mount volume mountString
        delay 3 -- wait for the volume to mount or Finder will fail to open
        tell application "Finder"
            activate
            open disk userName
        end tell
    else
        tell application "Finder"
            activate
            open disk userName
        end tell
    end if
end try

We push out the app via a policy to all computers, which also includes the dockutil script to add it to the users dock.

Hope this helps!

cbrewer
Valued Contributor II

@osxadmin Here's a piece of one of my scripts. memberOf finds AD groups that the user belongs to. OthersList is basically a list of what's already in the dock so we don't add a duplicate. The script then only makes additions to the dock if the conditions are right.

#!/bin/bash

#variables
user=$3
memberOf=`id -G -n $3 | tr '[:upper:]' '[:lower:]'`
OthersList=`defaults read /Users/$user/Library/Preferences/com.apple.dock persistent-others | grep "label"`
server="myserver.domain"

if [[ $memberOf =~ "test group" ]];then
    if [[ ! $OthersList =~ "Test Share" ]];then
        /usr/local/bin/dockutil --add smb://$server/share/test --label 'Test Share' --position end --no-restart /Users/$user
        echo "Added Test Share"
    fi
fi

osxadmin
Contributor II

@cbrewer Thank you very much!