|Sep 21, 2010, 11:27 PM||#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, 08: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 09:02 PM.
|Sep 22, 2010, 09: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, 10:59 PM||#5|
|Mac|MacBook 2.4GHz |Phone|Google Nexus One
|Feb 2, 2013, 03: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, 05: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, 05: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, 08: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 09:12 AM. Reason: debug testing
|Feb 9, 2013, 01: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, 06: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, 09: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, 07: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 09:17 PM.
|Jun 11, 2013, 11:58 PM||#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, 07: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.
|Dec 17, 2013, 10:18 AM||#17|
Can't get it to work
your solution is just what i searched for. Did everything just like explained in your "instructions.rtf" but i can't get it working.
I'm trying to explain the circumstances, so maybe you or someone else can figure out what went wrong:
1. My OS is OS X 10.9
2. I did all the necessary changes in the scripts and locationchanger - not sure at that point if the SSID has to be entered just like shown in the Network-Settings-Panel e.g. "Office" or like its stored in /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist with the cryptical name?
3. When i change networks just nothing happens.
4. To figure out where the problem is i tried to start locationchanger manually, the terminal appears and shows:
Nicolass-MacBook-Pro:~ Nicolas$ /bin/locationchanger ; exit; logout [Prozess beendet]
5. I tried to load the *.plist manually - that leads to a message telling me it's already loaded. So this part seems to work.
6. I tested my scripts manually - they work fine.
7. I used your terminal command to make locationchanger executable.
Now i'm really at the end of my knowledge and hope someone can help to get this working.
8. Installed growlnotify to get some feedback - and that shows where the problem is: location changer seems to recognize all networks as unspecified networks. So i played around a bit with the SSID in location changer and tested both the screen name e.g. 'office' and the cryptical one e.g. ‘RmlybWE=' stored in /Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist
Both ways didn't bring success.
So my thought now is: Did Mavericks change anything that could affect the first part of locationchanger?
#!/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 ' '`
Last edited by DeJung; Dec 17, 2013 at 10:53 AM. Reason: Update
|Dec 17, 2013, 04:25 PM||#18|
I did change something when I switched to Mavericks but I think it was just to use the notification center instead of growl. Does your SSID have a space or other special character in it?
And about the script running twice, I have a fix for that. I can post it later tonight. It is basically just an if statement with a delay but I can't remember exactly how I implemented it. When I get home I'll have a look. Sorry I forgot to repond to your question earlier.
|Dec 18, 2013, 12:34 AM||#19|
No my SSID are just simple words like 'office' and 'home'.
If it's ok for you i can attach my personalized locationchanger and i'd beg you to have a quick look at it if i messed something up.
Could you provide me the code snippet for using Notification Center instead of Growl?
|Dec 21, 2013, 10:03 AM||#20|
Sure, upload it and i'll have a look.
To use the notification center add the following line to locationchanger:
Place the attached application in your /bin folder (where locationchanger is)
change the growl notify lines to look like this:
$notify -message "mounting Airport drives..." -title "Joined home network" -sender com.apple.Terminal
I didn't write terminal-notifier. It is from here: https://github.com/alloy/terminal-notifier
I don't seem to get the issue with it running the scripts twice anymore and I don't have the version with the delay in it either (got a new computer, must have missed it when i copied my files over.) Sorry.
Last edited by maxl; Dec 21, 2013 at 10:22 AM.
|Dec 22, 2013, 09:16 AM||#21|
Thanks for helping Maxl!
Here's my personalized version of 'locationchanger'. Notification-Center support already implemented.
But it doesn't work at all - i change networks and nothing happens.
If you have any questions, feel free to contact me.
|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 02:16 PM|
|can't connect to wireless since upgrading to Lion||staypuffinpc||Mac Basics and Help||3||Aug 9, 2011 12:59 AM|
|Possible to mount network drive on connection to network?||Dimwhit||Mac Basics and Help||5||Mar 23, 2011 10:16 AM|
|suddenly unable to connect to any wireless networks||lansi23||iPhone||0||Jul 21, 2009 07:33 AM|
|Unable to connect to secured wireless networks||chendo||MacBook Pro||1||Dec 15, 2006 08:36 PM|
All times are GMT -5. The time now is 08:48 PM.