Run script in OS X 10.6 on network connection. (like /etc/network/if-up.d/)
Solution 1
You could try MarcoPolo or one of the similar utilities listed on its website. Location Changer looks promising if you're a minimalist.
Solution 2
A launchd
agent watching /etc/resolv.conf
, and two network related .plist files under /Library/Preferences/SystemConfiguration/
seems to work for me (in Mac OS X 10.8.4):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" \
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>ifup.ddns</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/Shared/bin/ddns-update.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/etc/resolv.conf</string>
<string>/Library/Preferences/SystemConfiguration/NetworkInterfaces.plist</string>
<string>/Library/Preferences/SystemConfiguration/com.apple.airport.preferences.plist</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
I had previously only used /etc/resolv.conf
, but there were cases where that wasn't enough.
Solution 3
You should consider using crankd, which precisely allows you to run scripts in response to many system events such as network changes, filesystem activity, application launching, etc.
As I couldn't find any sensible documentation, I also wrote a small blog post on getting started using crankd.
Solution 4
This seems to work in bash:
(echo -e "n.add State:/Network/Global/IPv4\nn.watch" & cat) | \
scutil | \
awk '/notification/ {system("echo change")}'
replace echo change
with your command, keeping in mind that if you need to quote anything in your command, you'll need to use '\''
:
(echo -e "n.add State:/Network/Global/IPv4\nn.watch" & cat) | \
scutil | \
awk '/notification/ {system("echo '\''&'\''")}'
Solution 5
ControlPlane: “Context Sensitive Computing”
ControlPlane is a direct port of MarcoPolo and in fact, much of the configuration from MarcoPolo still works with ControlPlane, just better! ControlPlane supports 32 and 64bit Intel based Macs running Snow Leopard and higher.
Related videos on Youtube
Yehia
Updated on September 17, 2022Comments
-
Yehia almost 2 years
Is there any way to run a script when a network interface comes up in Mac OS X?
I've tried putting a script named ip-up in /etc/ppp but it doesn't seem to fire when I disconnect and reconnect to a wireless network.
I'd prefer not to have to run a cron job to constantly check whether the network status has changed. In some linux distros, you can accomplish this by putting a script in /etc/network/if-up.d/ . Is there anything similar for Mac OS X?
-
Yehia over 13 yearslaunchd seems like it's a step in the right direction, especially if I can figure out how other programs use it to detect network changes.
-
Yehia over 13 yearsI'd prefer to do this without installing additional programs. However, the Location Changer link was very helpful since it includes a launchd configuration for running the program on a network change.
-
HikeMike over 13 yearsZxaos: Sure, that's why I included it. It's basically a launchd/bash script template for whatever you want. Saves you from writing the boilerplate code yourself.
-
Dan Rosenstark over 13 yearsif I can bother you to look at this very-relevant q (superuser.com/questions/265861) I'd much appreciate it (how to start and stop vpn from command line on OSX). Excuse the soliciting, I'll delete this comment in a few hours regardless. Thanks in any case!
-
HikeMike over 13 years@Yar Not a problem, but it's past midnight in central Europe right now, so please wait half a day or so before pinging me again (and please do so if nothing comes up in the meantime, it's an interesting question).
-
Dan Rosenstark over 13 yearsThanks @Daniel Beck. As you perhaps already saw, using Applescript was the answer. It probably is to many things on OSX, but I often forget it.
-
Janusz almost 10 yearsLocation Changer is not maintained anymore. Use ControlPlane instead
-
LiberalArtist about 9 yearsThis was very helpful to me, but there's an error (found by [this answer][1]): the opening
<plist version="1.0">
tag is missing. [1][apple.stackexchange.com/a/181127/56862] -
nfirvine about 9 yearsYour blog is protected from anonymous reading. Please consider duplicating the content here.
-
John Smith about 9 yearsSorry about that. My blog has moved, so I've just updated the url. Feel free to edit the answer if you feel that some of the information from my post could be migrated into this answer.
-
IceFire almost 6 years@mivk What exactly is supposed to change in these
SystemConfiguration
files? If I plug/unplug a network cable, nothing seems to change at all -
Slaven Rezic almost 5 yearsThe URL leads to a 404 page.
-
CaseyIT almost 5 years@Slaven-Rezic Updated answer and removed old link.
-
Dormouse almost 5 yearsThis seems close, but if I turn wifi off and back on, then the program only runs when the network disconnects, not when it reconnects. I do see that the
/etc/resolv.conf
file is deleted and readded when my wifi state changes, so I'm not sure why it's only running on disconnect.