Login Script to Mount SMB Share to Desktop?

Hello Mac Experts! I just started drinking the Mac Kool-Aid when I ordered my first Mac - a MBP - last week. I must admit it tastes way better than the Windoze 3 day old cold coffee I'm used to. So far I'm loving it, but I'm running into one issue on my work network.

After trying for 3 hours with our Linux guru we could not get Finder to open a connection to our file/web server which is Debian (Linux) through Samba. I tried everything I could think of and there's not much help on Google.

Using the command line mount_smbfs I was finally able to mount the samba share on our Debian to a folder in my home directory. Right now this is fine, for the rest of the day I'll be able to work, but I'm just wondering if there is a way I can create a script that runs when I login that does the following:

1) At login check if my Server is available (192.168.1.216) (this in case I'm using my MBP on the road or from home)

2) Run this command if the server exists:
mount_smbfs //myusername:mypassword@192.168.1.216/share /pathto/mountfolder/ondesktop

3) The last part is the key (I guess) - I want this share to show up on my Desktop like when I connect a USB drive or how my Macintosh HD does

I'm assuming it's fairlly easy to create a script that runs at login? I've Googled a little with no success but I think the tricky part will making sure the 192.168.1.216 is a valid server -- or maybe there doesn't even need to be a check it just runs the command and if it's not there nothing happens? The other tricky part for me is figuring out where exactly I need to mount the drive to so that it appears on my desktop as a mounted drive instead of in my /Users/username/smbMount folder that I arbitrarily made.

The later is what I really need to konw as I'm sure with alot of Googling I can find the best way to create a login script in Leopard, but I have no idea where to mount it to and I'm afraid to just start trying to mount to some device in /dev/ which (I think) could potentially screw things up.

I'm looking for a step by step here if someone wouldn't mind helping out this Mac Newb.

Thanks in advance!!!

MBP, Mac OS X (10.5.1)

Posted on Nov 20, 2007 8:11 AM

Reply
8 replies

Nov 20, 2007 10:21 AM in response to redbergy

Have some faith! This task is not as hard as you think. I'm not at home right now on my Mac so I can't give you a step by step but I am doing this at home the same as your asking. I'll try to explain what I can and if you can't get it let me know and I'll write up a how to tonight. The easy way to do this just to test your Samba setup is to go into Finder. From Finder "apple"-k. That will open a connect to server window. Then enter "smb://192.168.x.x/sharename". It will prompt for any username and password you have set through Samba. Once it's connected it will show in your Finder window on the left as a shared drive. Getting it on your desktop is just an option in Finder pref.

If all the above works than your Samba setup is correct and the hard part is done. In order for it to be done as an automated task you need to do 2 more steps.
1st- create a automater task to connect to the server or servers. Save the automater task as a program. Save it in your home folder.
2nd- In account pref. just add the automater task as a start-up item for your login user.
I say save it under your home folder so if you get kicked off for some reason "ie- network problem" you can simply reconnect by just running the task again. Easy! You don't need to run anything special for a laptop away from home because the automater task will just quit if it can't see the server you set up. Automater is just like scripting only easier!!

Let me know if you need the step-by-step for the automater setup.

Nov 20, 2007 11:19 AM in response to Jason Roberts1

Jason,

Thanks for the reply, I guess I should have been more clear in my post. For some reason, I cannot use Go > Connect to Server... in the Finder options. I get some type of error to the effect of "You do not have permission to access this drive" - when I type in an incorrect login I get a different error saying "Bad username or password". I can connect to this samba server using Windows and other Linux machines. I can also connect using smb in the command line of my Mac and I can mount the drive using mount_smbfs but for some reason I can't do the easy Finder way.

This is why I am asking about a start up script since it seems the only way I can access this share is by typing in the command line. I have a basic script going below, however I would like it to remove the two directories it creates first at startup, determine whether I can mount the share, then if I can, recreate those folders (that way if I'm not at work, I won't see the empty folders on my desktop)... Below is my script (.command file set to run at login):

mkdir /Users/adam/Desktop/blxshare
mkdir /Users/adam/Desktop/blxweb
mount_smbfs //username:password@192.168.1.216/blxshare /Users/adam/Desktop/blxshare
mount_smbfs //username:password@192.168.1.216/blxweb /Users/adam/Desktop/blxweb
KillAll Terminal
exit

So, like I said, first, I need to add a rm /Users/adam/Desktop/blxshare, then I need some way to check and see if 192.168.1.216 exists, if so, recreate the folder and mount the SMB share.

Nov 20, 2007 11:40 AM in response to redbergy

