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.
-
#!/bin/sh
-
-
# Put Samba Host IP as Host
-
host="XXX.XXX.XXX.XXX"
-
-
# Desktop Path with trailing slash
-
desktop="/Users/USERNAME/Desktop/"
-
-
# Share 1 Name
-
shr1="SHARENAME"
-
-
# Share 2 Name
-
shr2="SHARENAME"
-
-
echo "Searching for Samba Host ($host)…"
-
-
# 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@XXX.XXX.XXX.XXX/SHARENAME /Users/USERNAME/Desktop/SHARENAME
-
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@XXX.XXX.XXX.XXX/SHARENAME /Users/USERNAME/Desktop/SHARENAME
-
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
-
-
# 3 Second Timeout before Closing
-
read -t 3
-
-
# 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