Waking up a NAS device (a complete solution)

Dec 13, '10 07:30:00AM

Contributed by: Anonymous

I purchased a LaCie NAS device, and I make use the Deep Sleep mode available on the device to reduce the power comsumption. The device has a software called LaCie Network Assistant and there is a function to Wake Up the device. According to the manual this function should remember the MAC address after the first use but it does not. That's why I started looking for another way of waking up the device automatically.

I found this hint on your site but it doesn't really do what I wanted.

Then I found WakeOnLan for use from the command line. There's a link to the developer page.

Download the file and place the wol executable inside /usr/bin.

The easiest way to do this is by using the Finder menu Go » Go To Folder » /usr/bin/. Just drag-n-drop the file there. You may need to chmod the permissions of the file to 755.

I was then able to create this script. Here is what it does:

Here's the script. Open your favorited text editor and copy/paste this:
#!/bin/sh

echo #
echo # Mount NetworkDrive
echo #
# NAS IP address
host=192.168.1.2

# Local Network Range
LOCAL_NETWORK=192.168.1.

# NAS MAC Address
MAC_ADDRESS=00:00:00:00:00:00

# Share to mount
MNT_POINT=/Volumes/Data
VOLUME=/Volumes/
# Share 1 Name
SHR1=Data

# Look for network connection
echo "Looking for the right network connection..."
outp=`ifconfig | grep "inet ${LOCAL_NETWORK}*"`

# echo "$outp"

if [ "$outp" != "" ]; then
  echo "We have a connection!"

  # Ping the host to see if it exists
  echo "Ping $host..."
  outp=`ping -c 1 $host | grep "0.0% packet loss"`
  # echo "$outp"
  if [ "$outp" = "1 packets transmitted, 1 packets received, 0.0% packet loss" ]; then
    echo "Found $host..."
  else
    echo "Ping unsuccessful - Device sleeping"
    echo "Send WAKE UP signal to the device"
    wol -q $MAC_ADDRESS
    echo "Waiting for device..."
    sleep 240
  fi

  MNT_INFO=$(mount | awk -v mnt="$MNT_POINT" '{if ($3 == mnt) print $0}')
  if ["$MNT_INFO" == ""]; then
    echo "Mount point not available, perhaps just woke up"
    echo "Check if Directory exist..."
    # Share 1
    dir1=${VOLUME}${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... afp://$host/$SHR1 $VOLUME$SHR1"
      mount_afp afp://"$host""/$SHR1/" "$VOLUME""$SHR1/"  
    fi
  else
  echo "NAS already mounted!"
  fi
else
  echo "No Valid Network Connection Available... this script is useless..."
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
Save the file as 'MountNAS.command' and put it somewhere in your command path. Set the permissions to 755.

[crarko adds: I haven't tested this one. I mirrored the script here.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20101211080846643