Random Stuff of Randomness

Simon's

macOS action on network change

macOS – with its Posix roots – does come with a couple of useful network services auh as autofs and sshfs to name a few. When moving around, I join different networks and in each network, I ant to have different resources available. The following collection of scripts makes it possible to mount and unmount resources depending on location.

launchd

launchd can bee used to observe a couple of files. When they change something in the network configuration has changed:

/Library/LaunchDaemons/

<?xml version="1.0" encoding="UTF-8"?>
<!-- see `man 5 launchd.plist` -->

<!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>net.wunderlin.launch.network-change</string>
    <key>LowPriorityIO</key>
    <true />

    <key>ProgramArguments</key>
    <array>
      <string>/Users/Shared/bin/network-change.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>

network-change.sh

1#!/usr/bin/env bash
2
3# check for search domain
4SEARCH=$(grep search /etc/resolv.conf)
5
6syslog -l 5 -s "network-change.sh $SEARCH"

install.sh

1#!/usr/bin/env bash
2
3mkdir -p /Users/Shared/bin
4cp network-change.sh /Users/Shared/bin/
5cp network-change.plist /Library/LaunchDaemons
6launchctl load /Library/LaunchDaemons/network-change.plist
7launchctl list | grep net.wunderlin