简体   繁体   中英

mount a drive on Mac OS X with bash script (and NOT use expect)

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.

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

osascript -e 'mount volume "smb://user:password@server/share"'

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM