Script to mount SMB share?

joshbuddy
New Contributor III

Does anyone have an easy script to auto mount an SMB share that I could make a policy with through casper? I have a bunch of Windows servers used for online testing in our school district that have a data folder that needs to mount for the client program to work. Any help would be appreciated.

Thanks
-Josh

31 REPLIES 31

stevewood
Honored Contributor II
Honored Contributor II

This should work, but it will pass the password in clear text and it will be
On Thu, Aug 12, 2010 at 1:03 PM, <josh.glassing at spps.org> wrote:
stored in the script. You could write this in AppleScript and use some
dialogs to grab the user name and password:

#!/bin/sh

userName="user"
userPass="pass"
shareName="server.mynetwork.com/sharename"
whereToMount="/Volumes/Share"

mount_smbfs //$userName:$userPass@$shareName $whereToMount

Now, I have not tested that script, but I did test the mount_smbfs from the
Terminal to make sure it worked. Of course, this will not mount the volume
on the desktop. Not sure how to do that.

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475

ernstcs
Contributor III

stevewood
Honored Contributor II
Honored Contributor II

Also, the whereToMount location needs to exist before running the
mount_smbfs command. So you may want a command in there that does:

mkdir $whereToMount

before the mount_smbfs command.

Steve

tlarkin
Honored Contributor

Do you use Kerberos?

tell application Finder

mount smb://server.com/sharepoint

end tell

If everything is kerberized you shouldn't need to authenticate.

Not applicable

This is one that I use. Ultra basic.

-- Grant Lutz

joshbuddy
New Contributor III

Cool I really appreciate all the replies from everyone, I am going to try some of these out.

-Josh

Re: [Casper] Script to mount SMB share?

Grant Lutz (CW) to:
josh.glassing at spps.org, casper at list.jamfsoftware.com
08/12/2010 01:40 PM

This is one that I use. Ultra basic.

-- Grant Lutz

John_Wetter
Release Candidate Programs Tester

NWEA Testing? If so, there are all kinds of fun automation scripts floating around this list and the MN-EDU list. Just want to confirm before we narrow into that.

-John

lance_ogletree
Contributor

What Craig said.
There's a network mount script in the resource kit you should examine.

-Lance

joshbuddy
New Contributor III

Yes sir NWEA testing!

fun stuff!

-Josh

Re: [Casper] Script to mount SMB share?

John Wetter to:
Casper List
08/12/2010 03:17 PM

tlarkin
Honored Contributor

If it is NWEA testing....I feel your pain. I have an article how I deployed it and dealt with it over the pat 3 years.

http://tlarkin.com/tech/how-deploy-nwea-testtaker-64-intel-macs-casper

Not fun

Not applicable

The mount_smbfs // line is actually sufficient by itself, so long as it has
the user name & password ­ no real need for variables unless you're pulling
that info from elsewhere.

Good idea to create the mount point directory as well.

Mounting the shares on the desktop is easily enabled in the Finder
preferences ­ on the General tab under 'Show these items on the desktop:'
--> Connected servers. :)

mbuckner
Contributor

We started using the one in the resource kit over the weekend, and it works flawlessly for us. By the way, if you don’t specify the server and mount point, it will default to the user’s SMBHome. That’s not in the documentation, but it’s in the code comments. I wasted about 8 hours trying to figure out how to do exactly that (ours still aren’t mounting automatically) before I figured that out. Then I felt really dumb :-)

CasperSally
Valued Contributor II

The one in the resource kit works well for us too.

Anyone know if it's possible to set the share to mount hidden (if you have network shares set to show in Finder). I believe we used to do this through WGM with login items for mounted servers.

Thanks!

ernstcs
Contributor III

I will be deploying it everywhere here at UWEC as I roll out Snow Leopard
here the next few weeks.

It was a lot more reliable on 10.6 than 10.5. The only other thing I did was
create a script for logout that forces the unmount of these shares as well.
If you have a fast login turn around, the system sometimes doesn't quite let
go of those mounts/folders in time for the next person when they login.
Nothing shows up because the command will say the share is already there,
but still has permissions for the previous user.

Also, if you login with a local login that's not domain and can't use those
shares they hang around for a long while, too...at least it appeared to be
that way.

#!/bin/sh

. /etc/rc.common

/sbin/umount -f /Volumes/<share>

Craig E

joshbuddy
New Contributor III

I think I'm just having problems with the syntax in the resource kit, I am pretty much a scripting noob, I don't know if I am inputting the values correctly in the hardcoded value sets. If anyone is willing to share theirs filled out that would be very helpful.

Thanks a bunch for everyones help and input
-Josh

Re: [Casper] Script to mount SMB share?

casperadmin to:
'Buckner, Mark', Lance Ogletree , Casper List
08/13/2010 06:43 AM

CasperSally
Valued Contributor II

It's important that Kerberos is working for this to work - to make sure go, connect to server, type server name, are you prompted with share names without authenticating? If so Kerberos is ok, and you should be able to use resource kit script.

The only values I changed were

afp (I wanted afp, you can use smb)
Server
Sharename

