|
|
#1 |
|
Triple-A Player
Join Date: May 2002
Location: Germany
Posts: 83
|
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 |
|
|
|
|
|
#2 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,203
|
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
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". |
|
|
|
|
|
#3 |
|
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 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 Code:
mount -t afp afp://%USER:PasswordFromKeychain@IP/Data ~/Data Thanks a lot! Last edited by poenn; 10-15-2012 at 12:03 PM. |
|
|
|
|
|
#4 |
|
MVP
Join Date: Apr 2008
Location: Berkeley CA USA
Posts: 1,203
|
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. |
|
|
|
|
|
#5 |
|
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 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? |
|
|
|
|
|
#6 | |||||||||||||||||||||||
|
MVP
Join Date: May 2004
Posts: 2,100
|
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 |
|||||||||||||||||||||||
|
|
|
|
|
#7 |
|
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.
|
|
|
|
|
|
#8 |
|
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 |
|
|
|
|
|
#9 |
|
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 |
|
|
|
![]() |
| Tags |
| currently logged in user, fast user switching, mount, shell script |
|
|