Sorry for the misunderstanding. I think your going about this the hardest way but to each his/her own. The way I have described is the way I have it set up. In most of the forum posts I've seen Samba is set up incorrectly. Your setup doesn't sound like thats your problem thus I jumped to Finder setup. I can say that Leopard has added some funny network problems w/ Samba. The setup I wrote I know 1st hand works because I use it every day on my system. The only thing I can add to help aid in your trouble shooting is that Leopard's Samba interface and Tiger's Samba interface is different. I still have yet to find an easy way to connect to multiple Samba shares using different usernames/passwds on Leopard. Tiger would keychain passwds for each mounting point. Leopard seems to keychain by server. This is the problem I've come across in my home network where one server has multiple share points for family members. Don't know if this helps you at all but take it for what it's worth.

Nov 20, 2007 12:05 PM in response to redbergy

This is probably wrong, seeing as I don't own a Mac (yet), but for contacting your server, could the ping command be of any help?
Something like, Terminal tries to ping server, if it fails, Kill Terminal. If it succeeds, mount and open SMB share in Finder window.

I'm actually I found a thread asking this question, as I'll probably be using it at my home for quick access to my WinXP account, and school files.

Like I said, I'm not sure exactly how ping works, but it's worth a shot IMO.

Nov 20, 2007 12:16 PM in response to redbergy

Terminal skills are a little rusty. I believe you just need to do a "if" statement in your script. Try this link:
http://www.ils.unc.edu/courses/2006fall/inls668_001/shell_script_samples/pingmonitor/00ReadMe.html

Make a script to ping for life of the server then if it's good mount your drives, else: end if.

I still say this is the hard way!! Solve your Finder issues and your life becomes easy!

Nov 20, 2007 8:50 PM in response to Jason Roberts1

Okay here is my Startup script, currently it supports 2 shares (you can easily edit it to change it to more than 2 or only one) I hope this helps someone else.

Put this into a file with a .command extension and don't forget to hop in the terminal and type chmod +x myfile.command to make sure it has execute permissions. Drag the command file to your login startup applications and you should be good.

Edit: You're going to want to remove the \'s around the quotes and \# signs, the Mac forums aren't cooperating with me today.
<pre>
#!/bin/sh

\# Put Samba Host IP as Host
host="192.168.1.xxx"

\# Desktop Path with trailing slash
desktop="/Users/user/Desktop/"

\# Share Username
username="yourusername"

\# Share Password
password="yourpassword"

\# Share 1 Name
shr1="share1"

\# Share 2 Name
shr2="share2"

\# Ping the host to see if it exists
outp=`ping -c 1 $host | grep "0% packet loss"`

\# Based on ping create folders and mount
\# or don't mount and delete folders if they exist

if [ "$outp" = "1 packets transmitted, 1 packets received, 0% packet loss" ]; then
echo "Found $host, mounting file systems..."

\# Share 1
dir1=${desktop}${shr1}
if [ ! -d "$dir1" ]; then
\# Can't Find Directory So Create It
echo "Creating Mount Point: $dir1";
mkdir "$dir1"
else
echo "Found Mount Point: $dir1"
fi
if [ -d "$dir1" ]; then
echo "Mounting..."
mount_smbfs //"$username":"$password"@"$host"/"$dir1" "$desktop""$shr1"
fi

\# Share 2
dir2=${desktop}${shr2}
if [ ! -d "$dir2" ]; then
\# Can't Find Directory So Create It
echo "Creating Mount Point: $dir2";
mkdir "$dir2"
else
echo "Found Mount Point: $dir2"
fi
if [ -d "$dir1" ]; then
echo "Mounting..."
mount_smbfs //"$username":"$password"@"$host"/"$dir2" "$desktop""$shr2"
fi

else
echo "Could not find $host..."

dir1=${desktop}${shr1}
if [ ! -d "$dir1" ]; then
\# Can't Find Directory So Do Nothing
echo "No Unused Mount Point for $dir1"
else
echo "Found Unused Mount Point: $dir1"
echo "Removing..."
rmdir "$dir1"
fi

dir2=${desktop}${shr2}
if [ ! -d "$dir2" ]; then
\# Can't Find Directory So Do Nothing
echo "No Unused Mount Point for $dir2"
else
echo "Found Unused Mount Point: $dir2"
echo "Removing..."
rmdir "$dir2"
fi
fi

\# NOTE: If you have other Shell Scripts, or the Teriminal.app is running
\# enabling the next line will cause the entire Terminal.app to close.
\# If you are sure that you can kill the Terminal process feel free to
\# uncomment the following line so that the Terminal window the script brings
\# up will automatically close when finished

\# KillAll Terminal
</pre>

Message was edited by: redbergy

Message was edited by: redbergy

Nov 27, 2007 10:48 PM in response to redbergy

Why do you need to specify mount points? Once mounted they should automatically show on the desktop as long as you check off the finder preference to show connected servers on desktop, it seems to be turned off by default.

Wouldn't an automator action of:
get selected servers - smb://192..../share1 smb://192.../share2
connect to server

get you to the same place?

I seem to be having problems connecting to my NAS by name and was looking for other solutions people had for mounting SMB servers automatically on login.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Login Script to Mount SMB Share to Desktop?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.