Skip to content

Instantly share code, notes, and snippets.

@brunerd
Last active November 7, 2020 04:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunerd/b6a66c432876ef99298ad0acc3e62471 to your computer and use it in GitHub Desktop.
Save brunerd/b6a66c432876ef99298ad0acc3e62471 to your computer and use it in GitHub Desktop.
Report the states of Bluetooth Sharing services in macOS using jpt to parse JSON output from system_profiler
#!/bin/bash
#Joel Bruner
#BTSharingStateas - report bluetooth sharing states from system_profiler using jpt to parse output
#if jpt is not installed locally, embed function from jpt.min into this script: https://github.com/brunerd/jpt
#get all the data once
SPBluetooth=$(system_profiler -json SPBluetoothDataType 2>/dev/null)
#get the paths related to service state
statePaths=$(jpt -jd '$..service_state' <<< "${SPBluetooth}")
:<<STATEPATHS_SAMPLE
$.SPBluetoothDataType[0].services_title[0].file_browsing_title.service_state
$.SPBluetoothDataType[0].services_title[1].object_push_title.service_state
$.SPBluetoothDataType[0].services_title[2].internet_sharing_title.service_state
STATEPATHS_SAMPLE
#go through each path and report
IFS=$'\n'
for path in ${statePaths}; do
#strip out the service name and clean it up
serviceName=$(tr . $'\n' <<< "$path" | awk '/_title/ && !/services_title/ {print $1}' | sed 's/_title//')
#get the state and clean it up to: enabled/disabled
serviceState=$(jpt -T "${path}" <<< "${SPBluetooth}" | sed 's/attrib_//')
echo "${serviceName}: ${serviceState}"
done
#Turn on Intenet and Bluetooth Sharing to test for enablement
:<<OUTPUT_SAMPLE
file_browsing: enabled
object_push: enabled
internet_sharing: disabled
OUTPUT_SAMPLE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment