From 5985e0f5a4f34d50d4fb54dffc2f525f2326bd0b Mon Sep 17 00:00:00 2001 From: Zack T Date: Thu, 6 May 2021 19:22:16 -0700 Subject: [PATCH] v1.1.0 = Logic improvements and updates for Big Sur + Numerous overall logic improvements + Updated to account for xpath change in Big Sur (fixes determining the `Friendly Model Name`) --- .../Run-BomgarSupportOnDemand.sh | 235 ++++++++++++------ 1 file changed, 160 insertions(+), 75 deletions(-) diff --git a/Software/Beyond Trust Bomgar/Run-BomgarSupportOnDemand.sh b/Software/Beyond Trust Bomgar/Run-BomgarSupportOnDemand.sh index 9115962..ef8e9a6 100644 --- a/Software/Beyond Trust Bomgar/Run-BomgarSupportOnDemand.sh +++ b/Software/Beyond Trust Bomgar/Run-BomgarSupportOnDemand.sh @@ -3,12 +3,13 @@ ################################################################################################### # Script Name: Run-BomgarSupportOnDemand.sh # By: Zack Thompson / Created: 5/11/2020 -# Version: 1.0.0 / Updated: 5/11/2020 / By: ZT +# Version: 1.1.0 / Updated: 5/6/2021 / By: ZT # # Description: Utilizing the Bomgar API, downloads a Bomgar Support Client and assigns it to the # supplied team's queue based on the passed parameters. # -# Documentation: https://www.beyondtrust.com/docs/remote-support/how-to/integrations/api/session-gen/index.htm +# Documentation: +# https://www.beyondtrust.com/docs/remote-support/how-to/integrations/api/session-gen/index.htm # # Inspired by: Neil Martin # - https://soundmacguy.wordpress.com/2017/04/18/integrating-bomgar-and-jamf-self-service/ @@ -20,128 +21,212 @@ echo "***** Run BomgarSupportOnDemand Process: START *****" ################################################## # Define Variables -echo "Building attributes..." - issueCodeName="${4}" bomgarSiteURL="${5}" +temp_dir="/private/tmp" + +# Fail if an Issue Code Name isn't provided +if [[ "${issueCodeName}" == "" ]]; then + echo "ERROR: The OnDemand Support Group was not provided." + echo "***** Run BomgarSupportOnDemand Process: FAILED *****" + exit 1 +fi # Set the Default URL if not provided if [[ "${bomgarSiteURL}" == "" ]]; then - bomgarSiteURL="bomgar.company.org" + bomgarSiteURL="bomgar.company.org" fi -# Fail if an Issue Code Name isn't provided -if [[ "${issueCodeName}" == "" ]]; then - echo "ERROR: The OnDemand Support Group was not provided." - echo "***** Run BomgarSupportOnDemand Process: FAILED *****" - exit 1 -fi - -# Gett he Serial Number -serialNumber=$( /usr/sbin/ioreg -c IOPlatformExpertDevice -d 2 | /usr/bin/awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}' ) - -# Determine the length and get the required characters -if [[ ${#serialNumber} -eq 12 ]]; then - - serialIdentifer=$( echo "${serialNumber}" | /usr/bin/tail -c 5 ) - -elif [[ ${#serialNumber} -eq 11 ]]; then - - serialIdentifer=$( echo "${serialNumber}" | /usr/bin/tail -c 4 ) - -fi - -# Get the Friendly Model Name -friendlyModelName=$( /usr/bin/curl -s "https://support-sp.apple.com/sp/product?cc=${serialIdentifer}" | /usr/bin/xmllint --format - | /usr/bin/xpath "/root/configCode/text()" 2>/dev/null ) - -# Assign Firnedly Model Name and Serial Number to a Variable -customerDetails="${friendlyModelName}, ${serialNumber}" - -# Get the Console User -consoleUser=$( /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' ) - -# Get the Console Users' Full Name -fullName=$( /usr/bin/dscl . -read "/Users/${consoleUser}" dsAttrTypeStandard:RealName | /usr/bin/awk -F 'RealName:' '{print $1}' | /usr/bin/xargs ) - ################################################## # Define Functions exitCheck() { if [[ $1 != 0 ]]; then - echo "ERROR: ${3}" - echo "Reason: ${2}" - echo "Exit Code: ${1}" - echo "***** Run BomgarSupportOnDemand Process: FAILED *****" - exit 2 + echo "ERROR: ${3}" + echo "Reason: ${2}" + echo "Exit Code: ${1}" + echo "***** Run BomgarSupportOnDemand Process: FAILED *****" + exit 2 fi } +xpath_tool() { + + if [[ $( sw_vers -buildVersion ) > "20A" ]]; then + + /usr/bin/xpath -e "$@" + + else + + /usr/bin/xpath "$@" + + fi + +} + +find_bomgar_dmg() { + + # Find any download bomgar .dmg's in the specified directory + /usr/bin/find -E "${temp_dir}" -iregex ".*bomgar-scc-.*[.]dmg" -type f -prune -maxdepth 1 + +} + +bomgar_mounts() { + + # Find any mount Bomgar disk images + /usr/bin/find -E "/Volumes" -iregex ".*bomgar.*" -type d -prune -maxdepth 1 + +} + +eject_disks() { + + # Eject disk image if still mounted + if [[ -e "${1}" ]]; then + + echo "Ejecting: ${1}" + /usr/bin/hdiutil eject "${1}" + + fi + +} + +delete_file() { + + # Delete file if it still exists + if [[ -e "${1}" ]]; then + + echo "Deleting: ${1}" + /bin/rm "${1}" + + fi + +} + ################################################## # Bits staged... +# Get the Serial Number +serialNumber=$( /usr/sbin/ioreg -c IOPlatformExpertDevice -d 2 | + /usr/bin/awk -F\" '/IOPlatformSerialNumber/{print $(NF-1)}' ) + +# Determine the length and get the required characters +if [[ ${#serialNumber} -eq 12 ]]; then + + serialIdentifer=$( echo "${serialNumber}" | /usr/bin/tail -c 5 ) + +elif [[ ${#serialNumber} -eq 11 ]]; then + + serialIdentifer=$( echo "${serialNumber}" | /usr/bin/tail -c 4 ) + +fi + +# Get the Friendly Model Name +friendlyModelName=$( /usr/bin/curl -s \ + "https://support-sp.apple.com/sp/product?cc=${serialIdentifer}" | + /usr/bin/xmllint --format - | xpath_tool "/root/configCode/text()" 2>/dev/null ) + +# Assign Friendly Model Name and Serial Number to a Variable +customerDetails="${friendlyModelName}, ${serialNumber}" + +# Get the Console User +consoleUser=$( /usr/sbin/scutil <<< "show State:/Users/ConsoleUser" | + /usr/bin/awk '/Name :/ && ! /loginwindow/ { print $3 }' ) + +# Get the Console Users' Full Name +fullName=$( /usr/bin/dscl . -read "/Users/${consoleUser}" dsAttrTypeStandard:RealName | + /usr/bin/awk -F 'RealName:' '{print $1}' | /usr/bin/xargs ) + +# Check if any mounts already exist +pre_existing_bomgar_mounts=$( bomgar_mounts ) + +if [[ -n $pre_existing_bomgar_mounts ]]; then + + echo "Cleaning up old Bomgar mounts..." + + while IFS=$'\n' read -r mount_path; do + + eject_disks "${mount_path}" + + done < <(echo "${pre_existing_bomgar_mounts}") + +fi + +# Check if any dmg's already exist +pre_existing_dmg=$( find_bomgar_dmg ) + +if [[ -n $pre_existing_dmg ]]; then + + echo "Cleaning up old Bomgar disk images..." + + while IFS=$'\n' read -r dmg_path; do + + delete_file "${dmg_path}" + + done < <(echo "${pre_existing_dmg}") + +fi + echo "Downloading client..." -# Download the Support Application -- Using a User Agent that downloads a .dmg as I wasn't able get a .zip to extract via cli and work -exitStatus1=$( su - "${consoleUser}" -c "cd /private/tmp/ && /usr/bin/curl \ - --silent --show-error --fail \ - --url \"https://${bomgarSiteURL}/api/start_session\" \ - --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17' \ - -d issue_menu=1 \ - -d codeName=\"${issueCodeName}\" \ - -d customer.name=\"${fullName} (${consoleUser})\" \ - -d customer.details=\"${customerDetails}\" \ - --compressed \ - --remote-name \ - --remote-header-name" - ) +# Download the Support Application -- +# Using a User Agent here that downloads a .dmg as I +# wasn't able get a .zip to extract via cli and work. +exitStatus1=$( su - "${consoleUser}" -c "cd ${temp_dir} && /usr/bin/curl \ + --silent --show-error --fail \ + --url \"https://${bomgarSiteURL}/api/start_session\" \ + --header 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/601.5.17 (KHTML, like Gecko) Version/9.1 Safari/601.5.17' \ + -d issue_menu=1 \ + -d codeName=\"${issueCodeName}\" \ + -d customer.name=\"${fullName} (${consoleUser})\" \ + -d customer.details=\"${customerDetails}\" \ + --compressed \ + --remote-name \ + --remote-header-name" + ) exitCode1=$? exitCheck $exitCode1 "${exitStatus1}" "Failed to download the Remote Support application!" # Get the filename of the .dmg file -bomgarDMG="/private/tmp/$( /bin/ls "/private/tmp/" | /usr/bin/grep -E "bomgar-scc-.*[.]dmg" )" +bomgarDMG=$( find_bomgar_dmg ) # Mount the dmg if [[ -e "${bomgarDMG}" ]]; then - echo "Mounting: ${bomgarDMG}" - su - "${consoleUser}" -c "/usr/bin/hdiutil attach \"${bomgarDMG}\" -nobrowse -noverify -noautoopen -quiet" + echo "Mounting: ${bomgarDMG}" + su - "${consoleUser}" -c "/usr/bin/hdiutil attach \"${bomgarDMG}\" \ + -nobrowse -noverify -noautoopen -quiet" /bin/sleep 2 else - exitCheck 1 "Missing .dmg" "Failed to locate the downloaded file!" + exitCheck 1 "Missing .dmg" "Failed to locate the downloaded file!" fi # Get the name of the mount -bomgarMount=$( /bin/ls /Volumes/ | /usr/bin/grep bomgar ) +bomgarMount=$( bomgar_mounts ) # Get the name of the app -app=$( /bin/ls "/Volumes/${bomgarMount}/" | /usr/bin/grep .app ) +app=$( /usr/bin/find -E "${bomgarMount}" -iregex ".*[.]app" -type d -prune -maxdepth 1 ) echo "Running client..." # Run the Support Application -exitStatus2=$( su - "${consoleUser}" -c "/usr/bin/open \"/Volumes/${bomgarMount}/${app}\"" & ) +exitStatus2=$( su - "${consoleUser}" -c "/usr/bin/open \"${app}\"" & ) exitCode2=$? exitCheck $exitCode2 "${exitStatus2}" "Failed to run the Remote Support application!" +# The Bomgar application should take care of itself, +# so first give it time, but just in case it doesn't... +/bin/sleep 5 + # Perform clean up if still mounted -if [[ -e "/Volumes/${bomgarMount}/" ]]; then +eject_disks "${bomgarMount}" - echo "Ejecting: /Volumes/${bomgarMount}/" - /usr/bin/hdiutil eject "/Volumes/${bomgarMount}" -fi - -# Perform clean up is the disk image still exists -if [[ -e "${bomgarDMG}" ]]; then - - echo "Removing: ${bomgarDMG}" - /bin/rm "${bomgarDMG}" - -fi +# Perform clean up if the disk image still exists +delete_file "${bomgarDMG}" echo "***** Run BomgarSupportOnDemand Process: COMPLETE *****" exit 0 \ No newline at end of file