Go Back   The macosxhints Forums > OS X Help Requests > Networking



Reply
 
Thread Tools Rating: Thread Rating: 4 votes, 4.25 average. Display Modes
Old 10-14-2012, 06:22 AM   #1
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
Question Mounting a file share via script and Fast User Switching

Hi everyone,

sometimes I need to mount a file share using a shell script. It is usually something like this:

mkdir /Volumes/data
mount -t smbfs //username@192.168.1.10/data /Volumes/data

This works fine as long as thereís only one user account on the Mac. The problem is that sometimes there are 2 or more users sharing a Mac. They often use Fast User Switching since they do not want to log out in between. User A issues the shell script to mount the share. User B logs in via FUS and wants to access the share, too. But he/she instead gets an error from the script:

mkdir: File exists

Of course that is correct since /Volumes/data would only get removed if user A logs out or unmounts the volume. Any idea how to make this a bit more intelligent/reliable?

I also wonder how to replace the username in the mount command with a placeholder for the currently logged in user. I know that echo $USER returns the currently logged in user, but mount_afp does not like it (error 22) when I replace the username with $USER. Any ideas on that as well?

Thanks!
BjŲrn
poenn is offline   Reply With Quote
Old 10-14-2012, 03:50 PM   #2
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,193
For the first problem, punt if the volume is already mounted:

Code:
if [[ ! -e /Volumes/data ]]; then
    sudo mkdir /Volumes/data
    sudo mount -t smbfs //username@192.168.1.10/data /Volumes/data
fi
I have no SMB shares to test with, but that syntax looks odd to me. Shouldn't there be a protocol somewhere in the mount command? (sudo mount -t smbfs afp://username@..., or maybe smb:?)

For the second problem, note that $USER is the name of the current user on your machine, while "username" is the name of a user on the server. There's no automatic correlation between names on different computers.

Your sample code doesn't include any sudo commands, making me think it's already running as root. In other words, $USER is probably currently set to "root".
ganbustein is offline   Reply With Quote
Old 10-15-2012, 10:59 AM   #3
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
Thanks for your reply! I should have given more information, sorry.

The syntax indeed looks odd, but works with smb as long as you specify the protocol via -t. It is even shown like that in the manpage for mount_smbfs which is called by mount. Usually I do use smb:// or afp:// of course.

The username in my case is identical to the one on the server since the current user is always a directory services based network account. That is the main reason why I did not want to hardcode a username in the script and instead use the name that I can get via echo $USER. It would automatically be always the correct one this way.

My script is not running as root, but always as the currently logged in user. That is why I want to avoid sudo since most accounts are not admins and not in the sudoers list. I managed to circumvent the first problem by not using the same mount point (/Volumes) for everyone. Hereís an afp example of what the script is currently doing:

Code:
mkdir ~/Data
mount -t afp afp://user:pass@IP/Data ~/Data
This works fine, even when using Fast User Switching since everyone has its own mount point. I will change that later to somewhere inside ~/Library so the user usually wonít get to see.

Now it would be ideal if I could just replace user in the above example with a variable for the currently logged in user. The one reported by

Code:
echo $USER
is correct. But I canít use $USER with the mount command since this gives an error. It would be absolutely perfect if I could use something like

Code:
mount -t afp afp://%USER:PasswordFromKeychain@IP/Data ~/Data
But I could live with a login prompt (Keychain stored password would be awesome, of course) if I just could use a variable for the username. Any ideas on that?

Thanks a lot!

Last edited by poenn; 10-15-2012 at 11:03 AM.
poenn is offline   Reply With Quote
Old 10-15-2012, 03:19 PM   #4
ganbustein
MVP
 
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,193
The reason I thought you needed to be running as root is that, at least as far back as Snow Leopard, /Volumes has had an ACL that says "group:everyone deny add_file,add_subdirectory,directory_inherit,only_inherit". The mkdir command will fail unless you're using root privileges, running something older than Snow Leopard, or you've removed that ACL.

