Skip to content

Instantly share code, notes, and snippets.

@tbridge
Created February 7, 2017 16:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tbridge/45832cec31dffc464ea7d3aaaae2241d to your computer and use it in GitHub Desktop.
Save tbridge/45832cec31dffc464ea7d3aaaae2241d to your computer and use it in GitHub Desktop.
A short bash script to change settings in munki and notify a Slack channel.
#!/bin/bash
# This script is designed to evaluate a current ManagedInstalls.plist file, then make changes to the specified key. The script will then log a success message to a Slack webhook.
MPL="/Library/Preferences/ManagedInstalls.plist"
DEFAULTS="/usr/bin/defaults"
PREFSTOCHECK=()
SHOULDBE=()
CHANGED=()
DIDSET=()
CHANNEL="#random"
SCUTIL="/usr/sbin/scutil"
SERIAL=( `system_profiler SPHardwareDataType | awk '/Serial/ {print $4}'` )
CLIENTIDENT=( `$SCUTIL --get LocalHostName` )
SLACKHOOK="https://hooks.slack.com/services/yourhookgoeshere"
PREFSTOCHECK[0]=SoftwareRepoURL
PREFSTOCHECK[1]=ClientIdentifier
PREFSTOCHECK[2]=InstallAppleSoftwareUpdates
SHOULDBE[0]=https://brookland.technolutionary.com/munki_repo
SHOULDBE[1]=$CLIENTIDENT
SHOULDBE[2]=1
echo "First up: Are you an admin user? Enter your password below:"
#Let's see if this works...
#This isn't bulletproof, but this is a basic test.
sudo whoami > /tmp/quickytest
if
[[ `cat /tmp/quickytest` == "root" ]]; then
echo "Privilege Escalation Allowed, Please Continue."
else
echo "Privilege Escalation Denied, User Cannot Sudo."
exit 6 "You are not an admin user, you need to do this an admin user."
fi
echo "Checking your current Munki settings and see if they need changing."
tLen=${#PREFSTOCHECK[@]}
for (( i=0; i<tLen; i++));
do
echo $i
CHECK=$( $DEFAULTS read $MPL ${PREFSTOCHECK[$i]} )
echo $CHECK
if
[[ $CHECK == ${SHOULDBE[$i]} ]]; then
echo "Hey, cool, the [$i] value is what it's supposed to be! You're all done here."
else
echo "Okay, the current value of $CHECK doesn't match what we want it to be. Let's get that changed over to our desired value, ${SHOULDBE[$i]}."
sudo $DEFAULTS write $MPL ${PREFSTOCHECK[$i]} ${SHOULDBE[$i]}
CHANGED[$i]=${PREFSTOCHECK[$i]}
DIDSET[$i]=${SHOULDBE[$i]}
fi
done
echo ${#CHANGED[@]}
echo ${CHANGED[*]}
# We've made a change, now let's tell Slack."
IDENT=$( $DEFAULTS read $MPL ClientIdentifier )
read -d '' SLACK_PAYLOAD_DATA << EOF
{
"channel": "$CHANNEL",
"username": "MunkiBot",
"icon_emoji": ":monkey_face:",
"attachments": [
{
"fields": [
{
"title": "Munki Client Update",
"value": "This is a client update from $IDENT (Serial Number $SERIAL). I have updated ${#CHANGED[@]} settings successfully." ,
"short": false
}
]
}
]
}
EOF
SLACK_COMMAND="curl -X POST --data-urlencode 'payload=${SLACK_PAYLOAD_DATA}' ${SLACKHOOK}"
eval ${SLACK_COMMAND}
echo "All Set. I've reported this to $CHANNEL on your Slack."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment