Skip to main content
Question

Looking to create a SMB Auto Mount Policy

  • February 20, 2017
  • 14 replies
  • 57 views

Forum|alt.badge.img+3

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

Forum|alt.badge.img+14
  • Valued Contributor
  • February 20, 2017

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • February 20, 2017

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

Ben


Forum|alt.badge.img+15
  • Esteemed Contributor
  • February 20, 2017

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 + "
");'`

Forum|alt.badge.img+3
  • Author
  • New Contributor
  • February 20, 2017

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?


Forum|alt.badge.img+4

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.


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • February 20, 2017

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

Ben


Forum|alt.badge.img+15
  • Esteemed Contributor
  • February 20, 2017

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.


Forum|alt.badge.img+4

@bmethvin

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


Forum|alt.badge.img+3
  • Author
  • New Contributor
  • February 20, 2017

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
Forum|alt.badge.img+36
  • Hall of Fame
  • February 20, 2017

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


Forum|alt.badge.img+7
  • Valued Contributor
  • February 20, 2017

@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.


Forum|alt.badge.img+10
  • Contributor
  • February 20, 2017

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!


Forum|alt.badge.img+15
  • Esteemed Contributor
  • February 20, 2017

@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

Forum|alt.badge.img+7
  • Valued Contributor
  • February 20, 2017

@cbrewer Thank you very much!