So our server is serversharename<file:///serversharename> and this is what my script looks like

# HARDCODED VALUES SET HERE
loginUsername="$3"
shareUsername="$3" #The username of the user to be used to mount the share
authType="kerberos" #Valid values are "kerberos" (default) or "password"
password="" #Note this only needs to be set if authentication type is "password"
mountType="afp" #The type of file share. Valid types are "afp" (default) or "smb"
serverAddress="server" #The IP address or DNS name of the server - if left blank, the script will search for the "SMBHome" attribute in the user record
share="sharename" #The name of the share you are mounting - if left blank, the script will search for the "SMBHome" attribute in the user record

ernstcs
Contributor III

I didn’t change anything in the script. I added the script to the JSS and then I use policy to customize the options I want, so I can reuse the script for multiple shares with multiple policies.

And what has been mentioned is correct. Kerberos must be working properly for this to function, and the east test is to try and mount the SMB share logged in as that user and not get prompted for authentication.

See attached screens.

Craig E

tlarkin
Honored Contributor

Error on line 1? What shebang are you using? #!/bin/sh?

joshbuddy
New Contributor III

The beginning of the script has #!/bin/sh

I had been opening it in textedit. I removed the script from casper admin, deleted the resource kit off my computer and re downloaded it and uploaded the default mount script from the resource kit straight to admin without opening/modifying it and I am still getting the error.

Re: [Casper] Script to mount SMB share?

Thomas Larkin to:
casper-bounces, josh.glassing, Craig S. Ernst
08/13/2010 08:59 AM

Cc:
"Lance Ogletree", "Casper List"

Error on line 1? What shebang are you using? #!/bin/sh?

tlarkin
Honored Contributor

Copy/paste it in something like TextWrangler and have it show
"invisible" which will show you all tabs and spaces. Perhaps you got a
weird white space in there or something which happens sometimes when you
copy/paste stuff for coding.

joshbuddy
New Contributor III

It doesn't look like there are any out of place spaces or tabs in it in invisible mode. I'm using the original mountNetworkShare.sh that comes in the resource kit with no modifications, only thing i'm doing is labeling the parameters after it goes into casper admin... hmmm :-

Re: [Casper] Script to mount SMB share?

Thomas Larkin to:
josh.glassing
08/13/2010 09:13 AM

Cc:
"Lance Ogletree", "Casper List", casper-bounces, "Craig S. Ernst"

Copy/paste it in something like TextWrangler and have it show "invisible" which will show you all tabs and spaces. Perhaps you got a weird white space in there or something which happens sometimes when you copy/paste stuff for coding.

tlarkin
Honored Contributor

OK

I just looked at the script in the resource kit. Are you running this script as a log in policy? It looks like it is using $3 to find the currently logged in user, which I believe only parses properly when logging in. Casper must run the script, and it must be at log in for $3 to return the proper user name.

joshbuddy
New Contributor III

I just made a policy to run the script and tried it on a couple different computers with different user names (all of which have access to the share and connect up just fine if i command+k connect to them) and the policy status is kicking back the same error as casper remote was... I really hope I'm not overlooking something obvious :-

Re: [Casper] Script to mount SMB share?

Thomas Larkin to:
josh.glassing
08/13/2010 09:50 AM

Cc:
"Lance Ogletree", "Casper List", casper-bounces, "Craig S. Ernst"

OK

I just looked at the script in the resource kit. Are you running this script as a log in policy? It looks like it is using $3 to find the currently logged in user, which I believe only parses properly when logging in. Casper must run the script, and it must be at log in for $3 to return the proper user name.

tlarkin
Honored Contributor

The only thing I can think of, is that you aren't inputting the proper ServerAddress or share and it is defaulting to the SMB home when trying to mount?

joshbuddy
New Contributor III

Here is where I start to get a little confused, does the "share" refer to where it's mounted on the machine receiving it or refer to the share on the server itself. For example server.stuff.org/test

would I just put "test" in the share section or the entire path... or is it reference to the client machine and I need to have an already existing directory with which to mount it to?

Thanks again for all your patience and continued responses a million posts later

Re: [Casper] Script to mount SMB share?

Thomas Larkin to:
josh.glassing
08/13/2010 10:09 AM

Cc:
"Lance Ogletree", "Casper List", casper-bounces, "Craig S. Ernst"

The only thing I can think of, is that you aren't inputting the proper ServerAddress or share and it is defaulting to the SMB home when trying to mount?

stevewood
Honored Contributor II
Honored Contributor II

Looking at the mountNetworkShare.sh script, in this case "share" refers to
On Fri, Aug 13, 2010 at 10:13 AM, <josh.glassing at spps.org> wrote:
the share point on the server itself. So yes, in your example, "test" is
what would be put in the share section, or variable.

Steve Wood
Director of IT
swood at integer.com

The Integer Group | 1999 Bryan St. | Ste. 1700 | Dallas, TX 75201
T 214.758.6813 | F 214.758.6901 | C 940.312.2475

ernstcs
Contributor III

Share is a reference to the actualy share name on the server.

