AutoMount shares based on wireless network

Mar 29, '06 05:19:00AM

Contributed by: Eric Coleman

I need to mount shares when I am connected to the main wireless connection here at home. So I wrote a quick ruby script that will do this based on your network name, and mount the shares you have listed.

  1. Make a file called auto_mount_shares, open in a text editor.

  2. The script is:

    #!/usr/bin/ruby
    
    # network name to automount on
    mnt_network = "network-name"
    
    # share names to mount
    # mnt_shares["SHARE-NAME"] = "afp://USER:PASSWORD@IP/SHARE_NAME"
    mnt_shares            = Hash.new
    mnt_shares["backups"] = "afp://eric:password@10.1.1.2/backups"
    #mnt_shares["music"]  = "afp://music:music@10.1.1.2/music"
    
    
    network = `system_profiler SPAirPortDataType | grep -e "Current Wireless Network:"`
    network = network.sub(/Current Wireless Network: /, "").strip
    
    mnt_shares.each { |share_name,location|
      `cd /Volumes; mkdir #{share_name}; mount -t afp #{location} #{share_name} >> /dev/null 2>&1`
    }

  3. Move the file to /usr/local/bin (or somewhere of your liking, but I'll assume you've put it in /usr/local/bin).

  4. In Terminal, run:
    chmod u+x /usr/local/bin/auto_mount_shares
Now, to get it to execute upon login, we will add it as a LoginHook. In Terminal, do the following:
sudo defaults write com.apple.loginwindow \
LoginHook /usr/local/bin/auto_mount_shares
[robg adds: I haven't tested this one...]

Comments (4)


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