|Sep 22, 2010, 12:27 AM||#1|
Auto-mount a network drive after connecting to a wireless network
I am trying to figure out how to automatically connect a MacBook to a network drive after connecting to the wireless network that the drive is connected to.
Here is the situation:
I back-up my wife's MacBook via Time Machine to an external hard drive that is connected to my Apple desktop. It works great! I even have her system setup to automatically mount the drive upon log-in. Unfortunately, after leaving our wireless signal and then coming back the time machine drive isn't automatically mounted since she isn't "logging back-in". Is there an automator script that can be built to automatically connect to drive X after connecting to wireless network Z?
|Mac|MacBook 2.4GHz |Phone|Google Nexus One
|Sep 22, 2010, 09:46 PM||#2|
You can't do this with Automator.
The simplest solution would be a shell script running at a set interval, say 5 minutes, it would be set via launchd.
When the shell script runs, it check the SSID you are currently connected to, if it is network Z then it mounts that drive, if not then it doesn't. Essentially you would just have a daemon running in the background checking for that SSID.
I already wrote the shell script, let me know if this interests you and I can move forward with the rest.
It would be very similar to this: http://forums.macrumors.com/showthread.php?t=162636, except kicker.bundle no longer exists but there are solutions to that.
Last edited by calderone; Sep 22, 2010 at 10:02 PM.
|Sep 22, 2010, 10:10 PM||#4|
You know, I could actually use launchd's WatchPaths option to make it happen when networks are transitioned, this thought led me to this.
This is a pretty robust solution, http://tech.inhelsinki.nl/locationchanger/
This would be pretty much the same implementation I had in mind. Give it a shot.
|Sep 22, 2010, 11:59 PM||#5|
|Mac|MacBook 2.4GHz |Phone|Google Nexus One
|Feb 2, 2013, 04:32 AM||#7|
I am responding to this even though it is a couple years old because it shows up in a Google search. Here is what I did to mount disks automatically when I change networks.
First I created an applescript to mount the drive:
tell application "Finder" try mount volume "addressOfYourVolumeGoesHere" end try end tell
Make sure you update the appleScript file to reflect the address of your drive. You can find this by mounting your drive manually then right-click > get info.
Next I created LocationChanger.plist (thank you for the idea calderone) file to listen and act on any changes in the /Library/Preferences/SystemConfiguration folder:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>my.locationchanger</string> <key>ProgramArguments</key> <array> <string>/bin/locationchanger</string> </array> <key>WatchPaths</key> <array> <string>/Library/Preferences/SystemConfiguration</string> </array> </dict> </plist>
Finally I created locationchanger, which is a bit different from what the link that calderone posted was trying to do:
#!/bin/bash # automatically mount network volumes on network change # redirect all IO to /dev/null exec 1>/dev/null 2>/dev/null # wait 2 seconds for connection to be established sleep 2 # get various system information SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '` # SSID will not include spaces so leave them out when entering the name of your network case $SSID in # Home network SSID 'your network SSID goes here. keep the single quotes' ) # do whatever you want to do. I am mounting network drives /usr/local/bin/growlnotify -m "Joined home network and mounting Airport drives..." osascript ~/appleScripts/mountAirportMedia.scpt osascript ~/appleScripts/mountAirportBackup.scpt /usr/local/bin/growlnotify -m "Airport drives mounted!" ;; # Network 2 'network2' ) # do whatever you want to do. /usr/local/bin/growlnotify -m "Joined network 2. Doing nothing." ;; # any unspecified network * ) /usr/local/bin/growlnotify -m "Joined unknown network. Doing nothing." # do whatever you want esac exit 0
That's it and it is working great. I found it easier to just call applescript files then do everything in the shell. But it is up to you.
LocationChanger.plist goes in ~/Library/LaunchAgents/LocationChanger.plist (note: ~/Library/ is in your user home directory and hidden by default.)
locationchanger goes in /bin/locationchanger (note: /bin/ is in your systems root directory and hidden by default.)
If you wrote your own locationchanger make sure it is executable. You can change it with the terminal command:
chmod 755 pathToYourFile
Once you have put all the files in place and are ready to go type:
launchctl load /Library/LaunchAgents/LocationChanger.plist
into the terminal or restart your computer to start it up.
I also used growlnotify to let you know what it is doing.
I have attached the files I used so you can use them instead of creating your own if you want.
|Feb 7, 2013, 06:16 PM||#8|
I was very pleased to see a recent reply to this post just now, since I want to do exactly the same over here.
Although I can't seem to get it to work.
The applescript works, I tested it, it mounts the drives everytime.
I followed the other instructions very closely, but no luck.
No growl notifications some I'm wondering if it is able to launch the script.
Is there any way for me to check that?
when I try to load the launchctl thing, terminal responds that it's already loaded. I have no clue!
Care to help me out a bit?
I'm running Mountain Lion, does that matter?
My wifi is called "Gebakken lucht". Does that mean I have to put 'Gebakkenlucht' in the locationchanger?
BTW, in your instructions you say LocationChanger.plist goes in ~/Library/LaunchAgents/LocationChanger.plist. I guess you meant inside ~/Library/LaunchAgents/
And you probably mean 'launchctl load ~/Library/LaunchAgents/LocationChanger.plist' instead of 'launchctl load /Library/LaunchAgents/LocationChanger.plist'
Hope you could help me out!
|Feb 8, 2013, 06:45 PM||#9|
hey, you might not have growlnotify installed. You can download it here: http://growl.info/downloads just get the Growl 1.2.2 - For 10.6 and 10.5. growlnotify is in the Extras folder.
To test and see if it is your LocationChanger.plist file or the locationchanger script just double click on locationchanger, if it launches a terminal window and mounts the drives then the script is working fine and issue is with the .plist file.
once the launch agent is loaded if you make changes to the plist file you have to unload it via launchctl unload before you can load it again.
you can find out the name of your network by pasting:
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' '
into a terminal window. it will just display the network name as it will be stored in the variable.
also if the file path starts with a "~/" that is your home library. If it starts with a "/" then that is the root of the system. with the exception of the last one you pointed out. My terminal window prompt starts in my home directory so i left out the "~" for that launchctl command.
also, if you want you can add in a line like:
echo -e "Airport drives mounted!" >> ~/Desktop/debug.txt
anywhere in the locationchanger script. anything you type between the quotations will be written to a file on your desktop called debug.txt when the script is run. this helps a lot in locating any issues in the script file but this cannot be placed in the .plist file.
good luck. hope this helps, if you are still suck let me know and I'll do what i can to help.
|Feb 9, 2013, 09:58 AM||#10|
Thanks for the help, maxl!
I'm not there yet...
* growl is working.
* probably it's the locationchanger file. double-clicking it does not mount the drives.
* found the ssid with /System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I\ | grep ' SSID:' | cut -d ':' -f 2 | tr -d ' ' in terminal. I had the ssid correct in the locationchanger, so thats not the issue.
Although: when I was putting this string in terminal, before giving the ssid, terminal says "airport: invalid option --".
Since this string is also in the locationchanger, might this be a problem?
* so by moving the debug string around in the locationchanger, thats how I'll might be able to locate the string where the locationchanger doesn't work? I'll start testing now. I'll keep you informed!
UPDATE: the debug.txt gets updated on every run until the debug string is behind the following line: "case $SSID in". So when the debug string is behind "case $SSID in", the debug.txt doesn't get changed.
Thanks a lot already, help is greatly appreciated!
Last edited by kristiaanpipijn; Feb 9, 2013 at 10:12 AM. Reason: debug testing
|Feb 9, 2013, 02:03 PM||#11|
"airport: invalid option --" isn't an issue, I get that too on mine and it still works fine.
First thing I would check is that the path to your applescript is correct. Maybe you forgot the '~' at the start to indicate your home directory?
If you place echo -e "case 1" >> ~/Desktop/debug.txt in the first statement and echo -e "case 2" >> ~/Desktop/debug.txt in the second statement etc. You will be able to figure out which case it is choosing.
|Feb 10, 2013, 07:37 AM||#12|
Ha! Found it!
I did some searching around on the osascript, because everything else seemed to work fine. That's when I discovered the following.
I had placed the mountscript inside a folder inside the Application Support folder.
I think somehow it wasn't able to handle the space character in the pathname, because when I put the script in a different location (no spaces in pathname) the script is running how it should, growlnotifications and everything.
Any ideas on that?
Anyway, thanks again for the great script and your help!
|Feb 10, 2013, 10:52 AM||#13|
A space is a special character. If you want to use it in a path you have to escape it. In your case you would have needed to type ".../Application\ Support/..." the '\' tells the computer to treat the space as a space instead of the end of the command.
Glad you got it working in the end.
|Jun 11, 2013, 08:18 PM||#14|
can't get it
Hey maxl. Thanks so much for posting these instructions. I'm trying, but I can't seem to get it to work for me. I've downloaded the .zip file that you posted and made changes to locationchanger, and placed it in the /bin/ directory in the root folder. I've also made the necessary changes to the script file and pointed locationchanger to it. When I double-click on locationchanger, terminal opens and the network drive mounts as expected. The difficulty I'm having is that, even with the .plist file in LaunchAgents in my home directory, the drive doesn't mount automatically on startup or login. Any suggestions? Is there something I need to change in the .plist file?
Thanks for your help.
UPDATE: Never mind. It started working all of a sudden. Not sure what the glitch was but it's working great now. Thanks for a great tip!
Last edited by blovell; Jun 11, 2013 at 10:17 PM.
|Jun 12, 2013, 12:58 AM||#15|
Once the .plist file is loaded it just listens for a change in your network configuration file then does whatever you have asked it to, in this case check the network name and mount a drive. A network change has to occur to trigger the mounting of the drives. If you did launchctl load with the network connection already active nothing would happen because nothing in your network configuration file changes so the program would not be not triggered.
Glad you found this useful and that you got it working.
|Oct 25, 2013, 08:09 PM||#16|
Thanks for this tip, it works a treat. One thing I've found is that on waking my Macbook from sleep, the script appears to runs twice. I'm running Mavericks, get the following messages when waking up:
- a system notice that my share drive has been disconnected (this always happened before because I gather the wi-fi turns off when in sleep).
- growl notifications that the drives are being connected - both the connecting and connected notice
- a few seconds later, the first "connecting" notice happens again. The connected notice doesnt appear (presumably because the drive is already connected).
Any thoughts, or ideas of how I can diagnose? It is a minor irritation rather than any real problem. I did a launchctl list and it only shows up once, the script is pretty much as posted above.
|Thread Tools||Search this Thread|
|thread||Thread Starter||Forum||Replies||Last Post|
|Why does Mac OS say 3 users are connected to my shares? I'm the only one connected?||alexreich||Mac OS X Server, Xserve, and Networking||7||Oct 2, 2011 03:16 PM|
|can't connect to wireless since upgrading to Lion||staypuffinpc||Mac Basics and Help||3||Aug 9, 2011 01:59 AM|
|Possible to mount network drive on connection to network?||Dimwhit||Mac Basics and Help||5||Mar 23, 2011 11:16 AM|
|suddenly unable to connect to any wireless networks||lansi23||iPhone||0||Jul 21, 2009 08:33 AM|
|Unable to connect to secured wireless networks||chendo||MacBook Pro||1||Dec 15, 2006 09:36 PM|
All times are GMT -5. The time now is 07:10 AM.