Shell Script for Samba/SMB Problems with Leopard

November 24th, 2007 · No Comments

So far the only problem I’ve had with my new MBP and Leopard has been mounting shares from a Samba share on a Linux box we have at work. For some reason, I can’t just goto Go > Connect to Server in my Finder menu like I should be able to. After a few hours of trying everything on both the Linux box and my MacBook, I figured out the only way I could view my share was by connecting manually using the command line or by mounting the share to a folder also using the command line.I was sort of disappointed because I wanted my shares to connect as any other external drive would – a USB hard drive for example. If I connect the device, it shows up on my desktop until I unmount. If it’s not connected, nothing shows up. I decided to write a BASH shell script to take care of this for me.

A few key things to remember when creating a script:

1. To get the script to execute when you double click the file, it must have the “.command” extension in the filename. You can simply create a blank plain text file, write your script and save it as filename.command.

2. The script must have execute permissions. The easiest way to do this is to pop open the terminal and type this:

 chmod +x /path/to/File.command

After you’ve created your script, saved it to the location of your choice, and given it the proper permissions it should work just great. The last step (in my case) is to have your script run at login. Goto your System Preferences > Accounts > Select Your Account and click on the “Login Items” tab. Now just drag your .command file into the list and presto you’re done!

Below is my script, obviously you’ll need to customize it for your needs, but it should help you as a step in the right direction. I couldn’t find anything like this when I Googled so hopefully this is of help to someone.

This script will: 1) Search for the Samba server by IP Address, if it exists 2) create a folder on your desktop and 3) mount the SMB share to the folder it created, or… if the share doesn’t exist (can’t be pinged) it will attempt to remove the empty share folders from your desktop if they exist. Click “Read More…” to view the entire script.

  1. #!/bin/sh
  3. # Put Samba Host IP as Host
  4. host="XXX.XXX.XXX.XXX"
  6. # Desktop Path with trailing slash
  7. desktop="/Users/USERNAME/Desktop/"
  9. # Share 1 Name
  10. shr1="SHARENAME"
  12. # Share 2 Name
  13. shr2="SHARENAME"
  15. echo "Searching for Samba Host ($host)…"
  17. # Ping the host to see if it exists
  18. outp=`ping -c 1 $host | grep "0% packet loss"`
  20. # Based on ping create folders and mount
  21. # or don’t mount and delete folders if they exist
  23. if [ "$outp" = "1 packets transmitted, 1 packets received, 0% packet loss" ]; then
  24.     echo "Found $host, mounting file systems…"
  25.     # Share 1
  26.     dir1=${desktop}${shr1}
  27.     if [ ! -d "$dir1" ]; then
  28.         # Can’t Find Directory So Create It
  29.         echo "Creating Mount Point: $dir1";
  30.         mkdir "$dir1"
  31.     else
  32.         echo "Found Mount Point: $dir1"
  33.     fi
  34.     if [ -d "$dir1" ]; then
  35.         echo "Mounting…"
  36.         mount_smbfs //username:password@XXX.XXX.XXX.XXX/SHARENAME /Users/USERNAME/Desktop/SHARENAME
  37.     fi
  39.     # Share 2
  40.     dir2=${desktop}${shr2}
  41.     if [ ! -d "$dir2" ]; then
  42.         # Can’t Find Directory So Create It
  43.         echo "Creating Mount Point: $dir2";
  44.         mkdir "$dir2"
  45.     else
  46.         echo "Found Mount Point: $dir2"
  47.     fi
  48.     if [ -d "$dir1" ]; then
  49.         echo "Mounting…"
  50.         mount_smbfs //username:password@XXX.XXX.XXX.XXX/SHARENAME /Users/USERNAME/Desktop/SHARENAME
  51.     fi
  52. else
  53.     echo "Could not find $host…"
  54.     dir1=${desktop}${shr1}
  55.     if [ ! -d "$dir1" ]; then
  56.         # Can’t Find Directory So Do Nothing
  57.         echo "No Unused Mount Point for $dir1"
  58.     else
  59.         echo "Found Unused Mount Point: $dir1"
  60.         echo "Removing…"
  61.         rmdir "$dir1"
  62.     fi
  64.     dir2=${desktop}${shr2}
  65.     if [ ! -d "$dir2" ]; then
  66.         # Can’t Find Directory So Do Nothing
  67.         echo "No Unused Mount Point for $dir2"
  68.     else
  69.         echo "Found Unused Mount Point: $dir2"
  70.         echo "Removing…"
  71.         rmdir "$dir2"
  72.     fi
  73. fi
  75. # 3 Second Timeout before Closing
  76. read -t 3
  78. # NOTE: If you have other Shell Scripts, or the is running
  79. #       enabling the next line will cause the entire to close.
  80. #       If you are sure that you can kill the Terminal process feel free to
  81. #       uncomment the following line so that the Terminal window the script brings
  82. #       up will automatically close when finished
  84. # KillAll Terminal


Tags: Mac