But OK, moving the mount point into the home directory solves that problem. Some utilities and lots of scripts won't see it there (because they find volumes by searching /Volumes instead of doing the moral equivalent of diskutil list), but it should mostly work.

But I don't understand why something like:

mount -t smbfs //$USER@192.168.1.10/data ~/Data

won't work. $USER is expanded by the shell, before passing arguments to the mount command. What error are you getting? Are you sure you spelled it $USER and not $user or %USER?

Are you sure the following character was not in the list of valid characters for a variable name? (For example,

mkdir /Volumes/$USER_data

won't work (because the shell thinks you're interpolating the value of a variable named USER_data), but both of

mkdir /Volumes/Data_$USER
mkdir /Volumes/${USER}_data


will.)


Try putting an echo in front of the command you're trying:
echo mount -t smbfs //$USER@192.168.0.10/data ~/Data
to see if you're passing to the mount command what you think you're passing.
ganbustein is offline   Reply With Quote
Old 10-17-2012, 10:55 AM   #5
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
It is weird, ls -le gives me that:

drwxrwxrwt@  5 root  admin      170 17 Okt 17:41 Volumes
0: group:everyone deny add_file,add_subdirectory,directory_inherit,only_inherit


I just verified with another (non-admin) user account and that one could also use mkdir inside /Volumes just fine. I have not changed any POSIX/ACL privileges. Well, so be it.

Thanks for pointing me in the right direction with the echo command! I was testing the script in my test environment and I stupidly used a local account at the time of testing. This account of course does not exist on the server.

I will keep in mind the issues youíve mentioned concerning the non-standard mount point. But I suppose it will not be a problem for the users which will use the mount script.

I tested with afp here (smb should work the same) and this works:

mount -t afp afp://$USER:hardcodedpassword@10.0.77.3/data ~/data


It would be perfect if I could somehow pull the password out of the userís keychain instead of hardcoding it into the script. But I suppose thatís impossible or at least not trivial?
poenn is offline   Reply With Quote
Old 10-17-2012, 11:06 AM   #6
fracai
MVP
 
Join Date: May 2004
Posts: 2,094
Quote:
Originally Posted by poenn
It would be perfect if I could somehow pull the password out of the user’s keychain instead of hardcoding it into the script. But I suppose that’s impossible or at least not trivial?

This may help: http://blog.macromates.com/2006/keyc...ss-from-shell/

Don't miss the first comment, which refers to complex passwords.
__________________
i am jack's amusing sig file
fracai is offline   Reply With Quote
Old 10-17-2012, 01:28 PM   #7
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
Holy sh..! Thanks a lot for that. I just tested this and it works beautifully! The macosxhints forums really do not disappoint! I did not know about the security command, powerful stuff here.
poenn is offline   Reply With Quote
Old 10-17-2012, 02:32 PM   #8
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
Thank you ganbustein and fracai!

I am now using a script like this and it works fine:

get_pw () {
security 2>&1 >/dev/null find-generic-password -ga $USER \
|ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
mkdir ~/Data
mount -t afp afp://$USER:$(get_pw)@10.0.77.3/Data ~/Data
poenn is offline   Reply With Quote
Old 10-17-2012, 02:34 PM   #9
poenn
Triple-A Player
 
Join Date: May 2002
Location: Germany
Posts: 83
Thank you ganbustein and fracai!

I am now using a script like this and it works fine:

get_pw () {
security 2>&1 >/dev/null find-generic-password -ga $USER \
|ruby -e 'print $1 if STDIN.gets =~ /^password: "(.*)"$/'
}
mkdir ~/Data
mount -t afp afp://$USER:$(get_pw)@10.0.77.3/Data ~/Data
poenn is offline   Reply With Quote
Reply

Tags
currently logged in user, fast user switching, mount, shell script

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 11:26 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.