0

I want my mac to automatically download updates for me only if I am connected to a specific WiFi network. Is this possible with a script? If so, how?

  • I'm not sure exactly what it would be but the script would look like disable updates, enable updates when connected to specific wifi and disable again when disconnected. The script would most likely run as a cron job so there is a small chance that the update would be on the wrong network. – Thomas Nelson Aug 8 at 5:16
1

here is a working script, you just need customise it by changing the target_network value, which is the network you wish it to Enable AutoUpdates. And for all other wifi networks it will disable updates.

#/bin/bash

# Get WiFi network name
wifi_network=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -I | grep '\sSSID:' | sed 's/.*: //'`

# Set the name of the target network where you
# want it to ENABLE Automatic Downloads
#
target_network='MyHome_Network'


echo "Currently Connected to: $wifi_network wifi network"


if [ "$wifi_network" == "$target_network" ] ;
        then `sudo defaults write /Library/Preferences/com.apple.SoftwareUpdate AutomaticDownload -bool TRUE`
        echo "Download updates ENABLED";
else
        `sudo defaults write /Library/Preferences/com.apple.SoftwareUpdate AutomaticDownload -bool FALSE`
        echo "Download updates DISABLED";
fi

You have to put it to run on cron as root, or create a launchctl system service daemon (it is just a .plist XML file) to schedule it to run at some time interval.


For testing, you can read the current system updating settings with this command: sudo defaults read /Library/Preferences/com.apple.SoftwareUpdate

Run the script, read the settings, you will see it changes.


Complement:

here is a good tutorial to create a launchd daemon [with a XML example file included on the tutorial] to run a bash script as job in time intervals, I am adding it here because it fits perfectly as a complement to my answer, and will explain step by step how you can put the script above that I created, as a launchd daemon.

https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs

  • I'd replace grep '\sSSID:' | sed 's/.*: //' with awk '/ SSID:/{print $2}' as it eliminates the use of an extra pipe and does the same thing in with one command instead of two commands. – user3439894 Aug 11 at 0:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .