Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

AutoMount shares based on wireless network Network
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...]
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[12,809 views]  

AutoMount shares based on wireless network | 4 comments | Create New Account
Click here to return to the 'AutoMount shares based on wireless network' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Typo
Authored by: bruderfranz on Mar 29, '06 07:02:36AM
Last line should be

sudo defaults write com.apple.loginwindow \
LoginHook /usr/local/bin/auto_mount_shares


[ Reply to This | # ]
AutoMount shares based on wireless network
Authored by: Eric Coleman on Mar 29, '06 07:07:25AM

I'll reply in the comments today with an updated version of this. There is a small problem with the one above.

1) It should use a StartupItem so I can give it a network dep.
2) It needs to chown the directory before it's mounted so you have read/write.

I'll post the updated files in a zip when I get home.

Regards,
Eric



[ Reply to This | # ]
AutoMount shares based on wireless network
Authored by: foilpan on Mar 29, '06 09:36:00AM

shouldn't you use launchd for this under 10.4.x?



[ Reply to This | # ]
AutoMount shares based on wireless network
Authored by: TheCrunge on Mar 29, '06 09:51:42AM
You can actually get a script to run when certain elements of the network stack change. For example, if the IPv4 changes, the default interface, the location, and so on. For this purpose, I'll show the location change:

Add dict entry to /System -> Library -> SystemConfiguration -> Kicker.bundle -> Contents -> Resources -> Kicker.xml:
<dict> 
      <key>execCommand</key> 
      <string>/path/to/script.sh</string> 
      <key>execUID</key> 
      <integer>0</integer> 
      <key>keys</key> 
      <array> 
         <string>Setup:/</string>  
      </array> 
      <key>name</key> 
      <string>Label_Here</string> 
   </dict>
The specific item is in the array (Setup:/ is for the network location). For example you can add any or all of these items to get the script to run based on the item that changes:
<dict> 
      <key>execCommand</key> 
      <string>/path/to/script.sh</string> 
      <key>execUID</key> 
      <integer>0</integer> 
      <key>keys</key> 
      <array> 
         <string>Setup:/</string> 
         <string>State:/Network/Interface/en0/Link</string> 
         <string>State:/Network/Interface/en1/Link</string> 
         <string>State:/Network/Global/DNS</string> 
         <string>State:/Network/Global/IPv4</string> 
         <string>State:/Network/Global/IPv6</string> 
         <string>State:/Network/Global/NetInfo</string> 
      </array> 
      <key>name</key> 
      <string>Test</string> 
   </dict>
Just remember that if any part of the script fails, kicker will disable it. You'll need to restart (or HUP configd).

[ Reply to This | # ]