So if you had a path: smb://server.uwec.edu/sharename

Server: server.uwec.edu
Share: sharename

The script creates the local folders that mount will use to mount it to based on that information as well.

Craig E

tlarkin
Honored Contributor

so if you wanted to connect to a nested folder under a share point this
is how you should code it

server=10.20.10.20 share=finance_docs

So normally if you hit ? + K and connect to server by typing in
smb://10.20.10.20/finance_docs you need to fill in the share point from
the shared server. So from the resource kit...

# HARDCODED VALUES SET HERE loginUsername="$3" shareUsername="$3"#The username of the user to be used to mount the
share authType="kerberos"#Valid values are "kerberos" (default) or "password"

password=""#Note this only needs to be set if authentication type is
"password" mountType="smb"#The type of file share. Valid types are "afp" (default)
or "smb" serverAddress="10.20.10.20"#The IP address or DNS name of the server -
if left blank, the script will search for the "SMBHome" attribute in the
user record share="finance_docs"#The name of the share you are mounting - if left
blank, the script will search for the "SMBHome" attribute in the user
record

I hope that clears things up and makes sense

joshbuddy
New Contributor III

Ok, I got my stuff to work, here is what I ended up doing. I ended up creating a policy to have casper run a command at login to mount

sudo -u nameofuser mkdir /Volumes/Test/; sudo -u nameofuser mount -t smbfs //nameofuser:password at server/Test /Volumes/Test

and then another policy to unmount it at logout

umount /Volumes/Test

I was having all sorts of goofy problems tryin to get casper to run the scripts, if I ran the scripts from terminal when I had them locally stored on a machine they ran fine, but when I'd upload them to casper admin and try to run them they'd keep telling me there was an error on line 1 ... I am not sure what the problem is with that, when I get more time I will probably dig into it deeper... but I got stuff working the way I need it to.

Thank you to everyone for your input
-Josh

Re: [Casper] Script to mount SMB share?

Thomas Larkin to:
josh.glassing
08/13/2010 10:26 AM

Cc:
"Lance Ogletree", "Casper List", casper-bounces, "Craig S. Ernst"

so if you wanted to connect to a nested folder under a share point this is how you should code it

server=10.20.10.20 share=finance_docs

So normally if you hit ? + K and connect to server by typing in smb://10.20.10.20/finance_docs you need to fill in the share point from the shared server. So from the resource kit...

# HARDCODED VALUES SET HERE loginUsername="$3" shareUsername="$3"#The username of the user to be used to mount the share authType="kerberos"#Valid values are "kerberos" (default) or "password" password=""#Note this only needs to be set if authentication type is "password" mountType="smb"#The type of file share. Valid types are "afp" (default) or "smb" serverAddress="10.20.10.20"#The IP address or DNS name of the server - if left blank, the script will search for the "SMBHome" attribute in the user record share="finance_docs"#The name of the share you are mounting - if left blank, the script will search for the "SMBHome" attribute in the user record

I hope that clears things up and makes sense

ernstcs
Contributor III

Permissions problem on the file?

Craig E

On 8/13/10 3:15 PM, "josh.glassing at spps.org" <josh.glassing at spps.org> wrote:

Ok, I got my stuff to work, here is what I ended up doing. I ended up creating a policy to have casper run a command at login to mount

sudo -u nameofuser mkdir /Volumes/Test/; sudo -u nameofuser mount -t smbfs //nameofuser:password at server/Test /Volumes/Test

and then another policy to unmount it at logout

umount /Volumes/Test

I was having all sorts of goofy problems tryin to get casper to run the scripts, if I ran the scripts from terminal when I had them locally stored on a machine they ran fine, but when I'd upload them to casper admin and try to run them they'd keep telling me there was an error on line 1 ... I am not sure what the problem is with that, when I get more time I will probably dig into it deeper... but I got stuff working the way I need it to.

Thank you to everyone for your input
-Josh

Re: [Casper] Script to mount SMB share?

Thomas Larkin to: josh.glassing 08/13/2010 10:26 AM

Cc: "Lance Ogletree", "Casper List", casper-bounces, "Craig S. Ernst"

Not applicable

I'm a little late here but I've had my own stuff in place for a while and
it's different. Perhaps you could try it and see if it makes a difference.
This is a line from my login script. Without modding, $building is defined
earlier, this gives them the server name and $1 is the current user as far
as OS X is concerned not Casper. I suppose you could just change that to $3
for Casper.

#change focus to current user to run as them
su $1 -c /bin/sh <<EOF
#Make a place to mount the share for the user
mkdir /Volumes/$1
#initiate the Kerberos mount command and attach to the directory we made
just above
mount_afp "afp://;AUTH=Client%20Krb%20v2@"$building"xxx01.domain.org/Users/$1"
/Volumes/$1
#We're done as the current user
EOF

OR, without my building variable

su $1 -c /bin/sh <<EOF
mkdir /Volumes/$1
mount_afp "afp://;AUTH=Client%20Krb%20v2@someserver.domain.org/Users/$1"
/Volumes/$1
EOF

HTH

- JD