Skip to content

Instantly share code, notes, and snippets.

@Nosskirneh
Created July 27, 2017 07:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nosskirneh/c19d2da415fb6312672482eba112b156 to your computer and use it in GitHub Desktop.
Save Nosskirneh/c19d2da415fb6312672482eba112b156 to your computer and use it in GitHub Desktop.
Automatically update theos device settings and kill/open Dropbox on network change (macOS)
<?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>networkchange</string>
<key>LowPriorityIO</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>/Users/andeh/Dropbox/bin/updateIP.sh</string>
</array>
<key>WatchPaths</key>
<array>
<string>/etc/resolv.conf</string>
<string>/var/run/resolv.conf</string>
<string>/private/var/run/resolv.conf</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
# Get IP
IP=`ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p'`
echo "IP is $IP"
# Get SSID - Only works on Mac
SSID=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'`
echo "SSID is $SSID"
case $IP in
"192.168.0."*) # Home
open -a "Dropbox"
DEVICE_IP="192.168.0.96"
DEVICE_PORT="22";;
"192.168.1."*) # CH
open -a "Dropbox"
DEVICE_IP="192.168.1.96"
DEVICE_PORT="22";;
"192.168.32."*) # 4G-router
osascript -e 'tell application "Dropbox" to quit'
DEVICE_IP="192.168.32.151"
DEVICE_PORT="22";;
"172.20.10."*) # Tethering
osascript -e 'tell application "Dropbox" to quit'
DEVICE_IP="172.20.10.1"
DEVICE_PORT="22";;
*)
case $SSID in
"Comentor")
open -a "Dropbox"
DEVICE_IP="10.0.1.5"
DEVICE_PORT="22";;
*) # USB, eduroam does not allow SSH
open -a "Dropbox"
DEVICE_IP="localhost"
DEVICE_PORT="2222";;
esac;;
esac
echo "Setting the device IP to $DEVICE_IP"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "export THEOS_DEVICE_IP=${DEVICE_IP}" > "$DIR/theos.sh"
echo "export THEOS_DEVICE_PORT=${DEVICE_PORT}" >> "$DIR/theos.sh"
@Nosskirneh
Copy link
Author

Adapt the script to your liking.

Simply run

launchctl load networkchange.plist
launchctl start networkchange

afterwards. Remember to change the directory to the shell script in the plist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment