Open SMB share as non-root user from command line without "Connecting to" prompt?

DanJ_LRSFC
Contributor III

I've got a script that's running as a non-root user that needs to be able to open an SMB share. As it's not root, it can't create a folder in /Volumes, so I need to use "open" rather than "mount".

However in recent macOS versions there's a prompt saying "Connecting to" such and such a server.

Is there any command line switch which can bypass that prompt?

Thanks,
Dan Jackson (Senior ITServices Technician)
Long Road Sixth Form College
Cambridge, UK.

2 REPLIES 2

Hugonaut
Valued Contributor II

have you tried

mount_smbfs //$Uname:$Passw@$SambaShare $Location

I haven't actually done this but worked off the Man Page - 'man mount_smbfs'

NAME
     mount_smbfs -- mount a shared resource from an SMB file server

SYNOPSIS
     mount_smbfs [-N] [-o options] [-d mode] [-f mode] [-h] [-s] [-v]
                 //[domain;][user[:password]@]server[/share] path

DESCRIPTION
     The mount_smbfs command mounts a share from a remote server using
     SMB/CIFS protocol.
________________
Looking for a Jamf Managed Service Provider? Look no further than Rocketman
________________


Virtual MacAdmins Monthly Meetup - First Friday, Every Month

ryan_ball
Valued Contributor

@DanJ_LRSFC Try this:

#!/bin/bash

# SMB location here
serverShare="server.yourcorp.com/share"

# Allow unknown servers
# https://support.apple.com/en-us/HT207112
# This should really be done via a Configuration Profile
defaults write /Library/Preferences/com.apple.NetworkAuthorization AllowUnknownServers -bool YES
sleep 5

# Mount the drive as john_doe while logged in as john_doe
loggedInUser=$(/usr/bin/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 + "
");')
path="smb://${loggedInUser}:@${serverShare}"
/usr/bin/osascript -e "mount volume "$path""

exit 0