Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 5 down vote accepted

You could try MarcoPolo or one of the similar utilities listed on its website. Location Changer looks promising if you're a minimalist.

share|improve this answer
1  
I'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. – Zxaos Oct 20 '10 at 14:01
1  
Zxaos: 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. – Daniel Beck Oct 20 '10 at 15:09
    
if 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! – Dan Rosenstark Apr 2 '11 at 20:45
1  
@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). – Daniel Beck Apr 2 '11 at 23:15
    
Thanks @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. – Dan Rosenstark Apr 3 '11 at 13:34

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.

share|improve this answer
    
This 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] – LiberalArtist Apr 16 '15 at 4:53
    
@patrix (and LiberalArtist): Thanks for the correction. I hadn't noticed, because it worked for me in 10.8.5. I guess later versions are more strict. – mivk Apr 18 '15 at 15:16

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.

share|improve this answer
    
Your blog is protected from anonymous reading. Please consider duplicating the content here. – nfirvine Apr 30 '15 at 17:49
    
Sorry 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. – Juan A. Navarro May 9 '15 at 16:37

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.

enter image description here enter image description here enter image description here

share|improve this answer

Start with launchd. You might be able to similar functionality using it.

share|improve this answer
    
launchd 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. – Zxaos Oct 20 '10 at 13:58
    
@Zxaos, launchd seems like a step in the right direction, but there is no reliable way to make it detect network changes. See my answer for a solution using crankd instead. – Juan A. Navarro Mar 24 '11 at 13:06

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 '\''&'\''")}'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.