Mount an SMB share to Desktop at Login

Discussion in 'Mac Basics and Help' started by redbergy, Nov 20, 2007.

  1. redbergy macrumors newbie

    Joined:
    Nov 7, 2007
    #1
    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!!!
     
  2. jacg macrumors 6502a

    jacg

    Joined:
    Jan 16, 2003
    Location:
    UK
    #2
    I was wondering just the same thing today too!

    If you go to the finder preferences (general) you can select to have connected servers appear there. This may answer one of your questions.

    Unfortunately you are way ahead of me with the other stuff but I too would like to have a smb share auto-mount on login. I shall be watching this thread and I am sure someone will help out.

    I found this on another site: (not tested yet)

    Try connecting to your Windows shares this way. Works fine for me in Leopard. (No spaces in the actual address.) For added convenience, you then may want to add the share as a Login Item in the Accounts System Preferences.

    cifs://workgroup name or domain name:windows username@IP address/share name

    (http://forums.hexus.net/news/122033-major-mac-os-x-leopard-smb-bug-solved.html)
     
  3. redbergy thread starter macrumors newbie

    Joined:
    Nov 7, 2007
    #3
    Thanks for the reply Jacg, I have posted this in other forums and it seems I was being a bit vague in what I'm trying to do... So far I have a script that works, however if I'm not connected to my work network, it still creates the directories on my desktop and tries to mount the smb share (however it's unsuccessful and just dies off).

    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.

    Someone pointed me to this link: http://www.ils.unc.edu/courses/2006_fall/inls668_001/shell_script_samples/ping_monitor/00ReadMe.html saying I just need an IF ELSE statement, I'm going to check it out when I get home (since I've spent entirely too much time on this today already lol) and I'll post what I come up with.
     
  4. redbergy thread starter macrumors newbie

    Joined:
    Nov 7, 2007
    #4
    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.
    Code:
    #!/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	
    
     
  5. bsmesquita macrumors newbie

    Joined:
    Sep 14, 2009
    #5
    It was working...

    Guys,

    This script was working great... then I upgraded to Snow Leopard and it's not working anymore... :(

    I keep getting the same error telling me that the host is not there, but I'm absolutely sure it 's... I can even ping it outside of the script.

    Anyway, is there some kind of "adjustment" that needs to be done in order to get it working in SL?

    Thanks,
    Bruno Mesquita
     
  6. redbergy thread starter macrumors newbie

    Joined:
    Nov 7, 2007
    #6
    It seems the SL ping command has had a very minor change - it now goes into decimals when telling us packet loss - I typed this in my Terminal:

    ping google.com
    PING google.com (74.125.45.100): 56 data bytes
    64 bytes from 74.125.45.100: icmp_seq=0 ttl=50 time=36.644 ms
    ^C
    --- google.com ping statistics ---
    1 packets transmitted, 1 packets received, 0.0% packet loss
    round-trip min/avg/max/stddev = 36.644/36.644/36.644/0.000 ms

    Notice the 0.0% in bold. This will stop the script from thinking it found your host because it's looking for "0%" instead of "0.0%". Simply change this line:

    Code:
    outp=`ping -c 1 $host | grep "0% packet loss"`
    to:

    Code:
    outp=`ping -c 1 $host | grep "0.0% packet loss"`
    and this line:

    Code:
    if [ "$outp" = "1 packets transmitted, 1 packets received, 0% packet loss" ]; then
    to:

    Code:
    if [ "$outp" = "1 packets transmitted, 1 packets received, 0.0% packet loss" ]; then
    Hopefully that will do the trick, there may be other problems with the script (I don't use it any more and I haven't tested with SL) but at first glance this is a definite problem. Let me know if that works for you.
     
  7. bsmesquita macrumors newbie

    Joined:
    Sep 14, 2009
    #7
    thanks redbergy!

    I did realized the change in the ping command, and I did replace the command lines you mentioned... but for some reason it didn't work for me...

    Then you reply and I copied your code to the script and it worked!!! Go figure...

    Anyways, it just needs some little adjustments but I'll be alright!

    Thanks a lot for your prompt reply! Great script by the way... what are you using now to replace your solution?

    Regards,
    Bruno.
     
  8. redbergy thread starter macrumors newbie

    Joined:
    Nov 7, 2007
    #8
    Bruno,

    I'm glad you're getting some good use out of the script. We swapped out servers (from Debian to FreeBSD) and SMB just started working like a charm. I'm still completely in the dark as to why this happens. Swapping servers makes me think it's a config thing on the share, but then why can we connect using command line? Hurts my head to think about it again LOL :)
     
  9. wkearney99 macrumors member

    wkearney99

    Joined:
    Dec 14, 2008
    Location:
    Bethesda, MD USA
    #9
    But why bother to ping at all? Some hosts are not going to have the ICMP protocol enabled, it'll be blocked by their firewall. Just use the mount command and catch the error there. That'll either connect or fail, and is the more appropriate place to check. That and you don't waste the cycles, time and network traffic.
     

Share This Page