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

Automatically mount shares based on location Network
One thing that frequently bothered me about OS X was the lack of any way to automatically mount Windows file shares. At home, my DHCP server assigns me an IP of 10.0.x.x, and I want to mount my home shares, which include access to my media library, personal documents, etc. I also didn't know if I'd be connecting to my network via wifi or ethernet, so I would ideally like to check both. Well, some quick googling didn't really find an adequate solution, so I cobbled together my own.

First, we'll write an AppleScript to actually mount the Samba share. Start by running the Script Editor, and writing:
try
mount volume "smb://username:password@server_ip/share_name"
end try
Then, hit the Compile button. Assuming you got no errors, we're ready to save. Press File, Save As, and enter .mount as the filename (and make sure you are saving in your home directory). Choose Script as the file format, and then hit save. Now, we need a way to make this script run on boot/wakeup, if and only if we're on our home network. This requires an install of sleepwatcher, a great freeware utility.

Open the terminal, type nano -w ~/.wakeup and paste this text into the Terminal. As the script is written, it only mounts the volume if your computer's IP address's first two octets are 10.0.x.x. Feel free to modify to taste.

[robg adds: I haven't tested this one.]
    •    
  • Currently 2.40 / 5
  You rated: 2 / 5 (5 votes cast)
 
[34,357 views]  

Automatically mount shares based on location | 7 comments | Create New Account
Click here to return to the 'Automatically mount shares based on location' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Automatically mount shares based on location
Authored by: fresler on Dec 27, '06 09:24:47AM

You can also use my Home And Away application which can be found at http://[link]maccrafters.com/home_and_away/index.html[/link]



[ Reply to This | # ]
Automatically mount shares based on location
Authored by: Bruno Grieco on Dec 27, '06 11:39:04AM

I'm not sure I understood your problem.

Have you tried Bonjour for Windows. It's free and available from Apple's site.
Once installed on a PC, you may access it thru machine-name.local instead of a changing DHCP ip.
After mounting the server for the first time with your Applescript, just drag the server icon to the icon bar of a finder window.

Presto, everytime you want to mount the server, all you have to do is click on the icon.



[ Reply to This | # ]
Without the freeware
Authored by: nschum on Dec 27, '06 02:32:53PM
You don't need any software, you can also the following to
/System/Library/SystemConfiguration/Kicker.bundle/Contents/Resources/Kicker.xml

<dict>
<key>execCommand</key>
<string>/path/to/share_connect_sh</string>
<key>execUID</key>
<integer>0</integer>
<key>keys</key>
<array>
<string>Setup:/</string>
<string>State:/Network/Global/IPv4</string>
</array>
<key>name</key>
<string>ConnectSamba</string>
</dict>

And I use the following to determine the network, which more precise, but only works on wireless
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | fgrep -i " ssid" | awk '{print $2}'

More on this somewhere in the comments of http://www.macosxhints.com/article.php?story=20051124110051666


Now I'd really like to mount the share from a shell script directly. mount_smbfs seems to work, but it doesn't popup the volume in Finder ...

[ Reply to This | # ]
Automatically mount shares based on location
Authored by: brynthomas on Dec 27, '06 02:50:17PM

This is a great hint, especially when you keep your iTunes library on your network!

I did come across two issues when using it though, maybe these solutions will help someone else:

1) The script that's linked seems to have a problem on the 14th line with a break between the pr and int. Join the word back together as print and it works.

2) You need to run chmod 744 ~/.wakeup from the Terminal before it will work.



[ Reply to This | # ]
Automatically mount shares based on location
Authored by: gghose on Dec 27, '06 05:59:11PM
I've written a freeware package that mounts shares (including NFS), resets privoxy, iTunes, Mail, Skype, and proxy settings automatically according to location. Supports Growl and PPP and VPN connections as well. It's called LocationChanger:
[link:]http://www.versiontracker.com/dyn/moreinfo/macosx/30710



[ Reply to This | # ]
Automatically mount shares based on location
Authored by: jwibi on Feb 08, '07 05:27:26AM
it works as "advertised" if you iron out the following kinks in the bash script:

1. make sure there are no line feeds (as pointed out by brynthomas above) (there are two in the text - both with the long lines)

2. line 6:

status_ethernet=$(ifconfig en0 |grep status| awk '{print $4}')
should have
print $6
instead of
print $4
in it. (otherwise it did not work for me)

that's it.
great hint!
thx j

[ Reply to This | # ]

Automatically mount shares based on location
Authored by: cella_door on Mar 15, '09 05:04:13PM

Hi there,

I have just installed sleepwatcher and setup this little script on my Macbook (OSX 10.5). I have corrected the linefeed and print issues as mentioned above, however I am having problems.

While I can make both .wakeup and .sleep run, calls to ifconfig within .wakeup seem to be returning nothing, therefore none of the decision logic seems to functioning properly. if I run .wakeup manually from a terminal it works! It "feels" like a permissions issue but I dont know why.

Does anybody have any ideas as to why this script is not working correctly for me??

Many thanks



[ Reply to This | # ]