1

I'd like to mount a Samba drive in OS X using bash. This line does the trick:

mount -t smbfs //$SAMBAUSER@$ADRESS/$NAMEOFSHARE $MACOSXPATH

only one problem. I want it done without user input - which means no password can be manually put in. And I'm not going to ask my users to download fink just so they can install expect (as seen here).

I tried applying the accepted solution to a similar StackOverflow problem shown here by doing this:

echo "mypassword" | mount -t smbfs //$SAMBAUSER@$ADRESS/$NAMEOFSHARE $MACOSXPATH --stdin

but no luck - that doesn't work and Mac OS X tells me I used mount command improperly:

usage: mount [-dfruvw] [-o options] [-t ufs | external_type] special node
   mount [-adfruvw] [-t ufs | external_type]
   mount [-dfruvw] special | node

Any suggestions? This would be easy with an expect script - but that would ruin the user experience to have that prerequisite in my mind.

Community
  • 1
  • 1
lollercoaster
  • 15,969
  • 35
  • 115
  • 173

4 Answers4

4

The answer in this apple support discussion thread worked for me:

osascript -e 'mount volume "smb://user:password@server/share"'
Stuart Berg
  • 17,026
  • 12
  • 67
  • 99
4

If mount(8) can't just call the mount syscall on the filesystem, it looks for a program to help it. On FreeBSD and Mac OS X, those helper programs follow the naming convention mount_XXX, where XXX is the -t argument's value.

That means you want to check the mount_smbfs(8) man page, which tells us about -N:

-N    Do not ask for a password.  At run time, mount_smbfs reads the ~/Library/Preferences/nsmb.conf
      file for additional configuration parameters and a password.  If no password is found,
      mount_smbfs prompts for it.

Unfortunately the man page trail ends with one for nsmb.conf that doesn't mention anything about storing passwords. On FreeBSD 8.0, at least, the solution is to put a password key with a plain text(!) password value under a [SERVER:USER] heading. That would be type C according to the linked nsmb.conf man page.

So it seems that you'll want to dump a pre-configured nsmb.conf into your user's ~/Library/Preferences/ directory and then call your mount command with -N. As far as I know you can't provide a hashed value, which is not especially awesome. I'll try to get access to a MacBook in a few hours to test this.

NB: This is not how to do it with the GNU toolchain. If you're on Linux, you're probably going to be using something like mount.cifs(8). The correct solution in that case is the credentials=filename option (used after -o, of course), where filename is a file of credentials in key=value form, separated by newlines. See http://linux.die.net/man/8/mount.cifs

Mark Williams
  • 638
  • 6
  • 10
2

I'll give you 2 options. First, include the password on the command line:

mount -t smbfs //$SAMBAUSER:$PASSWORD@$ADRESS/$NAMEOFSHARE $MACOSXPATH

This is not a great option because the command line (including password) is visible to anyone who happens to be logged in at the moment. Not great, but it is a possibility.

Second, use expect. The Mac OS X Hints article you linked dates from 2002, when OS X v10.2 would've been current. While 10.2 apparently didn't include expect as a standard component, 10.6 does, and I'm pretty sure it's been included for several versions now.

#!/usr/bin/expect -f
spawn mount -t smbfs //fred@12.34.56.78/myfiles /tmp/mountpoint
expect "Password:"
send "wibble\r"
wait
Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • absolutely. thanks! I just used the first for simplicity. I do wonder about expect though - I couldn't seem to find any resources about which version it started being included on Mac OS X.... – lollercoaster Jul 03 '11 at 06:57
  • just out of curiosity - the expect script does not work. I tried it with `sudo expect script-name` and it just echoed the line `spawn mount -t smbfs //fred@12.34.56.78/myfiles /tmp/mountpoint` (except with real values of course) and then prompted me for my password....what went wrong? – lollercoaster Jul 03 '11 at 07:20
  • It sounds like the `expect "Password:"` isn't matching properly against the prompt. Try running it with `sudo expect -d ./ts` to get a debug-mode narration of what's happening as it runs. – Gordon Davisson Jul 03 '11 at 19:22
0

You not only can use a hashed value, you're expected to, at least in older releases. The crypt option for smbutil is not DOD-level security but as with most security, you're trying to keep the honest people honest: the bent ones will find a way. It seems to be broken in Mountain Lion but the nsmb.conf file in ~/Library/Preferences should be secure at the OS level. The /etc/nsmb.conf would override it if it exists. I'm sure there's a reason why plain text files are obsolete but didn't we go this with NetInfo? How long did it take for Apple to allow the old standbys (/etc/hosts, /etc/passwd) to be used?

paul
  • 1